Compare commits
5 Commits
9020415e74
...
c36c2d6c60
Author | SHA1 | Date | |
---|---|---|---|
|
c36c2d6c60 | ||
|
41f4062d1b | ||
|
9934ef91c0 | ||
|
8c6ef9c3cc | ||
|
46e0ad988b |
@ -23,7 +23,12 @@ type UploadCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UploadError 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 {
|
type UploadFileRequest struct {
|
||||||
@ -177,11 +182,22 @@ func (c *UploadCommand) FileUpload(sum string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *UploadCommand) Execute(args []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 {
|
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,9 +76,7 @@ func (s *Service) Signup(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.Status(http.StatusNoContent)
|
||||||
"status": "success",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Login(c *gin.Context) {
|
func (s *Service) Login(c *gin.Context) {
|
||||||
@ -99,8 +97,7 @@ func (s *Service) Login(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
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)
|
c.AbortWithError(http.StatusNotFound, ErrSessionNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.Status(http.StatusNoContent)
|
||||||
"status": "success",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) AccountInit() {
|
func (s *Service) AccountInit() {
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
8
internal/photos/api/default.go
Normal file
8
internal/photos/api/default.go
Normal 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()
|
||||||
|
}
|
@ -1,31 +1,37 @@
|
|||||||
package photosapi
|
package photosapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
StatusSuccess = "success"
|
ErrReqMissingBody = errors.New("missing body")
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
if err := c.Errors.Last(); err != nil {
|
details := err.Error()
|
||||||
if err.IsType(gin.ErrorTypeBind) {
|
if details == "EOF" {
|
||||||
c.JSON(-1, gin.H{
|
details = "missing body"
|
||||||
"status": StatusFailed,
|
}
|
||||||
"error": "binding error",
|
|
||||||
"details": strings.Split(err.Error(), "\n"),
|
switch err.Type {
|
||||||
})
|
case gin.ErrorTypeBind:
|
||||||
} else {
|
c.JSON(-1, gin.H{
|
||||||
c.JSON(-1, gin.H{
|
"error": "binding error",
|
||||||
"status": StatusFailed,
|
"details": strings.Split(details, "\n"),
|
||||||
"error": err.Error(),
|
})
|
||||||
})
|
default:
|
||||||
}
|
c.JSON(-1, gin.H{
|
||||||
|
"error": details,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,7 +141,6 @@ 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,
|
||||||
@ -156,7 +155,6 @@ 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)
|
||||||
|
|
||||||
@ -164,7 +162,6 @@ 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 {
|
||||||
@ -174,7 +171,6 @@ 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,8 +8,7 @@ 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{
|
||||||
"status": "success",
|
"user": s.CurrentSession(c).Account.Login,
|
||||||
"user": s.CurrentSession(c).Account.Login,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@ 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,7 +36,6 @@ 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(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -108,7 +107,6 @@ 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",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,9 +121,7 @@ func (s *Service) UploadCancel(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.Status(http.StatusNoContent)
|
||||||
"status": "success",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type UploadCompleteRequest struct {
|
type UploadCompleteRequest struct {
|
||||||
@ -148,8 +144,14 @@ 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.POST("/part/:upload_id", s.UploadPart)
|
// upload.GET("/create", s.UploadCreate)
|
||||||
upload.GET("/cancel/:upload_id", s.UploadCancel)
|
// upload.POST("/part/:upload_id", s.UploadPart)
|
||||||
upload.POST("/complete/:upload_id", s.UploadComplete)
|
// 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)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user