Compare commits

..

No commits in common. "c36c2d6c60bb2bf2bf130f632c161daa568e546c" and "9020415e74274b795a1c1a014f5aba90219248e5" have entirely different histories.

10 changed files with 62 additions and 64 deletions

View File

@ -23,12 +23,7 @@ type UploadCommand struct {
}
type UploadError struct {
Error string `json:"error"`
Status string `json:"string"`
}
type UploadCreate struct {
UploadId string `json:"upload_id"`
Error string
}
type UploadFileRequest struct {
@ -182,22 +177,11 @@ func (c *UploadCommand) FileUpload(sum string) error {
}
func (c *UploadCommand) Execute(args []string) error {
cli := c.Cli()
resp, err := cli.R().SetError(&UploadError{}).SetResult(&UploadCreate{}).Post("/upload/create")
sum, err := c.FileExists()
if err != nil {
return err
}
if err, ok := resp.Error().(*UploadError); ok {
logger.Println(string(resp.Body()))
logger.Println(resp.Error(), resp.StatusCode(), resp.Header())
return errors.New(err.Error)
}
result := resp.Result().(*UploadCreate)
fmt.Printf("Upload create: %s", result.UploadId)
return nil
return c.FileUpload(sum)
}
func init() {

View File

@ -76,7 +76,9 @@ func (s *Service) Signup(c *gin.Context) {
return
}
c.Status(http.StatusNoContent)
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
}
func (s *Service) Login(c *gin.Context) {
@ -97,7 +99,8 @@ func (s *Service) Login(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"token": session.Token,
"status": "success",
"token": session.Token,
})
}
@ -111,7 +114,9 @@ func (s *Service) Logout(c *gin.Context) {
c.AbortWithError(http.StatusNotFound, ErrSessionNotFound)
return
}
c.Status(http.StatusNoContent)
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
}
func (s *Service) AccountInit() {

View File

@ -0,0 +1,19 @@
package photosapi
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
)
var (
ErrReqMissingBody = errors.New("missing body")
)
func (s *Service) RequireBody(c *gin.Context) {
if c.Request.Method == "POST" && c.Request.ContentLength == 0 {
c.AbortWithError(http.StatusBadRequest, ErrReqMissingBody)
return
}
}

View File

@ -1,8 +0,0 @@
package photosapi
import "github.com/gin-gonic/gin"
func (s *Service) DefaultJSON(c *gin.Context) {
c.Header("content-type", "application/json; charset=utf-8")
c.Next()
}

View File

@ -1,37 +1,31 @@
package photosapi
import (
"errors"
"strings"
"github.com/gin-gonic/gin"
)
var (
ErrReqMissingBody = errors.New("missing body")
StatusSuccess = "success"
StatusFailed = "failed"
)
func (s *Service) HandleError(c *gin.Context) {
c.Next()
err := c.Errors.Last()
if err == nil {
return
}
details := err.Error()
if details == "EOF" {
details = "missing body"
}
switch err.Type {
case gin.ErrorTypeBind:
c.JSON(-1, gin.H{
"error": "binding error",
"details": strings.Split(details, "\n"),
})
default:
c.JSON(-1, gin.H{
"error": details,
})
if err := c.Errors.Last(); err != nil {
if err.IsType(gin.ErrorTypeBind) {
c.JSON(-1, gin.H{
"status": StatusFailed,
"error": "binding error",
"details": strings.Split(err.Error(), "\n"),
})
} else {
c.JSON(-1, gin.H{
"status": StatusFailed,
"error": err.Error(),
})
}
}
}

View File

@ -141,6 +141,7 @@ func (s *Service) FileCreate(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
"sum": file.Checksum,
"nbChunks": len(file.Chunks),
"size": rs,
@ -155,6 +156,7 @@ func (s *Service) FileCreateChunk(c *gin.Context) {
b := bytes.NewBuffer([]byte{})
io.Copy(b, c.Request.Body)
c.Request.Body.Close()
sess := s.CurrentSession(c)
@ -162,6 +164,7 @@ func (s *Service) FileCreateChunk(c *gin.Context) {
if err := chunk.Save(sess.Account.Login); err != nil {
if errors.Is(err, ErrStoreChunkAlreadyExists) {
c.JSON(http.StatusOK, gin.H{
"status": "success",
"checksum": chunk.Sum,
})
} else {
@ -171,6 +174,7 @@ func (s *Service) FileCreateChunk(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
"checksum": chunk.Sum,
})
}

View File

@ -52,8 +52,8 @@ func (s *Service) SetupRoutes() {
s.Gin.Use(
gin.Logger(),
s.Recovery,
s.DefaultJSON,
s.HandleError,
s.RequireBody,
)
s.AccountInit()

View File

@ -8,7 +8,8 @@ import (
func (s *Service) Me(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"user": s.CurrentSession(c).Account.Login,
"status": "success",
"user": s.CurrentSession(c).Account.Login,
})
}

View File

@ -18,6 +18,7 @@ func (s *Service) Recovery(c *gin.Context) {
if err := recover(); err != nil {
s.LogErr.Print("PANIC", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"status": StatusFailed,
"error": ErrUnexpected.Error(),
"details": err,
})

View File

@ -36,6 +36,7 @@ func (s *Service) UploadCreate(c *gin.Context) {
}
c.JSON(http.StatusCreated, gin.H{
"status": "success",
"upload_id": sha.String(),
})
}
@ -107,6 +108,7 @@ func (s *Service) UploadPart(c *gin.Context) {
"part": uploadPart.Part,
"size": w,
"sha256": uploadPart.PartSha256,
"status": "success",
})
}
@ -121,7 +123,9 @@ func (s *Service) UploadCancel(c *gin.Context) {
return
}
c.Status(http.StatusNoContent)
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
}
type UploadCompleteRequest struct {
@ -144,14 +148,8 @@ func (s *Service) UploadComplete(c *gin.Context) {
func (s *Service) UploadInit() {
upload := s.Gin.Group("/upload")
// upload.GET("/create", s.UploadCreate)
// upload.POST("/part/:upload_id", s.UploadPart)
// upload.GET("/cancel/:upload_id", s.UploadCancel)
// upload.POST("/complete/:upload_id", s.UploadComplete)
upload.POST("", s.UploadCreate)
upload.DELETE("/:upload_id", s.UploadCancel)
upload.PUT("/:upload_id", s.UploadPart)
upload.POST("/:upload_id", s.UploadComplete)
upload.GET("/create", s.UploadCreate)
upload.POST("/part/:upload_id", s.UploadPart)
upload.GET("/cancel/:upload_id", s.UploadCancel)
upload.POST("/complete/:upload_id", s.UploadComplete)
}