make switchlanguage work with POST
All checks were successful
mozhi pipeline / Push Docker image to Codeberg docker registry (push) Successful in 15m6s
mozhi pipeline / Build and publish artifacts (push) Successful in 24m43s

This commit is contained in:
2023-10-11 15:57:28 +05:30
parent d4d45832c8
commit 99a4eca07b
4 changed files with 28 additions and 23 deletions

View File

@@ -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.")
}

View File

@@ -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