Files
ephemerup/api/form_handlers.go

222 lines
6.6 KiB
Go
Raw Normal View History

2023-02-20 20:25:38 +01:00
/*
Copyright © 2023 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2023-02-24 12:16:03 +01:00
package api
2023-02-20 20:25:38 +01:00
import (
//"github.com/alecthomas/repr"
2023-02-24 12:16:03 +01:00
"github.com/gofiber/fiber/v2"
2023-02-20 20:25:38 +01:00
"github.com/google/uuid"
2023-03-17 19:30:03 +01:00
"github.com/tlinden/cenophane/cfg"
"github.com/tlinden/cenophane/common"
2023-02-28 19:05:09 +01:00
2023-03-26 20:19:31 +02:00
"bytes"
"html/template"
"strings"
2023-02-20 20:25:38 +01:00
"time"
)
func FormCreate(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
2023-02-20 20:25:38 +01:00
id := uuid.NewString()
var formdata common.Form
2023-02-20 20:25:38 +01:00
// init form obj
entry := &common.Form{Id: id, Created: common.Timestamp{Time: time.Now()}}
2023-02-20 20:25:38 +01:00
2023-03-16 15:04:03 +01:00
// retrieve the API Context name from the session
apicontext, err := SessionGetApicontext(c)
2023-03-16 15:04:03 +01:00
if err != nil {
return JsonStatus(c, fiber.StatusInternalServerError,
"Unable to initialize session store from context: "+err.Error())
}
entry.Context = apicontext
// extract auxilliary form data (expire field et al)
2023-02-24 12:16:03 +01:00
if err := c.BodyParser(&formdata); err != nil {
2023-03-13 18:48:38 +01:00
return JsonStatus(c, fiber.StatusInternalServerError,
"bodyparser error : "+err.Error())
}
2023-02-24 12:16:03 +01:00
// post process expire
if len(formdata.Expire) == 0 {
entry.Expire = "asap"
} else {
ex, err := common.Untaint(formdata.Expire, cfg.RegDuration) // duration or asap allowed
if err != nil {
2023-03-13 18:48:38 +01:00
return JsonStatus(c, fiber.StatusForbidden,
"Invalid data: "+err.Error())
}
entry.Expire = ex
}
// get url [and zip if there are multiple files]
returnUrl := strings.Join([]string{cfg.Url, "form", id}, "/")
2023-03-23 18:44:14 +01:00
entry.Url = returnUrl
2023-02-20 20:25:38 +01:00
Log("Now serving %s", returnUrl)
2023-02-20 20:25:38 +01:00
Log("Expire set to: %s", entry.Expire)
Log("Form created with API-Context %s", entry.Context)
2023-02-20 20:25:38 +01:00
2023-02-28 19:05:09 +01:00
// we do this in the background to not thwart the server
go db.Insert(id, entry)
2023-02-20 20:25:38 +01:00
2023-03-13 18:48:38 +01:00
// everything went well so far
res := &common.Response{Forms: []*common.Form{entry}}
2023-03-13 18:48:38 +01:00
res.Success = true
res.Code = fiber.StatusOK
return c.Status(fiber.StatusOK).JSON(res)
2023-02-28 19:05:09 +01:00
}
2023-02-20 20:25:38 +01:00
2023-03-26 20:19:31 +02:00
// delete form
func FormDelete(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
id, err := common.Untaint(c.Params("id"), cfg.RegKey)
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Invalid id provided!")
}
2023-02-28 19:05:09 +01:00
if len(id) == 0 {
return JsonStatus(c, fiber.StatusForbidden,
"No id specified!")
2023-02-28 19:05:09 +01:00
}
2023-03-16 15:04:03 +01:00
// retrieve the API Context name from the session
apicontext, err := SessionGetApicontext(c)
2023-03-16 15:04:03 +01:00
if err != nil {
return JsonStatus(c, fiber.StatusInternalServerError,
"Unable to initialize session store from context: "+err.Error())
}
2023-02-28 19:05:09 +01:00
2023-03-16 15:04:03 +01:00
err = db.Delete(apicontext, id)
2023-02-28 19:05:09 +01:00
if err != nil {
// non existent db entry with that id, or other db error, see logs
return JsonStatus(c, fiber.StatusForbidden,
2023-03-26 20:19:31 +02:00
"No form with that id could be found!")
2023-02-28 19:05:09 +01:00
}
return nil
2023-02-20 20:25:38 +01:00
}
// returns the whole list + error code, no post processing by server
func FormsList(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
2023-03-16 16:35:55 +01:00
// fetch filter from body(json expected)
setcontext := new(SetContext)
if err := c.BodyParser(setcontext); err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Unable to parse body: "+err.Error())
}
filter, err := common.Untaint(setcontext.Apicontext, cfg.RegKey)
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
2023-03-16 16:35:55 +01:00
"Invalid api context filter provided!")
}
// retrieve the API Context name from the session
apicontext, err := SessionGetApicontext(c)
2023-03-16 16:35:55 +01:00
if err != nil {
return JsonStatus(c, fiber.StatusInternalServerError,
"Unable to initialize session store from context: "+err.Error())
}
2023-03-16 16:35:55 +01:00
// get list
2023-03-26 20:19:31 +02:00
response, err := db.List(apicontext, filter, common.TypeForm)
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
2023-03-26 20:19:31 +02:00
"Unable to list forms: "+err.Error())
}
// if we reached this point we can signal success
2023-03-26 20:19:31 +02:00
response.Success = true
response.Code = fiber.StatusOK
2023-03-26 20:19:31 +02:00
return c.Status(fiber.StatusOK).JSON(response)
}
2023-03-26 20:19:31 +02:00
// returns just one form obj + error code
func FormDescribe(c *fiber.Ctx, cfg *cfg.Config, db *Db) error {
id, err := common.Untaint(c.Params("id"), cfg.RegKey)
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Invalid id provided!")
}
2023-03-16 15:04:03 +01:00
// retrieve the API Context name from the session
apicontext, err := SessionGetApicontext(c)
2023-03-16 15:04:03 +01:00
if err != nil {
return JsonStatus(c, fiber.StatusInternalServerError,
"Unable to initialize session store from context: "+err.Error())
}
2023-03-26 20:19:31 +02:00
response, err := db.Get(apicontext, id, common.TypeForm)
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
2023-03-26 20:19:31 +02:00
"No form with that id could be found!")
}
2023-03-26 20:19:31 +02:00
for _, form := range response.Forms {
form.Url = strings.Join([]string{cfg.Url, "form", id}, "/")
}
// if we reached this point we can signal success
2023-03-26 20:19:31 +02:00
response.Success = true
response.Code = fiber.StatusOK
2023-03-26 20:19:31 +02:00
return c.Status(fiber.StatusOK).JSON(response)
}
2023-03-26 20:19:31 +02:00
/*
Render the upload html form. Template given by --formpage, stored
as text in cfg.Formpage. It will be rendered using golang's
template engine, data to be filled in is the form matching the
given id.
*/
2023-03-26 20:19:31 +02:00
func FormPage(c *fiber.Ctx, cfg *cfg.Config, db *Db, shallexpire bool) error {
id, err := common.Untaint(c.Params("id"), cfg.RegKey)
if err != nil {
return c.Status(fiber.StatusForbidden).SendString("Invalid id provided!")
}
apicontext, err := SessionGetApicontext(c)
2023-03-26 20:19:31 +02:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Unable to initialize session store from context:" + err.Error())
}
response, err := db.Get(apicontext, id, common.TypeForm)
if err != nil || len(response.Forms) == 0 {
return c.Status(fiber.StatusForbidden).SendString("No form with that id could be found!")
}
t := template.New("form")
if t, err = t.Parse(cfg.Formpage); err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Unable to load form template: " + err.Error())
}
// prepare upload url
uploadurl := strings.Join([]string{cfg.ApiPrefix + ApiVersion, "uploads"}, "/")
response.Forms[0].Url = uploadurl
2023-03-26 20:19:31 +02:00
var out bytes.Buffer
if err := t.Execute(&out, response.Forms[0]); err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Unable to render form template: " + err.Error())
}
c.Set("Content-type", "text/html; charset=utf-8")
return c.Status(fiber.StatusOK).SendString(out.String())
}