2023-07-06 10:54:45 +00:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2023-08-27 11:52:32 +00:00
|
|
|
"codeberg.org/aryak/mozhi/utils"
|
2023-07-06 10:54:45 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2023-08-27 15:57:46 +00:00
|
|
|
// HandleSourceLanguages godoc
|
2023-08-29 09:20:56 +00:00
|
|
|
//
|
2023-08-27 15:57:46 +00:00
|
|
|
// @Summary Show list of available source languages for engine
|
|
|
|
// @Param engine query string true "Engine name"
|
|
|
|
// @Success 200 {object} utils.List
|
2023-08-27 15:59:14 +00:00
|
|
|
// @Router /api/source_languages [get]
|
2023-07-06 10:54:45 +00:00
|
|
|
func HandleSourceLanguages(c *fiber.Ctx) error {
|
2023-08-15 16:11:02 +00:00
|
|
|
engine := utils.Sanitize(c.Query("engine"), "alpha")
|
2023-07-06 10:54:45 +00:00
|
|
|
if engine == "" {
|
2023-08-27 14:18:56 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
|
2023-07-06 10:54:45 +00:00
|
|
|
}
|
2023-08-27 14:18:56 +00:00
|
|
|
data, err := utils.LangList(engine, "sl")
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2023-07-22 14:22:38 +00:00
|
|
|
}
|
2023-07-06 10:54:45 +00:00
|
|
|
return c.JSON(data)
|
|
|
|
}
|
2023-08-27 14:24:30 +00:00
|
|
|
|
2023-08-27 15:57:46 +00:00
|
|
|
// HandleTargetLanguages godoc
|
2023-08-29 09:20:56 +00:00
|
|
|
//
|
2023-08-27 15:57:46 +00:00
|
|
|
// @Summary Show list of available target languages for engine
|
|
|
|
// @Param engine query string true "Engine name"
|
|
|
|
// @Success 200 {object} utils.List
|
2023-08-27 15:59:14 +00:00
|
|
|
// @Router /api/target_languages [get]
|
2023-07-06 10:54:45 +00:00
|
|
|
func HandleTargetLanguages(c *fiber.Ctx) error {
|
2023-08-15 16:11:02 +00:00
|
|
|
engine := utils.Sanitize(c.Query("engine"), "alpha")
|
2023-07-06 10:54:45 +00:00
|
|
|
if engine == "" {
|
2023-08-27 14:18:56 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "engine is a required query string.")
|
2023-07-06 10:54:45 +00:00
|
|
|
}
|
2023-08-27 14:18:56 +00:00
|
|
|
data, err := utils.LangList(engine, "tl")
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2023-07-22 14:22:38 +00:00
|
|
|
}
|
2023-07-06 10:54:45 +00:00
|
|
|
return c.JSON(data)
|
|
|
|
}
|
2023-08-27 14:24:30 +00:00
|
|
|
|
2023-08-27 15:57:46 +00:00
|
|
|
// HandleTTS godoc
|
2023-08-29 09:20:56 +00:00
|
|
|
//
|
2023-08-27 15:57:46 +00:00
|
|
|
// @Summary Get Text-To-Speech for specified language using specified engine
|
2023-08-27 15:59:14 +00:00
|
|
|
// @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 10:54:45 +00:00
|
|
|
func HandleTTS(c *fiber.Ctx) error {
|
2023-08-15 16:11:02 +00:00
|
|
|
engine := utils.Sanitize(c.Query("engine"), "alpha")
|
|
|
|
lang := utils.Sanitize(c.Query("lang"), "alpha")
|
2023-07-22 14:24:15 +00:00
|
|
|
text := c.Query("text")
|
2023-08-27 14:18:56 +00:00
|
|
|
if engine == "" || text == "" || lang == "" {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "engine, lang, text are required query strings.")
|
2023-07-06 10:54:45 +00:00
|
|
|
}
|
2023-08-27 14:18:56 +00:00
|
|
|
data, err := utils.TTS(engine, lang, text)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2023-07-06 10:54:45 +00:00
|
|
|
}
|
2023-07-22 14:24:15 +00:00
|
|
|
c.Set("Content-Type", "audio/mpeg")
|
2023-08-16 06:28:03 +00:00
|
|
|
return c.Send(data)
|
2023-07-06 10:54:45 +00:00
|
|
|
}
|
2023-08-27 14:24:30 +00:00
|
|
|
|
2023-08-27 15:57:46 +00:00
|
|
|
// HandleTranslate godoc
|
2023-08-29 09:20:56 +00:00
|
|
|
//
|
2023-08-27 15:59:14 +00:00
|
|
|
// @Summary Translate text
|
|
|
|
// @Description When engine is set to all, it will return an array of utils.LangOut.
|
|
|
|
// @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"
|
|
|
|
// @Success 200 {object} utils.LangOut
|
|
|
|
// @Router /api/translate [get]
|
2023-08-15 16:11:02 +00:00
|
|
|
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")
|
2023-08-27 14:18:56 +00:00
|
|
|
if engine == "" || from == "" || to == "" || text == "" {
|
2023-08-15 16:11:02 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
|
|
|
|
}
|
2023-08-16 06:28:03 +00:00
|
|
|
var dataarr []utils.LangOut
|
2023-08-27 14:24:30 +00:00
|
|
|
var data utils.LangOut
|
|
|
|
var err error
|
2023-08-27 14:18:56 +00:00
|
|
|
if engine == "all" {
|
2023-08-16 06:28:03 +00:00
|
|
|
dataarr = utils.TranslateAll(to, from, text)
|
2023-08-27 14:18:56 +00:00
|
|
|
} else {
|
|
|
|
data, err = utils.Translate(engine, to, from, text)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
2023-08-15 16:11:02 +00:00
|
|
|
}
|
2023-08-16 06:28:03 +00:00
|
|
|
if engine == "all" {
|
|
|
|
return c.JSON(dataarr)
|
|
|
|
} else {
|
|
|
|
return c.JSON(data)
|
|
|
|
}
|
2023-08-15 16:11:02 +00:00
|
|
|
}
|