mirror of
https://github.com/simon987/ws_bucket.git
synced 2025-04-10 14:06:46 +00:00
Upload slot info endpoint
This commit is contained in:
parent
290c67d3f6
commit
22ef1a44ad
@ -79,6 +79,7 @@ func New(db *gorm.DB) *WebApi {
|
||||
|
||||
router.POST("/slot", LogRequestMiddleware(api.AllocateUploadSlot))
|
||||
router.GET("/slot", LogRequestMiddleware(api.ReadUploadSlot))
|
||||
router.GET("/slot_info", LogRequestMiddleware(api.UploadSlotInfo))
|
||||
router.GET("/upload", LogRequestMiddleware(api.Upload))
|
||||
|
||||
api.server = fasthttp.Server{
|
||||
|
@ -11,6 +11,10 @@ type GenericResponse struct {
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
type GetUploadSlotResponse struct {
|
||||
UploadSlot `json:"upload_slot"`
|
||||
}
|
||||
|
||||
type AllocateUploadSlotRequest struct {
|
||||
Token string `json:"token"`
|
||||
MaxSize int64 `json:"max_size"`
|
||||
|
22
api/slot.go
22
api/slot.go
@ -166,6 +166,28 @@ func executeUploadHook(slot UploadSlot) {
|
||||
}).Info("Execute upload hook")
|
||||
}
|
||||
|
||||
func (api *WebApi) UploadSlotInfo(ctx *fasthttp.RequestCtx) {
|
||||
tokenStr := string(ctx.Request.Header.Peek("X-Upload-Token"))
|
||||
if tokenStr == "" {
|
||||
tokenStr = string(ctx.Request.URI().QueryArgs().Peek("token"))
|
||||
}
|
||||
|
||||
slot := UploadSlot{}
|
||||
err := api.db.Where("token=?", tokenStr).First(&slot).Error
|
||||
|
||||
if err != nil {
|
||||
ctx.Response.Header.SetStatusCode(404)
|
||||
logrus.WithError(err).WithFields(logrus.Fields{
|
||||
"token": tokenStr,
|
||||
}).Warning("Upload slot not found")
|
||||
return
|
||||
}
|
||||
|
||||
Json(GetUploadSlotResponse{
|
||||
UploadSlot: slot,
|
||||
}, ctx)
|
||||
return
|
||||
}
|
||||
func (api *WebApi) ReadUploadSlot(ctx *fasthttp.RequestCtx) {
|
||||
|
||||
tokenStr := string(ctx.Request.Header.Peek("X-Upload-Token"))
|
||||
|
@ -97,8 +97,37 @@ func TestTokenInQueryString(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestUploadSlotInfo(t *testing.T) {
|
||||
|
||||
if allocateUploadSlot(api.AllocateUploadSlotRequest{
|
||||
FileName: "testuploadslotinfo.png",
|
||||
Token: "testuploadslotinfo",
|
||||
MaxSize: 123,
|
||||
}).Ok != true {
|
||||
t.Error()
|
||||
}
|
||||
|
||||
resp := getSlotInfo("testuploadslotinfo")
|
||||
|
||||
if resp.FileName != "testuploadslotinfo.png" {
|
||||
t.Error()
|
||||
}
|
||||
if resp.Token != "testuploadslotinfo" {
|
||||
t.Error()
|
||||
}
|
||||
if resp.MaxSize != 123 {
|
||||
t.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func allocateUploadSlot(request api.AllocateUploadSlotRequest) (ar *api.GenericResponse) {
|
||||
resp := Post("/slot", request)
|
||||
UnmarshalResponse(resp, &ar)
|
||||
return
|
||||
}
|
||||
|
||||
func getSlotInfo(token string) (ar *api.GetUploadSlotResponse) {
|
||||
resp := Get("/slot_info", token)
|
||||
UnmarshalResponse(resp, &ar)
|
||||
return
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user