check file exists

This commit is contained in:
celogeek 2022-03-05 17:21:24 +01:00
parent 2361ac8f17
commit 65355f0ea0
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 21 additions and 0 deletions

View File

@ -143,3 +143,23 @@ func (s *Service) FileCreateChunk(c *gin.Context) {
"checksum": chunk.Sum,
})
}
func (s *Service) FileExists(c *gin.Context) {
checksum := c.Param("checksum")
if len(checksum) != 40 {
s.Error(c, http.StatusBadRequest, photoserrors.ErrStoreBadChecksum)
return
}
var fileExists int64
if err := s.DB.Model(&models.File{}).Where("checksum = ?", checksum).Count(&fileExists).Error; err != nil {
s.Error(c, http.StatusInternalServerError, err)
return
}
if fileExists > 0 {
c.Status(http.StatusOK)
} else {
c.Status(http.StatusNotFound)
}
}

View File

@ -61,6 +61,7 @@ func (s *Service) SetupRoutes() {
album.Use(s.RequireSession)
album.POST("", s.FileCreate)
album.POST("/chunk", s.FileCreateChunk)
album.HEAD("/:checksum", s.FileExists)
s.Gin.NoRoute(func(c *gin.Context) {
s.Error(c, http.StatusNotFound, photoserrors.ErrReqNotFound)