From 99a4eca07beac6eb2f6e5f7bf0e9ea804decd057 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Wed, 11 Oct 2023 15:57:28 +0530 Subject: [PATCH] make switchlanguage work with POST --- pages/api.go | 8 ++++---- pages/index.go | 17 +++++------------ serve/serve.go | 13 +++++++------ utils/{sanitize.go => main.go} | 13 ++++++++++++- 4 files changed, 28 insertions(+), 23 deletions(-) rename utils/{sanitize.go => main.go} (63%) diff --git a/pages/api.go b/pages/api.go index bdb6523..d14d82f 100644 --- a/pages/api.go +++ b/pages/api.go @@ -75,10 +75,10 @@ func HandleTTS(c *fiber.Ctx) error { // @Success 200 {object} libmozhi.LangOut // @Router /api/translate [get] func HandleTranslate(c *fiber.Ctx) error { - engine := utils.Sanitize(getQueryOrFormValue(c, "engine"), "alpha") - from := utils.Sanitize(getQueryOrFormValue(c, "from"), "alpha") - to := utils.Sanitize(getQueryOrFormValue(c, "to"), "alpha") - text := getQueryOrFormValue(c, "text") + engine := utils.Sanitize(utils.GetQueryOrFormValue(c, "engine"), "alpha") + from := utils.Sanitize(utils.GetQueryOrFormValue(c, "from"), "alpha") + to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha") + text := utils.GetQueryOrFormValue(c, "text") if engine == "" || from == "" || to == "" || text == "" { return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.") } diff --git a/pages/index.go b/pages/index.go index 1312115..755b21d 100644 --- a/pages/index.go +++ b/pages/index.go @@ -5,6 +5,7 @@ import ( "slices" "codeberg.org/aryak/libmozhi" + "codeberg.org/aryak/mozhi/utils" "github.com/gofiber/fiber/v2" ) @@ -68,14 +69,6 @@ func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) return deDuplicateLists(sl), deDuplicateLists(tl) } -func getQueryOrFormValue(c *fiber.Ctx, key string) string { - if c.Method() == "POST" { - return c.FormValue(key) - } else { - return c.Query(key) - } -} - func HandleIndex(c *fiber.Ctx) error { engines := engineList() var enginesAsArray []string @@ -83,7 +76,7 @@ func HandleIndex(c *fiber.Ctx) error { enginesAsArray = append(enginesAsArray, engine) } - var engine = getQueryOrFormValue(c, "engine") + var engine = utils.GetQueryOrFormValue(c, "engine") if engine == "" || !slices.Contains(enginesAsArray, engine) { engine = "google" } @@ -97,9 +90,9 @@ func HandleIndex(c *fiber.Ctx) error { targetLanguages, _ = libmozhi.LangList(engine, "tl") } - originalText := getQueryOrFormValue(c, "text") - to := getQueryOrFormValue(c, "to") - from := getQueryOrFormValue(c, "from") + originalText := utils.GetQueryOrFormValue(c, "text") + to := utils.GetQueryOrFormValue(c, "to") + from := utils.GetQueryOrFormValue(c, "from") var translation libmozhi.LangOut var translationExists bool diff --git a/serve/serve.go b/serve/serve.go index e7304f5..938337b 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -11,6 +11,7 @@ import ( "codeberg.org/aryak/mozhi/pages" "codeberg.org/aryak/mozhi/public" "codeberg.org/aryak/mozhi/views" + "codeberg.org/aryak/mozhi/utils" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/fiber/v2/middleware/filesystem" @@ -97,14 +98,14 @@ func Serve(port string) { api.Get("/swagger/*", swagger.HandlerDefault) // default app.All("/", pages.HandleIndex) - app.Get("/about", pages.HandleAbout) - app.Get("/switchlanguages", func(c *fiber.Ctx) error { - engine := c.Query("engine") - from := c.Query("from") - to := c.Query("to") - text := c.Query("text") + app.All("/switchlanguages", func(c *fiber.Ctx) error { + engine := utils.Sanitize(utils.GetQueryOrFormValue(c, "engine"), "alpha") + from := utils.Sanitize(utils.GetQueryOrFormValue(c, "from"), "alpha") + to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha") + text := utils.Sanitize(utils.GetQueryOrFormValue(c, "text"), "alpha") return c.Redirect("/?engine="+engine+"&from="+to+"&to="+from+"&text="+text+"&redirected=true", 301) }) + app.Get("/about", pages.HandleAbout) app.Use("/", filesystem.New(filesystem.Config{ MaxAge: 2592000, Root: http.FS(public.GetFiles()), diff --git a/utils/sanitize.go b/utils/main.go similarity index 63% rename from utils/sanitize.go rename to utils/main.go index 59a74e4..fe595f1 100644 --- a/utils/sanitize.go +++ b/utils/main.go @@ -1,6 +1,17 @@ package utils -import "regexp" +import ( + "regexp" + "github.com/gofiber/fiber/v2" +) + +func GetQueryOrFormValue(c *fiber.Ctx, key string) string { + if c.Method() == "POST" { + return c.FormValue(key) + } else { + return c.Query(key) + } +} func Sanitize(str string, strip string) string { nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`)