From 86ce908b4e974953dce0224ae75d4741f70fa4c1 Mon Sep 17 00:00:00 2001 From: celogeek Date: Sat, 14 May 2022 19:38:54 +0200 Subject: [PATCH] check file existant before processing --- internal/photos/api/upload.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/internal/photos/api/upload.go b/internal/photos/api/upload.go index 382f0d6..b81abf8 100644 --- a/internal/photos/api/upload.go +++ b/internal/photos/api/upload.go @@ -22,6 +22,7 @@ var ( ErrUploadNotExists = errors.New("upload id doesn't exists") ErrUploadPartTooLarge = fmt.Errorf("upload part too large (> %d B)", MaxUploadPartSize) ErrUploadPartWrongSha256 = errors.New("upload part wrong sha256") + ErrFileAlreadExists = errors.New("file already exists") ) // Model @@ -53,6 +54,8 @@ func (s *Service) UploadCreate(c *gin.Context) { c.JSON(http.StatusCreated, upload) } +// Service + type Upload struct { Id string `json:"upload_id" uri:"upload_id" binding:"required,uuid"` } @@ -62,6 +65,12 @@ type UploadPartQuery struct { PartSha256 string `form:"sha256" binding:"required,sha256"` } +type UploadCompleteRequest struct { + Sha256 string `json:"sha256" binding:"required,sha256"` + Name string `json:"name" binding:"required"` + Parts uint `json:"parts" binding:"required"` +} + func (s *Service) UploadPart(c *gin.Context) { var ( upload Upload @@ -133,12 +142,6 @@ func (s *Service) UploadCancel(c *gin.Context) { c.Status(http.StatusNoContent) } -type UploadCompleteRequest struct { - Sha256 string `json:"sha256" binding:"required,sha256"` - Name string `json:"name" binding:"required"` - Parts uint `json:"parts" binding:"required"` -} - func (s *Service) UploadComplete(c *gin.Context) { var ( upload Upload @@ -153,6 +156,13 @@ func (s *Service) UploadComplete(c *gin.Context) { return } + f, err := s.StorageUpload.Stat(uploadCompleteRequest.Sha256[0:1], uploadCompleteRequest.Sha256[1:2], uploadCompleteRequest.Sha256) + fmt.Println(err) + if err == nil && f.Mode().IsRegular() { + c.AbortWithError(http.StatusConflict, ErrFileAlreadExists) + return + } + c.Status(http.StatusNoContent) }