fix error handling

This commit is contained in:
2023-03-29 14:59:04 +02:00
parent 0e572f0aa4
commit 50d25fe7b7
14 changed files with 96 additions and 48 deletions

View File

@@ -53,7 +53,10 @@ func UploadPost(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
var returnUrl string
var formdata Meta
os.MkdirAll(filepath.Join(cfg.StorageDir, id), os.ModePerm)
if err := os.MkdirAll(filepath.Join(cfg.StorageDir, id), os.ModePerm); err != nil {
return JsonStatus(c, fiber.StatusInternalServerError,
"Unable to initialize directories: "+err.Error())
}
// fetch auxiliary form data
form, err := c.MultipartForm()
@@ -114,7 +117,11 @@ func UploadPost(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
Log("Uploaded with API-Context %s", entry.Context)
// we do this in the background to not thwart the server
go db.Insert(id, entry)
go func() {
if err := db.Insert(id, entry); err != nil {
Log("Failed to insert: " + err.Error())
}
}()
// everything went well so far
res := &common.Response{Uploads: []*common.Upload{entry}}
@@ -131,7 +138,9 @@ func UploadPost(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
if err == nil {
if len(r.Forms) == 1 {
if r.Forms[0].Expire == "asap" {
db.Delete(apicontext, formid)
if err := db.Delete(apicontext, formid); err != nil {
Log("Failed to delete formid %s: %s", formid, err.Error())
}
}
// email notification to form creator
@@ -184,7 +193,11 @@ func UploadFetch(c *fiber.Ctx, cfg *cfg.Config, db *Db, shallExpire ...bool) err
if _, err := os.Stat(filename); err != nil {
// db entry is there, but file isn't (anymore?)
go db.Delete(apicontext, id)
go func() {
if err := db.Delete(apicontext, id); err != nil {
Log("Unable to delete entry id %s: %s", id, err.Error())
}
}()
return fiber.NewError(404, "No download with that id could be found!")
}
@@ -192,12 +205,14 @@ func UploadFetch(c *fiber.Ctx, cfg *cfg.Config, db *Db, shallExpire ...bool) err
err = c.Download(filename, file)
if len(shallExpire) > 0 {
if shallExpire[0] == true {
if shallExpire[0] {
go func() {
// check if we need to delete the file now and do it in the background
if upload.Expire == "asap" {
cleanup(filepath.Join(cfg.StorageDir, id))
db.Delete(apicontext, id)
if err := db.Delete(apicontext, id); err != nil {
Log("Unable to delete entry id %s: %s", id, err.Error())
}
}
}()
}