Compare commits

..

3 Commits

Author SHA1 Message Date
celogeek
86ce908b4e
check file existant before processing 2022-05-14 19:38:54 +02:00
celogeek
7dae7a4c2b
improve output, delete on exit 2022-05-14 19:38:36 +02:00
celogeek
367f0c978e
add stat for file existant checks 2022-05-14 19:38:08 +02:00
3 changed files with 40 additions and 18 deletions

View File

@ -34,9 +34,10 @@ func (c *UploadCommand) Execute(args []string) error {
if resp.IsError() {
return resp.Error().(*photosapi.ErrorWithDetails)
}
uploadId := resp.Result().(*photosapi.Upload).Id
defer cli.R().SetPathParam("id", uploadId).Delete("/upload/{id}")
f, err := os.Open(c.File)
if err != nil {
return err
@ -86,21 +87,29 @@ func (c *UploadCommand) Execute(args []string) error {
}
}
fmt.Printf(
"Result:\n - Upload ID: %s\n - Parts: %d\n",
completeRequest := &photosapi.UploadCompleteRequest{
Sha256: hex.EncodeToString(completesha256.Sum(nil)),
Parts: uint(parts),
Name: filepath.Base(c.File),
}
fmt.Printf(`Result:
- Upload ID: %s
- Name : %s
- Parts : %d
- SHA256 : %s
`,
uploadId,
parts,
completeRequest.Name,
completeRequest.Parts,
completeRequest.Sha256,
)
resp, err = cli.
R().
SetError(&photosapi.ErrorWithDetails{}).
SetPathParam("id", uploadId).
SetBody(&photosapi.UploadCompleteRequest{
Sha256: hex.EncodeToString(completesha256.Sum(nil)),
Parts: uint(parts),
Name: filepath.Base(c.File),
}).
SetBody(completeRequest).
Post("/upload/{id}")
if err != nil {
@ -111,8 +120,6 @@ func (c *UploadCommand) Execute(args []string) error {
return resp.Error().(*photosapi.ErrorWithDetails)
}
cli.R().SetPathParam("id", uploadId).Delete("/upload/{id}")
return nil
}

View File

@ -2,6 +2,7 @@ package photosapi
import (
"fmt"
"io/fs"
"os"
"path/filepath"
)
@ -24,7 +25,7 @@ func (s *Storage) Create(paths ...string) error {
}
func (s *Storage) Exists(paths ...string) bool {
f, err := os.Stat(s.Join(paths...))
f, err := s.Stat(paths...)
if err != nil {
return false
}
@ -38,3 +39,7 @@ func (s *Storage) Delete(paths ...string) error {
return fmt.Errorf("%s doesn't exists", s.Join(paths...))
}
}
func (s *Storage) Stat(paths ...string) (fs.FileInfo, error) {
return os.Stat(s.Join(paths...))
}

View File

@ -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)
}