2023-07-06 16:24:45 +05:30
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2024-05-22 11:48:54 +05:30
|
|
|
"encoding/base64"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2023-09-11 07:45:52 +05:30
|
|
|
"codeberg.org/aryak/libmozhi"
|
2023-09-10 18:04:08 +05:30
|
|
|
"codeberg.org/aryak/mozhi/utils"
|
2023-07-06 16:24:45 +05:30
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2023-08-27 21:27:46 +05:30
|
|
|
// HandleSourceLanguages godoc
|
2023-08-29 14:50:56 +05:30
|
|
|
//
|
2023-08-27 21:27:46 +05:30
|
|
|
// @Summary Show list of available source languages for engine
|
|
|
|
// @Param engine query string true "Engine name"
|
2023-09-09 20:03:14 +05:30
|
|
|
// @Success 200 {object} libmozhi.List
|
2023-08-27 21:29:14 +05:30
|
|
|
// @Router /api/source_languages [get]
|
2023-07-06 16:24:45 +05:30
|
|
|
func HandleSourceLanguages(c *fiber.Ctx) error {
|
2023-08-15 21:41:02 +05:30
|
|
|
engine := utils.Sanitize(c.Query("engine"), "alpha")
|
2023-07-06 16:24:45 +05:30
|
|
|
if engine == "" {
|
2023-08-27 19:48:56 +05:30
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
|
2023-07-06 16:24:45 +05:30
|
|
|
}
|
2023-09-09 20:03:14 +05:30
|
|
|
data, err := libmozhi.LangList(engine, "sl")
|
2023-08-27 19:48:56 +05:30
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2023-07-22 19:52:38 +05:30
|
|
|
}
|
2023-07-06 16:24:45 +05:30
|
|
|
return c.JSON(data)
|
|
|
|
}
|
2023-08-27 19:54:30 +05:30
|
|
|
|
2023-08-27 21:27:46 +05:30
|
|
|
// HandleTargetLanguages godoc
|
2023-08-29 14:50:56 +05:30
|
|
|
//
|
2023-08-27 21:27:46 +05:30
|
|
|
// @Summary Show list of available target languages for engine
|
|
|
|
// @Param engine query string true "Engine name"
|
2023-09-09 20:03:14 +05:30
|
|
|
// @Success 200 {object} libmozhi.List
|
2023-08-27 21:29:14 +05:30
|
|
|
// @Router /api/target_languages [get]
|
2023-07-06 16:24:45 +05:30
|
|
|
func HandleTargetLanguages(c *fiber.Ctx) error {
|
2023-08-15 21:41:02 +05:30
|
|
|
engine := utils.Sanitize(c.Query("engine"), "alpha")
|
2023-07-06 16:24:45 +05:30
|
|
|
if engine == "" {
|
2023-08-27 19:48:56 +05:30
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
|
2023-07-06 16:24:45 +05:30
|
|
|
}
|
2023-09-09 20:03:14 +05:30
|
|
|
data, err := libmozhi.LangList(engine, "tl")
|
2023-08-27 19:48:56 +05:30
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2023-07-22 19:52:38 +05:30
|
|
|
}
|
2023-07-06 16:24:45 +05:30
|
|
|
return c.JSON(data)
|
|
|
|
}
|
2023-08-27 19:54:30 +05:30
|
|
|
|
2023-08-27 21:27:46 +05:30
|
|
|
// HandleTTS godoc
|
2023-08-29 14:50:56 +05:30
|
|
|
//
|
2023-08-27 21:27:46 +05:30
|
|
|
// @Summary Get Text-To-Speech for specified language using specified engine
|
2023-08-27 21:29:14 +05:30
|
|
|
// @Param engine query string true "Engine name"
|
|
|
|
// @Param lang query string true "Language being TTS'd"
|
|
|
|
// @Param text query string true "Text being TTS'd"
|
|
|
|
// @Router /api/tts [get]
|
2023-07-06 16:24:45 +05:30
|
|
|
func HandleTTS(c *fiber.Ctx) error {
|
2023-08-15 21:41:02 +05:30
|
|
|
engine := utils.Sanitize(c.Query("engine"), "alpha")
|
|
|
|
lang := utils.Sanitize(c.Query("lang"), "alpha")
|
2023-07-22 19:54:15 +05:30
|
|
|
text := c.Query("text")
|
2023-08-27 19:48:56 +05:30
|
|
|
if engine == "" || text == "" || lang == "" {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "engine, lang, text are required query strings.")
|
2023-07-06 16:24:45 +05:30
|
|
|
}
|
2023-09-09 20:03:14 +05:30
|
|
|
data, err := libmozhi.TTS(engine, lang, text)
|
2023-08-27 19:48:56 +05:30
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2023-07-06 16:24:45 +05:30
|
|
|
}
|
2023-07-22 19:54:15 +05:30
|
|
|
c.Set("Content-Type", "audio/mpeg")
|
2023-09-10 18:44:17 +05:30
|
|
|
return c.Status(fiber.StatusOK).Send(data)
|
2023-07-06 16:24:45 +05:30
|
|
|
}
|
2023-08-27 19:54:30 +05:30
|
|
|
|
2023-08-27 21:27:46 +05:30
|
|
|
// HandleTranslate godoc
|
2023-08-29 14:50:56 +05:30
|
|
|
//
|
2023-08-27 21:29:14 +05:30
|
|
|
// @Summary Translate text
|
2023-09-09 20:03:14 +05:30
|
|
|
// @Description When engine is set to all, it will return an array of libmozhi.LangOut.
|
2023-08-27 21:29:14 +05:30
|
|
|
// @Param engine query string true "Engine name"
|
|
|
|
// @Param from query string true "Source language"
|
|
|
|
// @Param to query string true "Target language"
|
|
|
|
// @Param text query string true "Text being translated"
|
2023-09-10 18:04:08 +05:30
|
|
|
// @Success 200 {object} libmozhi.LangOut
|
2023-08-27 21:29:14 +05:30
|
|
|
// @Router /api/translate [get]
|
2023-08-15 21:41:02 +05:30
|
|
|
func HandleTranslate(c *fiber.Ctx) error {
|
2023-10-11 15:57:28 +05:30
|
|
|
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")
|
2023-08-27 19:48:56 +05:30
|
|
|
if engine == "" || from == "" || to == "" || text == "" {
|
2023-08-15 21:41:02 +05:30
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
|
|
|
|
}
|
2023-08-27 19:48:56 +05:30
|
|
|
if engine == "all" {
|
2024-02-20 14:41:45 +05:30
|
|
|
dataarr := libmozhi.TranslateAll(to, from, text)
|
|
|
|
return c.JSON(dataarr)
|
2024-04-03 17:03:14 +05:30
|
|
|
} else if strings.Contains(engine, ",") == true {
|
|
|
|
engineArr := strings.Split(engine, ",")
|
|
|
|
dataarr, err := libmozhi.TranslateSome(engineArr, to, from, text)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
return c.JSON(dataarr)
|
2023-08-27 19:48:56 +05:30
|
|
|
} else {
|
2024-02-20 14:41:45 +05:30
|
|
|
data, err := libmozhi.Translate(engine, to, from, text)
|
2023-08-27 19:48:56 +05:30
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
2023-08-16 11:58:03 +05:30
|
|
|
return c.JSON(data)
|
|
|
|
}
|
2023-08-15 21:41:02 +05:30
|
|
|
}
|
2023-10-25 18:39:31 +05:30
|
|
|
|
2024-04-14 18:41:18 +05:30
|
|
|
// HandleImg godoc
|
|
|
|
//
|
|
|
|
// @Summary Translate an image
|
|
|
|
// @Description When engine is set to all, it will return an array of libmozhi.LangOut.
|
2024-05-22 11:48:54 +05:30
|
|
|
// @Param engine formData string true "Engine name"
|
|
|
|
// @Param from formData string true "Source language"
|
|
|
|
// @Param to formData string true "Target language"
|
|
|
|
// @Param image formData file true "PNG image"
|
2024-04-14 18:41:18 +05:30
|
|
|
// @Success 200 {object} libmozhi.ImgOut
|
|
|
|
// @Router /api/image [post]
|
|
|
|
func HandleImg(c *fiber.Ctx) error {
|
2024-05-22 11:48:54 +05:30
|
|
|
engine := utils.Sanitize(c.FormValue("engine"), "alpha")
|
|
|
|
from := utils.Sanitize(c.FormValue("from"), "alpha")
|
|
|
|
to := utils.Sanitize(c.FormValue("to"), "alpha")
|
|
|
|
file, err := c.FormFile("image")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-04-14 18:41:18 +05:30
|
|
|
}
|
2024-05-22 11:48:54 +05:30
|
|
|
if file.Header.Get("Content-Type") != "image/png" {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid image, only png images are accepted")
|
|
|
|
}
|
|
|
|
fileData, err := file.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
image, err := io.ReadAll(fileData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if engine == "" || from == "" || to == "" {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine are required arguments.")
|
2024-04-14 18:41:18 +05:30
|
|
|
}
|
2024-05-22 11:48:54 +05:30
|
|
|
imageEncoded := base64.StdEncoding.EncodeToString(image)
|
|
|
|
data, err := libmozhi.ImageGoogle(to, from, imageEncoded)
|
2024-04-14 18:41:18 +05:30
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
return c.JSON(data)
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:39:31 +05:30
|
|
|
// HandleEngines godoc
|
|
|
|
//
|
|
|
|
// @Summary List engines
|
|
|
|
// @Description Lists available Engines.
|
|
|
|
// @Success 200 {object} map[string]string
|
|
|
|
// @Router /api/engines [get]
|
|
|
|
func HandleEngines(c *fiber.Ctx) error {
|
|
|
|
engines := utils.EngineList()
|
|
|
|
serializedData := make(map[string]interface{}, len(engines))
|
|
|
|
for engineId, engineName := range engines {
|
|
|
|
serializedData[engineId] = engineName
|
|
|
|
}
|
|
|
|
return c.JSON(serializedData)
|
|
|
|
}
|