Compare commits

...

5 Commits

Author SHA1 Message Date
celogeek
c36c2d6c60
rework on route 2022-05-09 09:49:54 +02:00
celogeek
41f4062d1b
remove status 2022-05-09 09:49:11 +02:00
celogeek
9934ef91c0
remove check body 2022-05-09 09:48:26 +02:00
celogeek
8c6ef9c3cc
start using new upload api 2022-05-08 18:12:47 +02:00
celogeek
46e0ad988b
remove body close 2022-05-08 17:04:08 +02:00
10 changed files with 64 additions and 62 deletions

View File

@ -23,7 +23,12 @@ type UploadCommand struct {
}
type UploadError struct {
Error string
Error string `json:"error"`
Status string `json:"string"`
}
type UploadCreate struct {
UploadId string `json:"upload_id"`
}
type UploadFileRequest struct {
@ -177,11 +182,22 @@ func (c *UploadCommand) FileUpload(sum string) error {
}
func (c *UploadCommand) Execute(args []string) error {
sum, err := c.FileExists()
cli := c.Cli()
resp, err := cli.R().SetError(&UploadError{}).SetResult(&UploadCreate{}).Post("/upload/create")
if err != nil {
return err
}
return c.FileUpload(sum)
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
}
func init() {

View File

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

View File

@ -1,19 +0,0 @@
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

@ -0,0 +1,8 @@
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,31 +1,37 @@
package photosapi
import (
"errors"
"strings"
"github.com/gin-gonic/gin"
)
var (
StatusSuccess = "success"
StatusFailed = "failed"
ErrReqMissingBody = errors.New("missing body")
)
func (s *Service) HandleError(c *gin.Context) {
c.Next()
err := c.Errors.Last()
if err == nil {
return
}
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(),
})
}
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,
})
}
}

View File

@ -141,7 +141,6 @@ func (s *Service) FileCreate(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
"sum": file.Checksum,
"nbChunks": len(file.Chunks),
"size": rs,
@ -156,7 +155,6 @@ 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)
@ -164,7 +162,6 @@ 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 {
@ -174,7 +171,6 @@ 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,8 +8,7 @@ import (
func (s *Service) Me(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "success",
"user": s.CurrentSession(c).Account.Login,
"user": s.CurrentSession(c).Account.Login,
})
}

View File

@ -18,7 +18,6 @@ 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,7 +36,6 @@ func (s *Service) UploadCreate(c *gin.Context) {
}
c.JSON(http.StatusCreated, gin.H{
"status": "success",
"upload_id": sha.String(),
})
}
@ -108,7 +107,6 @@ func (s *Service) UploadPart(c *gin.Context) {
"part": uploadPart.Part,
"size": w,
"sha256": uploadPart.PartSha256,
"status": "success",
})
}
@ -123,9 +121,7 @@ func (s *Service) UploadCancel(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
c.Status(http.StatusNoContent)
}
type UploadCompleteRequest struct {
@ -148,8 +144,14 @@ 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.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)
}