From 65355f0ea0137bef498d07d95f6649aa7c16acff Mon Sep 17 00:00:00 2001 From: celogeek Date: Sat, 5 Mar 2022 17:21:24 +0100 Subject: [PATCH] check file exists --- internal/photos/api/file.go | 20 ++++++++++++++++++++ internal/photos/api/main.go | 1 + 2 files changed, 21 insertions(+) diff --git a/internal/photos/api/file.go b/internal/photos/api/file.go index f3b9a1d..ac51f93 100644 --- a/internal/photos/api/file.go +++ b/internal/photos/api/file.go @@ -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) + } +} diff --git a/internal/photos/api/main.go b/internal/photos/api/main.go index 71bfb7e..684eb10 100644 --- a/internal/photos/api/main.go +++ b/internal/photos/api/main.go @@ -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)