mirror of
https://codeberg.org/aryak/mozhi
synced 2025-02-17 09:27:42 +05:30
make switchlanguage work with POST
This commit is contained in:
parent
d4d45832c8
commit
99a4eca07b
@ -75,10 +75,10 @@ func HandleTTS(c *fiber.Ctx) error {
|
|||||||
// @Success 200 {object} libmozhi.LangOut
|
// @Success 200 {object} libmozhi.LangOut
|
||||||
// @Router /api/translate [get]
|
// @Router /api/translate [get]
|
||||||
func HandleTranslate(c *fiber.Ctx) error {
|
func HandleTranslate(c *fiber.Ctx) error {
|
||||||
engine := utils.Sanitize(getQueryOrFormValue(c, "engine"), "alpha")
|
engine := utils.Sanitize(utils.GetQueryOrFormValue(c, "engine"), "alpha")
|
||||||
from := utils.Sanitize(getQueryOrFormValue(c, "from"), "alpha")
|
from := utils.Sanitize(utils.GetQueryOrFormValue(c, "from"), "alpha")
|
||||||
to := utils.Sanitize(getQueryOrFormValue(c, "to"), "alpha")
|
to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha")
|
||||||
text := getQueryOrFormValue(c, "text")
|
text := utils.GetQueryOrFormValue(c, "text")
|
||||||
if engine == "" || from == "" || to == "" || text == "" {
|
if engine == "" || from == "" || to == "" || text == "" {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
|
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"codeberg.org/aryak/libmozhi"
|
"codeberg.org/aryak/libmozhi"
|
||||||
|
"codeberg.org/aryak/mozhi/utils"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -68,14 +69,6 @@ func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List)
|
|||||||
return deDuplicateLists(sl), deDuplicateLists(tl)
|
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 {
|
func HandleIndex(c *fiber.Ctx) error {
|
||||||
engines := engineList()
|
engines := engineList()
|
||||||
var enginesAsArray []string
|
var enginesAsArray []string
|
||||||
@ -83,7 +76,7 @@ func HandleIndex(c *fiber.Ctx) error {
|
|||||||
enginesAsArray = append(enginesAsArray, engine)
|
enginesAsArray = append(enginesAsArray, engine)
|
||||||
}
|
}
|
||||||
|
|
||||||
var engine = getQueryOrFormValue(c, "engine")
|
var engine = utils.GetQueryOrFormValue(c, "engine")
|
||||||
if engine == "" || !slices.Contains(enginesAsArray, engine) {
|
if engine == "" || !slices.Contains(enginesAsArray, engine) {
|
||||||
engine = "google"
|
engine = "google"
|
||||||
}
|
}
|
||||||
@ -97,9 +90,9 @@ func HandleIndex(c *fiber.Ctx) error {
|
|||||||
targetLanguages, _ = libmozhi.LangList(engine, "tl")
|
targetLanguages, _ = libmozhi.LangList(engine, "tl")
|
||||||
}
|
}
|
||||||
|
|
||||||
originalText := getQueryOrFormValue(c, "text")
|
originalText := utils.GetQueryOrFormValue(c, "text")
|
||||||
to := getQueryOrFormValue(c, "to")
|
to := utils.GetQueryOrFormValue(c, "to")
|
||||||
from := getQueryOrFormValue(c, "from")
|
from := utils.GetQueryOrFormValue(c, "from")
|
||||||
|
|
||||||
var translation libmozhi.LangOut
|
var translation libmozhi.LangOut
|
||||||
var translationExists bool
|
var translationExists bool
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"codeberg.org/aryak/mozhi/pages"
|
"codeberg.org/aryak/mozhi/pages"
|
||||||
"codeberg.org/aryak/mozhi/public"
|
"codeberg.org/aryak/mozhi/public"
|
||||||
"codeberg.org/aryak/mozhi/views"
|
"codeberg.org/aryak/mozhi/views"
|
||||||
|
"codeberg.org/aryak/mozhi/utils"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/compress"
|
"github.com/gofiber/fiber/v2/middleware/compress"
|
||||||
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
||||||
@ -97,14 +98,14 @@ func Serve(port string) {
|
|||||||
api.Get("/swagger/*", swagger.HandlerDefault) // default
|
api.Get("/swagger/*", swagger.HandlerDefault) // default
|
||||||
|
|
||||||
app.All("/", pages.HandleIndex)
|
app.All("/", pages.HandleIndex)
|
||||||
app.Get("/about", pages.HandleAbout)
|
app.All("/switchlanguages", func(c *fiber.Ctx) error {
|
||||||
app.Get("/switchlanguages", func(c *fiber.Ctx) error {
|
engine := utils.Sanitize(utils.GetQueryOrFormValue(c, "engine"), "alpha")
|
||||||
engine := c.Query("engine")
|
from := utils.Sanitize(utils.GetQueryOrFormValue(c, "from"), "alpha")
|
||||||
from := c.Query("from")
|
to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha")
|
||||||
to := c.Query("to")
|
text := utils.Sanitize(utils.GetQueryOrFormValue(c, "text"), "alpha")
|
||||||
text := c.Query("text")
|
|
||||||
return c.Redirect("/?engine="+engine+"&from="+to+"&to="+from+"&text="+text+"&redirected=true", 301)
|
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{
|
app.Use("/", filesystem.New(filesystem.Config{
|
||||||
MaxAge: 2592000,
|
MaxAge: 2592000,
|
||||||
Root: http.FS(public.GetFiles()),
|
Root: http.FS(public.GetFiles()),
|
||||||
|
@ -1,6 +1,17 @@
|
|||||||
package utils
|
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 {
|
func Sanitize(str string, strip string) string {
|
||||||
nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`)
|
nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`)
|
Loading…
x
Reference in New Issue
Block a user