diff --git a/api/slot.go b/api/slot.go index 442bca0..5d6f3cc 100644 --- a/api/slot.go +++ b/api/slot.go @@ -53,11 +53,18 @@ func (api *WebApi) AllocateUploadSlot(ctx *fasthttp.RequestCtx) { return } - api.allocateUploadSlot(req) + err = api.allocateUploadSlot(req) - Json(GenericResponse{ - Ok: true, - }, ctx) + if err == nil { + Json(GenericResponse{ + Ok: true, + }, ctx) + } else { + Json(GenericResponse{ + Ok: false, + Message: err.Error(), + }, ctx) + } } func (api *WebApi) Upload(ctx *fasthttp.RequestCtx) { @@ -178,7 +185,7 @@ func (api *WebApi) ReadUploadSlot(ctx *fasthttp.RequestCtx) { mu.(*sync.RWMutex).RUnlock() } -func (api *WebApi) allocateUploadSlot(req *AllocateUploadSlotRequest) { +func (api *WebApi) allocateUploadSlot(req *AllocateUploadSlotRequest) error { slot := &UploadSlot{ MaxSize: req.MaxSize, @@ -190,5 +197,5 @@ func (api *WebApi) allocateUploadSlot(req *AllocateUploadSlotRequest) { "slot": slot, }).Info("Allocated new upload slot") - api.db.Create(slot) + return api.db.Create(slot).Error } diff --git a/test/main_test.go b/test/main_test.go index 570355f..8155793 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -4,6 +4,7 @@ import ( "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" "github.com/simon987/ws_bucket/api" + "os" "testing" "time" ) @@ -22,4 +23,5 @@ func TestMain(m *testing.M) { time.Sleep(time.Millisecond * 100) m.Run() + _ = os.Remove("test.db") } diff --git a/test/slot_test.go b/test/slot_test.go index f7583e0..5618df4 100644 --- a/test/slot_test.go +++ b/test/slot_test.go @@ -46,6 +46,25 @@ func TestAllocateUploadSlotUnsafePath(t *testing.T) { } } +func TestDuplicateUploadSlot(t *testing.T) { + + if allocateUploadSlot(api.AllocateUploadSlotRequest{ + FileName: "test.png", + Token: "testdupe", + MaxSize: 100, + }).Ok != true { + t.Error() + } + + if allocateUploadSlot(api.AllocateUploadSlotRequest{ + FileName: "test.png", + Token: "testdupe", + MaxSize: 100, + }).Ok != false { + t.Error() + } +} + func allocateUploadSlot(request api.AllocateUploadSlotRequest) (ar *api.GenericResponse) { resp := Post("/slot", request) UnmarshalResponse(resp, &ar)