From e88399d76d508fd97cf55a5b9140bf009c47cfd0 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 13 Mar 2023 18:48:38 +0100 Subject: [PATCH] enhanced output --- README.md | 5 +++-- upctl/lib/client.go | 14 ++++++++++---- upd/api/handlers.go | 29 +++++++++++++++++++---------- upd/api/server.go | 3 +-- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 028aade..a6dc860 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,12 @@ Simple standalone file upload server with api and cli ## TODO - also serve a html upload page -- add metrics +- add metrics (as in https://github.com/ansrivas/fiberprometheus) - add authorization checks for delete and list based on apicontext -- change output of upload, use the same as list - do not manually generate output urls, use fiber.GetRoute() - import code from upd into upctl to avoid duplicates, like the time stuff we've now +- upd: https://docs.gofiber.io/guide/error-handling/ to always use json output +- upctl: get rid of HandleResponse(), used only once anyway ## BUGS diff --git a/upctl/lib/client.go b/upctl/lib/client.go index 24ba55a..ad17f3c 100644 --- a/upctl/lib/client.go +++ b/upctl/lib/client.go @@ -145,17 +145,15 @@ func UploadFiles(c *cfg.Config, args []string) error { }, 10*time.Millisecond). Post(rq.Url) - fmt.Println("") - if err != nil { return err } - return HandleResponse(c, resp) + return printUploadsResponse(resp) } func HandleResponse(c *cfg.Config, resp *req.Response) error { - // we expect a json response + // we expect a json response, extract the error, if any r := Response{} if err := json.Unmarshal([]byte(resp.String()), &r); err != nil { @@ -252,6 +250,10 @@ func Describe(c *cfg.Config, args []string) error { return err } + return printUploadsResponse(resp) +} + +func printUploadsResponse(resp *req.Response) error { uploads := Uploads{} if err := json.Unmarshal([]byte(resp.String()), &uploads); err != nil { @@ -262,6 +264,10 @@ func Describe(c *cfg.Config, args []string) error { return errors.New(uploads.Message) } + if uploads.Message != "" { + fmt.Println(uploads.Message) + } + WriteExtended(&uploads) return nil diff --git a/upd/api/handlers.go b/upd/api/handlers.go index 3ea4447..f2d5800 100644 --- a/upd/api/handlers.go +++ b/upd/api/handlers.go @@ -28,7 +28,7 @@ import ( "time" ) -func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) { +func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) error { // supports upload of multiple files with: // // curl -X POST localhost:8080/putfile \ @@ -51,8 +51,8 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) { // fetch auxiliary form data form, err := c.MultipartForm() if err != nil { - Log("multipart error %s", err.Error()) - return "", err + return JsonStatus(c, fiber.StatusForbidden, + "mime/multipart error "+err.Error()) } // init upload obj @@ -62,14 +62,15 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) { files := form.File["upload[]"] members, err := SaveFormFiles(c, cfg, files, id) if err != nil { - return "", fiber.NewError(500, "Could not store uploaded file[s]!") + return JsonStatus(c, fiber.StatusInternalServerError, + "Could not store uploaded file[s]: "+err.Error()) } entry.Members = members // extract auxilliary form data (expire field et al) if err := c.BodyParser(&formdata); err != nil { - Log("bodyparser error %s", err.Error()) - return "", err + return JsonStatus(c, fiber.StatusInternalServerError, + "bodyparser error : "+err.Error()) } // post process expire @@ -78,7 +79,8 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) { } else { ex, err := Untaint(formdata.Expire, cfg.RegDuration) // duration or asap allowed if err != nil { - return "", err + return JsonStatus(c, fiber.StatusForbidden, + "Invalid data: "+err.Error()) } entry.Expire = ex } @@ -86,7 +88,8 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) { // get url [and zip if there are multiple files] returnUrl, Newfilename, err := ProcessFormFiles(cfg, entry.Members, id) if err != nil { - return "", fiber.NewError(500, "Could not process uploaded file[s]!") + return JsonStatus(c, fiber.StatusInternalServerError, + "Could not process uploaded file[s]: "+err.Error()) } entry.File = Newfilename @@ -96,7 +99,8 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) { // auth.validateAPIKey()) sess, err := Sessionstore.Get(c) if err != nil { - return "", fiber.NewError(500, "Unable to initialize session store from context!") + return JsonStatus(c, fiber.StatusInternalServerError, + "Unable to initialize session store from context: "+err.Error()) } apicontext := sess.Get("apicontext") if apicontext != nil { @@ -110,7 +114,12 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) { // we do this in the background to not thwart the server go db.Insert(id, entry) - return returnUrl, nil + // everything went well so far + res := &Uploads{Entries: []*Upload{entry}} + res.Success = true + res.Message = "Download url: " + returnUrl + res.Code = fiber.StatusOK + return c.Status(fiber.StatusOK).JSON(res) } func FileGet(c *fiber.Ctx, cfg *cfg.Config, db *Db, shallExpire ...bool) error { diff --git a/upd/api/server.go b/upd/api/server.go index b6194c7..69994f3 100644 --- a/upd/api/server.go +++ b/upd/api/server.go @@ -56,8 +56,7 @@ func Runserver(conf *cfg.Config, args []string) error { { // authenticated routes api.Post("/file/", auth, func(c *fiber.Ctx) error { - msg, err := FilePut(c, conf, db) - return SendResponse(c, msg, err) + return FilePut(c, conf, db) }) api.Get("/file/:id/:file", auth, func(c *fiber.Ctx) error {