mirror of
https://codeberg.org/aryak/mozhi
synced 2024-12-22 18:59:45 +05:30
Merge pull request 'feat: support for long inputs and post requests' (#7) from Bnyro/mozhi:fix-long-inputs into master
Reviewed-on: https://codeberg.org/aryak/mozhi/pulls/7
This commit is contained in:
commit
3f7ba6b071
@ -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(c.Query("engine"), "alpha")
|
engine := utils.Sanitize(getQueryOrFormValue(c, "engine"), "alpha")
|
||||||
from := utils.Sanitize(c.Query("from"), "alpha")
|
from := utils.Sanitize(getQueryOrFormValue(c, "from"), "alpha")
|
||||||
to := utils.Sanitize(c.Query("to"), "alpha")
|
to := utils.Sanitize(getQueryOrFormValue(c, "to"), "alpha")
|
||||||
text := c.Query("text")
|
text := 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.")
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package pages
|
package pages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"codeberg.org/aryak/libmozhi"
|
"codeberg.org/aryak/libmozhi"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func envTrueNoExist(env string) bool {
|
func envTrueNoExist(env string) bool {
|
||||||
@ -14,27 +16,26 @@ func envTrueNoExist(env string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func engineList() map[string]string {
|
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 {
|
if envTrueNoExist("MOZHI_GOOGLE_ENABLED") == false {
|
||||||
delete(engines,"google")
|
delete(engines, "google")
|
||||||
} else if envTrueNoExist("MOZHI_DEEPL_ENABLED") == false {
|
} else if envTrueNoExist("MOZHI_DEEPL_ENABLED") == false {
|
||||||
delete(engines,"deepl")
|
delete(engines, "deepl")
|
||||||
} else if envTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") == false {
|
} else if envTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") == false {
|
||||||
delete(engines,"duckduckgo")
|
delete(engines, "duckduckgo")
|
||||||
} else if envTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") == false || envTrueNoExist("MOZHI_LIBRETRANSLATE_URL") {
|
} else if envTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") == false || envTrueNoExist("MOZHI_LIBRETRANSLATE_URL") {
|
||||||
delete(engines,"libre")
|
delete(engines, "libre")
|
||||||
} else if envTrueNoExist("MOZHI_MYMEMORY_ENABLED") == false {
|
} else if envTrueNoExist("MOZHI_MYMEMORY_ENABLED") == false {
|
||||||
delete(engines,"mymemory")
|
delete(engines, "mymemory")
|
||||||
} else if envTrueNoExist("MOZHI_REVERSO_ENABLED") == false {
|
} else if envTrueNoExist("MOZHI_REVERSO_ENABLED") == false {
|
||||||
delete(engines,"reverso")
|
delete(engines, "reverso")
|
||||||
} else if envTrueNoExist("MOZHI_WATSON_ENABLED") == false {
|
} else if envTrueNoExist("MOZHI_WATSON_ENABLED") == false {
|
||||||
delete(engines,"watson")
|
delete(engines, "watson")
|
||||||
} else if envTrueNoExist("MOZHI_YANDEX_ENABLED") == false {
|
} else if envTrueNoExist("MOZHI_YANDEX_ENABLED") == false {
|
||||||
delete(engines,"yandex")
|
delete(engines, "yandex")
|
||||||
}
|
}
|
||||||
return engines
|
return engines
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// DeduplicateLists deduplicates a slice of List based on the Id field
|
// DeduplicateLists deduplicates a slice of List based on the Id field
|
||||||
func deDuplicateLists(input []libmozhi.List) []libmozhi.List {
|
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)
|
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 engine string
|
var enginesAsArray []string
|
||||||
var originalText string
|
for engine := range engines {
|
||||||
if c.Query("engine") == "" {
|
enginesAsArray = append(enginesAsArray, engine)
|
||||||
engine = "google"
|
|
||||||
}
|
}
|
||||||
if c.Query("engine") != "" {
|
|
||||||
for key, _ := range engines {
|
var engine = getQueryOrFormValue(c, "engine")
|
||||||
if c.Query("engine") == key {
|
if engine == "" || !slices.Contains(enginesAsArray, engine) {
|
||||||
engine = c.Query("engine")
|
engine = "google"
|
||||||
}
|
|
||||||
}
|
|
||||||
if engine == "" {
|
|
||||||
engine = "google"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var sourceLanguages []libmozhi.List
|
var sourceLanguages []libmozhi.List
|
||||||
@ -94,9 +97,10 @@ func HandleIndex(c *fiber.Ctx) error {
|
|||||||
targetLanguages, _ = libmozhi.LangList(engine, "tl")
|
targetLanguages, _ = libmozhi.LangList(engine, "tl")
|
||||||
}
|
}
|
||||||
|
|
||||||
originalText = c.Query("text")
|
originalText := getQueryOrFormValue(c, "text")
|
||||||
to := c.Query("to")
|
to := getQueryOrFormValue(c, "to")
|
||||||
from := c.Query("from")
|
from := getQueryOrFormValue(c, "from")
|
||||||
|
|
||||||
var translation libmozhi.LangOut
|
var translation libmozhi.LangOut
|
||||||
var translationExists bool
|
var translationExists bool
|
||||||
var transall []libmozhi.LangOut
|
var transall []libmozhi.LangOut
|
||||||
@ -126,18 +130,18 @@ func HandleIndex(c *fiber.Ctx) error {
|
|||||||
translationExists = false
|
translationExists = false
|
||||||
}
|
}
|
||||||
return c.Render("index", fiber.Map{
|
return c.Render("index", fiber.Map{
|
||||||
"Engine": engine,
|
"Engine": engine,
|
||||||
"enginesNames": engines,
|
"enginesNames": engines,
|
||||||
"SourceLanguages": sourceLanguages,
|
"SourceLanguages": sourceLanguages,
|
||||||
"TargetLanguages": targetLanguages,
|
"TargetLanguages": targetLanguages,
|
||||||
"OriginalText": originalText,
|
"OriginalText": originalText,
|
||||||
"Translation": translation,
|
"Translation": translation,
|
||||||
"TranslationExists": translationExists,
|
"TranslationExists": translationExists,
|
||||||
"TranslateAll": transall,
|
"TranslateAll": transall,
|
||||||
"From": from,
|
"From": from,
|
||||||
"To": to,
|
"To": to,
|
||||||
"TtsFrom": ttsFrom,
|
"TtsFrom": ttsFrom,
|
||||||
"TtsTo": ttsTo,
|
"TtsTo": ttsTo,
|
||||||
"defaultLang": "en",
|
"defaultLang": "en",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -85,6 +85,7 @@ func Serve(port string) {
|
|||||||
|
|
||||||
api := app.Group("/api")
|
api := app.Group("/api")
|
||||||
api.Get("/translate", pages.HandleTranslate)
|
api.Get("/translate", pages.HandleTranslate)
|
||||||
|
api.Post("/translate", pages.HandleTranslate)
|
||||||
api.Get("/source_languages", pages.HandleSourceLanguages)
|
api.Get("/source_languages", pages.HandleSourceLanguages)
|
||||||
api.Get("/target_languages", pages.HandleTargetLanguages)
|
api.Get("/target_languages", pages.HandleTargetLanguages)
|
||||||
api.Get("/tts", pages.HandleTTS)
|
api.Get("/tts", pages.HandleTTS)
|
||||||
@ -97,6 +98,7 @@ func Serve(port string) {
|
|||||||
api.Get("/swagger/*", swagger.HandlerDefault) // default
|
api.Get("/swagger/*", swagger.HandlerDefault) // default
|
||||||
|
|
||||||
app.Get("/", pages.HandleIndex)
|
app.Get("/", pages.HandleIndex)
|
||||||
|
app.Post("/", pages.HandleIndex)
|
||||||
app.Get("/about", pages.HandleAbout)
|
app.Get("/about", pages.HandleAbout)
|
||||||
app.Get("/switchlanguages", func(c *fiber.Ctx) error {
|
app.Get("/switchlanguages", func(c *fiber.Ctx) error {
|
||||||
engine := c.Query("engine")
|
engine := c.Query("engine")
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<br /><br />
|
<br /><br />
|
||||||
<form action="/" method="GET" id="translation-form">
|
<form action="/" method="POST" id="translation-form">
|
||||||
<!-- This hidden input is so that the engine gets sent in the GET request even though its not declared here -->
|
<!-- This hidden input is so that the engine gets sent in the request even though its not declared here -->
|
||||||
<input name="engine" value="{{.Engine}}" type="hidden" />
|
<input name="engine" value="{{.Engine}}" type="hidden" />
|
||||||
|
|
||||||
<div class="wrap languages">
|
<div class="wrap languages">
|
||||||
|
Loading…
Reference in New Issue
Block a user