Compare commits
No commits in common. "c36c2d6c60bb2bf2bf130f632c161daa568e546c" and "9020415e74274b795a1c1a014f5aba90219248e5" have entirely different histories.
c36c2d6c60
...
9020415e74
@ -23,12 +23,7 @@ type UploadCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UploadError struct {
|
type UploadError struct {
|
||||||
Error string `json:"error"`
|
Error string
|
||||||
Status string `json:"string"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UploadCreate struct {
|
|
||||||
UploadId string `json:"upload_id"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type UploadFileRequest struct {
|
type UploadFileRequest struct {
|
||||||
@ -182,22 +177,11 @@ func (c *UploadCommand) FileUpload(sum string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *UploadCommand) Execute(args []string) error {
|
func (c *UploadCommand) Execute(args []string) error {
|
||||||
cli := c.Cli()
|
sum, err := c.FileExists()
|
||||||
resp, err := cli.R().SetError(&UploadError{}).SetResult(&UploadCreate{}).Post("/upload/create")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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() {
|
func init() {
|
||||||
|
@ -76,7 +76,9 @@ func (s *Service) Signup(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Status(http.StatusNoContent)
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"status": "success",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Login(c *gin.Context) {
|
func (s *Service) Login(c *gin.Context) {
|
||||||
@ -97,7 +99,8 @@ func (s *Service) Login(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
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)
|
c.AbortWithError(http.StatusNotFound, ErrSessionNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Status(http.StatusNoContent)
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"status": "success",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) AccountInit() {
|
func (s *Service) AccountInit() {
|
||||||
|
19
internal/photos/api/check_body.go
Normal file
19
internal/photos/api/check_body.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
@ -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()
|
|
||||||
}
|
|
@ -1,37 +1,31 @@
|
|||||||
package photosapi
|
package photosapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrReqMissingBody = errors.New("missing body")
|
StatusSuccess = "success"
|
||||||
|
StatusFailed = "failed"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Service) HandleError(c *gin.Context) {
|
func (s *Service) HandleError(c *gin.Context) {
|
||||||
c.Next()
|
c.Next()
|
||||||
err := c.Errors.Last()
|
|
||||||
if err == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
details := err.Error()
|
if err := c.Errors.Last(); err != nil {
|
||||||
if details == "EOF" {
|
if err.IsType(gin.ErrorTypeBind) {
|
||||||
details = "missing body"
|
c.JSON(-1, gin.H{
|
||||||
}
|
"status": StatusFailed,
|
||||||
|
"error": "binding error",
|
||||||
switch err.Type {
|
"details": strings.Split(err.Error(), "\n"),
|
||||||
case gin.ErrorTypeBind:
|
})
|
||||||
c.JSON(-1, gin.H{
|
} else {
|
||||||
"error": "binding error",
|
c.JSON(-1, gin.H{
|
||||||
"details": strings.Split(details, "\n"),
|
"status": StatusFailed,
|
||||||
})
|
"error": err.Error(),
|
||||||
default:
|
})
|
||||||
c.JSON(-1, gin.H{
|
}
|
||||||
"error": details,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,6 +141,7 @@ func (s *Service) FileCreate(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"status": "success",
|
||||||
"sum": file.Checksum,
|
"sum": file.Checksum,
|
||||||
"nbChunks": len(file.Chunks),
|
"nbChunks": len(file.Chunks),
|
||||||
"size": rs,
|
"size": rs,
|
||||||
@ -155,6 +156,7 @@ func (s *Service) FileCreateChunk(c *gin.Context) {
|
|||||||
|
|
||||||
b := bytes.NewBuffer([]byte{})
|
b := bytes.NewBuffer([]byte{})
|
||||||
io.Copy(b, c.Request.Body)
|
io.Copy(b, c.Request.Body)
|
||||||
|
c.Request.Body.Close()
|
||||||
|
|
||||||
sess := s.CurrentSession(c)
|
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 err := chunk.Save(sess.Account.Login); err != nil {
|
||||||
if errors.Is(err, ErrStoreChunkAlreadyExists) {
|
if errors.Is(err, ErrStoreChunkAlreadyExists) {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"status": "success",
|
||||||
"checksum": chunk.Sum,
|
"checksum": chunk.Sum,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@ -171,6 +174,7 @@ func (s *Service) FileCreateChunk(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"status": "success",
|
||||||
"checksum": chunk.Sum,
|
"checksum": chunk.Sum,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -52,8 +52,8 @@ func (s *Service) SetupRoutes() {
|
|||||||
s.Gin.Use(
|
s.Gin.Use(
|
||||||
gin.Logger(),
|
gin.Logger(),
|
||||||
s.Recovery,
|
s.Recovery,
|
||||||
s.DefaultJSON,
|
|
||||||
s.HandleError,
|
s.HandleError,
|
||||||
|
s.RequireBody,
|
||||||
)
|
)
|
||||||
|
|
||||||
s.AccountInit()
|
s.AccountInit()
|
||||||
|
@ -8,7 +8,8 @@ import (
|
|||||||
|
|
||||||
func (s *Service) Me(c *gin.Context) {
|
func (s *Service) Me(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"user": s.CurrentSession(c).Account.Login,
|
"status": "success",
|
||||||
|
"user": s.CurrentSession(c).Account.Login,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ func (s *Service) Recovery(c *gin.Context) {
|
|||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
s.LogErr.Print("PANIC", err)
|
s.LogErr.Print("PANIC", err)
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"status": StatusFailed,
|
||||||
"error": ErrUnexpected.Error(),
|
"error": ErrUnexpected.Error(),
|
||||||
"details": err,
|
"details": err,
|
||||||
})
|
})
|
||||||
|
@ -36,6 +36,7 @@ func (s *Service) UploadCreate(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusCreated, gin.H{
|
c.JSON(http.StatusCreated, gin.H{
|
||||||
|
"status": "success",
|
||||||
"upload_id": sha.String(),
|
"upload_id": sha.String(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -107,6 +108,7 @@ func (s *Service) UploadPart(c *gin.Context) {
|
|||||||
"part": uploadPart.Part,
|
"part": uploadPart.Part,
|
||||||
"size": w,
|
"size": w,
|
||||||
"sha256": uploadPart.PartSha256,
|
"sha256": uploadPart.PartSha256,
|
||||||
|
"status": "success",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +123,9 @@ func (s *Service) UploadCancel(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Status(http.StatusNoContent)
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"status": "success",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type UploadCompleteRequest struct {
|
type UploadCompleteRequest struct {
|
||||||
@ -144,14 +148,8 @@ func (s *Service) UploadComplete(c *gin.Context) {
|
|||||||
|
|
||||||
func (s *Service) UploadInit() {
|
func (s *Service) UploadInit() {
|
||||||
upload := s.Gin.Group("/upload")
|
upload := s.Gin.Group("/upload")
|
||||||
|
upload.GET("/create", s.UploadCreate)
|
||||||
// upload.GET("/create", s.UploadCreate)
|
upload.POST("/part/:upload_id", s.UploadPart)
|
||||||
// upload.POST("/part/:upload_id", s.UploadPart)
|
upload.GET("/cancel/:upload_id", s.UploadCancel)
|
||||||
// upload.GET("/cancel/:upload_id", s.UploadCancel)
|
upload.POST("/complete/:upload_id", s.UploadComplete)
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user