From 66851587302ff76767c1b50241f9a9ad0b724ca0 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 5 Oct 2023 15:29:22 +0200 Subject: [PATCH] feat: support for long inputs and post requests --- pages/api.go | 8 ++--- pages/index.go | 86 +++++++++++++++++++++++++----------------------- serve/serve.go | 2 ++ views/index.html | 4 +-- 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/pages/api.go b/pages/api.go index 6069515..bdb6523 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(c.Query("engine"), "alpha") - from := utils.Sanitize(c.Query("from"), "alpha") - to := utils.Sanitize(c.Query("to"), "alpha") - text := c.Query("text") + 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") 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 b1066e5..1312115 100644 --- a/pages/index.go +++ b/pages/index.go @@ -1,9 +1,11 @@ package pages import ( + "os" + "slices" + "codeberg.org/aryak/libmozhi" "github.com/gofiber/fiber/v2" - "os" ) func envTrueNoExist(env string) bool { @@ -14,27 +16,26 @@ func envTrueNoExist(env string) bool { } func engineList() map[string]string { - engines := map[string]string{"all":"All Engines", "google":"Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "watson": "Watson", "yandex": "Yandex"} + engines := map[string]string{"all": "All Engines", "google": "Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "watson": "Watson", "yandex": "Yandex"} if envTrueNoExist("MOZHI_GOOGLE_ENABLED") == false { - delete(engines,"google") + delete(engines, "google") } else if envTrueNoExist("MOZHI_DEEPL_ENABLED") == false { - delete(engines,"deepl") + delete(engines, "deepl") } else if envTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") == false { - delete(engines,"duckduckgo") + delete(engines, "duckduckgo") } else if envTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") == false || envTrueNoExist("MOZHI_LIBRETRANSLATE_URL") { - delete(engines,"libre") + delete(engines, "libre") } else if envTrueNoExist("MOZHI_MYMEMORY_ENABLED") == false { - delete(engines,"mymemory") + delete(engines, "mymemory") } else if envTrueNoExist("MOZHI_REVERSO_ENABLED") == false { - delete(engines,"reverso") + delete(engines, "reverso") } else if envTrueNoExist("MOZHI_WATSON_ENABLED") == false { - delete(engines,"watson") + delete(engines, "watson") } else if envTrueNoExist("MOZHI_YANDEX_ENABLED") == false { - delete(engines,"yandex") + delete(engines, "yandex") } return engines -} - +} // DeduplicateLists deduplicates a slice of List based on the Id field func deDuplicateLists(input []libmozhi.List) []libmozhi.List { @@ -67,22 +68,24 @@ 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 engine string - var originalText string - if c.Query("engine") == "" { - engine = "google" + var enginesAsArray []string + for engine := range engines { + enginesAsArray = append(enginesAsArray, engine) } - if c.Query("engine") != "" { - for key, _ := range engines { - if c.Query("engine") == key { - engine = c.Query("engine") - } - } - if engine == "" { - engine = "google" - } + + var engine = getQueryOrFormValue(c, "engine") + if engine == "" || !slices.Contains(enginesAsArray, engine) { + engine = "google" } var sourceLanguages []libmozhi.List @@ -94,9 +97,10 @@ func HandleIndex(c *fiber.Ctx) error { targetLanguages, _ = libmozhi.LangList(engine, "tl") } - originalText = c.Query("text") - to := c.Query("to") - from := c.Query("from") + originalText := getQueryOrFormValue(c, "text") + to := getQueryOrFormValue(c, "to") + from := getQueryOrFormValue(c, "from") + var translation libmozhi.LangOut var translationExists bool var transall []libmozhi.LangOut @@ -126,18 +130,18 @@ func HandleIndex(c *fiber.Ctx) error { translationExists = false } return c.Render("index", fiber.Map{ - "Engine": engine, - "enginesNames": engines, - "SourceLanguages": sourceLanguages, - "TargetLanguages": targetLanguages, - "OriginalText": originalText, - "Translation": translation, - "TranslationExists": translationExists, - "TranslateAll": transall, - "From": from, - "To": to, - "TtsFrom": ttsFrom, - "TtsTo": ttsTo, - "defaultLang": "en", + "Engine": engine, + "enginesNames": engines, + "SourceLanguages": sourceLanguages, + "TargetLanguages": targetLanguages, + "OriginalText": originalText, + "Translation": translation, + "TranslationExists": translationExists, + "TranslateAll": transall, + "From": from, + "To": to, + "TtsFrom": ttsFrom, + "TtsTo": ttsTo, + "defaultLang": "en", }) } diff --git a/serve/serve.go b/serve/serve.go index 9303271..923ec73 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -85,6 +85,7 @@ func Serve(port string) { api := app.Group("/api") api.Get("/translate", pages.HandleTranslate) + api.Post("/translate", pages.HandleTranslate) api.Get("/source_languages", pages.HandleSourceLanguages) api.Get("/target_languages", pages.HandleTargetLanguages) api.Get("/tts", pages.HandleTTS) @@ -97,6 +98,7 @@ func Serve(port string) { api.Get("/swagger/*", swagger.HandlerDefault) // default app.Get("/", pages.HandleIndex) + app.Post("/", pages.HandleIndex) app.Get("/about", pages.HandleAbout) app.Get("/switchlanguages", func(c *fiber.Ctx) error { engine := c.Query("engine") diff --git a/views/index.html b/views/index.html index 0814f93..e94c351 100644 --- a/views/index.html +++ b/views/index.html @@ -14,8 +14,8 @@

-
- + +