diff --git a/pages/api.go b/pages/api.go index 5906ab3..bbe4d8d 100644 --- a/pages/api.go +++ b/pages/api.go @@ -6,7 +6,7 @@ import ( ) func HandleSourceLanguages(c *fiber.Ctx) error { - engine := c.Query("engine") + engine := utils.Sanitize(c.Query("engine"), "alpha") if engine == "" { return c.SendStatus(fiber.StatusBadRequest) } @@ -29,7 +29,7 @@ func HandleSourceLanguages(c *fiber.Ctx) error { return c.JSON(data) } func HandleTargetLanguages(c *fiber.Ctx) error { - engine := c.Query("engine") + engine := utils.Sanitize(c.Query("engine"), "alpha") if engine == "" { return c.SendStatus(fiber.StatusBadRequest) } @@ -37,7 +37,7 @@ func HandleTargetLanguages(c *fiber.Ctx) error { if engine == "google" { data = utils.LangListGoogle("tl") } else if engine == "libre" { - data = utils.LangListLibreTranslate("sl") + data = utils.LangListLibreTranslate("tl") } else if engine == "reverso" { data = utils.LangListReverso("tl") } else if engine == "deepl" { @@ -52,8 +52,8 @@ func HandleTargetLanguages(c *fiber.Ctx) error { return c.JSON(data) } func HandleTTS(c *fiber.Ctx) error { - engine := c.Query("engine") - lang := c.Query("lang") + engine := utils.Sanitize(c.Query("engine"), "alpha") + lang := utils.Sanitize(c.Query("lang"), "alpha") text := c.Query("text") // Why does go not have an andor statement :( if engine == "" { @@ -72,3 +72,35 @@ func HandleTTS(c *fiber.Ctx) error { c.Set("Content-Type", "audio/mpeg") return c.Send([]byte(data)) } +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") + if engine == "" && from == "" && to == "" && text == ""{ + return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.") + } + var data utils.LangOut + var err error + if engine == "google" { + data, err = utils.TranslateGoogle(to, from, text) + } else if engine == "libre" { + data, err = utils.TranslateLibreTranslate(to, from, text) + } else if engine == "reverso" { + data, err = utils.TranslateReverso(to, from, text) + } else if engine == "deepl" { + data, err = utils.TranslateDeepl(to, from, text) + } else if engine == "watson" { + data, err = utils.TranslateWatson(to, from, text) + } else if engine == "yandex" { + data, err = utils.TranslateYandex(to, from, text) + } else if engine == "mymemory" { + data, err = utils.TranslateMyMemory(to, from, text) + } + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + data.SourceLang = from + data.TargetLang = to + return c.JSON(data) +} diff --git a/serve/serve.go b/serve/serve.go index 12a226d..5584991 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -5,7 +5,6 @@ import ( "log" "os" "runtime" - "strings" "codeberg.org/aryak/simplytranslate/pages" "codeberg.org/aryak/simplytranslate/utils" @@ -39,23 +38,6 @@ func Serve(port string) { EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.0.0.0/0"}, ProxyHeader: fiber.HeaderXForwardedFor, - ErrorHandler: func(ctx *fiber.Ctx, err error) error { - code := fiber.StatusInternalServerError - - if e, ok := err.(*fiber.Error); ok { - code = e.Code - } - link := strings.TrimPrefix(err.Error(), "Cannot GET ") - err = ctx.Status(code).Render("error", fiber.Map{ - "error": err, - "link": link, - }) - if err != nil { - return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error") - } - - return nil - }, }) // For debugging purposes @@ -97,7 +79,7 @@ func Serve(port string) { }) app.Get("/", pages.HandleIndex) - //app.Get("/api/translate", pages.HandleTranslateApi) + app.Get("/api/translate", pages.HandleTranslate) app.Get("/api/source_languages", pages.HandleSourceLanguages) app.Get("/api/target_languages", pages.HandleTargetLanguages) app.Get("/api/tts", pages.HandleTTS) diff --git a/utils/engines.go b/utils/engines.go index d3a0f36..6b4ad65 100644 --- a/utils/engines.go +++ b/utils/engines.go @@ -11,8 +11,10 @@ import ( ) type LangOut struct { - OutputText string - AutoDetect string + OutputText string `json:"translated-text"` + AutoDetect string `json:"detected"` + SourceLang string `json:"source_language"` + TargetLang string `json:"target_language"` } func TranslateGoogle(to string, from string, text string) (LangOut, error) { diff --git a/utils/langlist-reverso.go b/utils/langlist-reverso.go index 3950c62..333e58c 100644 --- a/utils/langlist-reverso.go +++ b/utils/langlist-reverso.go @@ -1,10 +1,5 @@ package utils -type List struct { - Name string - Id string -} - func LangListReverso(listType string) []List { // IDs got from original simplytranslate-web and trial and error. Usually first three letters of language. var ListData = []List{ diff --git a/utils/main.go b/utils/main.go new file mode 100644 index 0000000..4e4ce2a --- /dev/null +++ b/utils/main.go @@ -0,0 +1,6 @@ +package utils + +type List struct { + Name string + Id string +} diff --git a/utils/sanitize.go b/utils/sanitize.go new file mode 100644 index 0000000..59a74e4 --- /dev/null +++ b/utils/sanitize.go @@ -0,0 +1,14 @@ +package utils + +import "regexp" + +func Sanitize(str string, strip string) string { + nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`) + nonAlphaRegex := regexp.MustCompile(`[^a-zA-Z0-9]+`) + if strip == "alpha" { + return nonAlphaRegex.ReplaceAllString(str, "") + } else if strip == "alphanumeric" { + return nonAlphanumericRegex.ReplaceAllString(str, "") + } + return "" +}