mirror of
https://codeberg.org/scip/ephemerup.git
synced 2025-12-17 04:30:57 +01:00
enhanced output
This commit is contained in:
@@ -4,11 +4,12 @@ Simple standalone file upload server with api and cli
|
|||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
- also serve a html upload page
|
- 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
|
- 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()
|
- 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
|
- 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
|
## BUGS
|
||||||
|
|
||||||
|
|||||||
@@ -145,17 +145,15 @@ func UploadFiles(c *cfg.Config, args []string) error {
|
|||||||
}, 10*time.Millisecond).
|
}, 10*time.Millisecond).
|
||||||
Post(rq.Url)
|
Post(rq.Url)
|
||||||
|
|
||||||
fmt.Println("")
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return HandleResponse(c, resp)
|
return printUploadsResponse(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleResponse(c *cfg.Config, resp *req.Response) error {
|
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{}
|
r := Response{}
|
||||||
|
|
||||||
if err := json.Unmarshal([]byte(resp.String()), &r); err != nil {
|
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 err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return printUploadsResponse(resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func printUploadsResponse(resp *req.Response) error {
|
||||||
uploads := Uploads{}
|
uploads := Uploads{}
|
||||||
|
|
||||||
if err := json.Unmarshal([]byte(resp.String()), &uploads); err != nil {
|
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)
|
return errors.New(uploads.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if uploads.Message != "" {
|
||||||
|
fmt.Println(uploads.Message)
|
||||||
|
}
|
||||||
|
|
||||||
WriteExtended(&uploads)
|
WriteExtended(&uploads)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import (
|
|||||||
"time"
|
"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:
|
// supports upload of multiple files with:
|
||||||
//
|
//
|
||||||
// curl -X POST localhost:8080/putfile \
|
// 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
|
// fetch auxiliary form data
|
||||||
form, err := c.MultipartForm()
|
form, err := c.MultipartForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log("multipart error %s", err.Error())
|
return JsonStatus(c, fiber.StatusForbidden,
|
||||||
return "", err
|
"mime/multipart error "+err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// init upload obj
|
// init upload obj
|
||||||
@@ -62,14 +62,15 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) {
|
|||||||
files := form.File["upload[]"]
|
files := form.File["upload[]"]
|
||||||
members, err := SaveFormFiles(c, cfg, files, id)
|
members, err := SaveFormFiles(c, cfg, files, id)
|
||||||
if err != nil {
|
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
|
entry.Members = members
|
||||||
|
|
||||||
// extract auxilliary form data (expire field et al)
|
// extract auxilliary form data (expire field et al)
|
||||||
if err := c.BodyParser(&formdata); err != nil {
|
if err := c.BodyParser(&formdata); err != nil {
|
||||||
Log("bodyparser error %s", err.Error())
|
return JsonStatus(c, fiber.StatusInternalServerError,
|
||||||
return "", err
|
"bodyparser error : "+err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// post process expire
|
// post process expire
|
||||||
@@ -78,7 +79,8 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) {
|
|||||||
} else {
|
} else {
|
||||||
ex, err := Untaint(formdata.Expire, cfg.RegDuration) // duration or asap allowed
|
ex, err := Untaint(formdata.Expire, cfg.RegDuration) // duration or asap allowed
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return JsonStatus(c, fiber.StatusForbidden,
|
||||||
|
"Invalid data: "+err.Error())
|
||||||
}
|
}
|
||||||
entry.Expire = ex
|
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]
|
// get url [and zip if there are multiple files]
|
||||||
returnUrl, Newfilename, err := ProcessFormFiles(cfg, entry.Members, id)
|
returnUrl, Newfilename, err := ProcessFormFiles(cfg, entry.Members, id)
|
||||||
if err != nil {
|
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
|
entry.File = Newfilename
|
||||||
|
|
||||||
@@ -96,7 +99,8 @@ func FilePut(c *fiber.Ctx, cfg *cfg.Config, db *Db) (string, error) {
|
|||||||
// auth.validateAPIKey())
|
// auth.validateAPIKey())
|
||||||
sess, err := Sessionstore.Get(c)
|
sess, err := Sessionstore.Get(c)
|
||||||
if err != nil {
|
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")
|
apicontext := sess.Get("apicontext")
|
||||||
if apicontext != nil {
|
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
|
// we do this in the background to not thwart the server
|
||||||
go db.Insert(id, entry)
|
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 {
|
func FileGet(c *fiber.Ctx, cfg *cfg.Config, db *Db, shallExpire ...bool) error {
|
||||||
|
|||||||
@@ -56,8 +56,7 @@ func Runserver(conf *cfg.Config, args []string) error {
|
|||||||
{
|
{
|
||||||
// authenticated routes
|
// authenticated routes
|
||||||
api.Post("/file/", auth, func(c *fiber.Ctx) error {
|
api.Post("/file/", auth, func(c *fiber.Ctx) error {
|
||||||
msg, err := FilePut(c, conf, db)
|
return FilePut(c, conf, db)
|
||||||
return SendResponse(c, msg, err)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
api.Get("/file/:id/:file", auth, func(c *fiber.Ctx) error {
|
api.Get("/file/:id/:file", auth, func(c *fiber.Ctx) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user