Compare commits

..

No commits in common. "86ce908b4e974953dce0224ae75d4741f70fa4c1" and "5dbd59fe8f60bd1add7e067fa103f96c5ef02856" have entirely different histories.

3 changed files with 18 additions and 40 deletions

View File

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

View File

@ -2,7 +2,6 @@ package photosapi
import ( import (
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -25,7 +24,7 @@ func (s *Storage) Create(paths ...string) error {
} }
func (s *Storage) Exists(paths ...string) bool { func (s *Storage) Exists(paths ...string) bool {
f, err := s.Stat(paths...) f, err := os.Stat(s.Join(paths...))
if err != nil { if err != nil {
return false return false
} }
@ -39,7 +38,3 @@ func (s *Storage) Delete(paths ...string) error {
return fmt.Errorf("%s doesn't exists", s.Join(paths...)) 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,7 +22,6 @@ var (
ErrUploadNotExists = errors.New("upload id doesn't exists") ErrUploadNotExists = errors.New("upload id doesn't exists")
ErrUploadPartTooLarge = fmt.Errorf("upload part too large (> %d B)", MaxUploadPartSize) ErrUploadPartTooLarge = fmt.Errorf("upload part too large (> %d B)", MaxUploadPartSize)
ErrUploadPartWrongSha256 = errors.New("upload part wrong sha256") ErrUploadPartWrongSha256 = errors.New("upload part wrong sha256")
ErrFileAlreadExists = errors.New("file already exists")
) )
// Model // Model
@ -54,8 +53,6 @@ func (s *Service) UploadCreate(c *gin.Context) {
c.JSON(http.StatusCreated, upload) c.JSON(http.StatusCreated, upload)
} }
// Service
type Upload struct { type Upload struct {
Id string `json:"upload_id" uri:"upload_id" binding:"required,uuid"` Id string `json:"upload_id" uri:"upload_id" binding:"required,uuid"`
} }
@ -65,12 +62,6 @@ type UploadPartQuery struct {
PartSha256 string `form:"sha256" binding:"required,sha256"` 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) { func (s *Service) UploadPart(c *gin.Context) {
var ( var (
upload Upload upload Upload
@ -142,6 +133,12 @@ func (s *Service) UploadCancel(c *gin.Context) {
c.Status(http.StatusNoContent) 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) { func (s *Service) UploadComplete(c *gin.Context) {
var ( var (
upload Upload upload Upload
@ -156,13 +153,6 @@ func (s *Service) UploadComplete(c *gin.Context) {
return 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) c.Status(http.StatusNoContent)
} }