use multipart form for image translation
All checks were successful
mozhi pipeline / Push Docker image to Codeberg docker registry (push) Successful in 30m9s
mozhi pipeline / Build and publish artifacts (push) Successful in 1h28m59s

This commit is contained in:
Arya 2024-05-22 11:48:54 +05:30
parent 41c6ec9712
commit 555567ccdb
Signed by: arya
GPG Key ID: 842D12BDA50DF120

View File

@ -1,11 +1,13 @@
package pages package pages
import ( import (
"encoding/base64"
"io"
"strings"
"codeberg.org/aryak/libmozhi" "codeberg.org/aryak/libmozhi"
"codeberg.org/aryak/mozhi/utils" "codeberg.org/aryak/mozhi/utils"
"encoding/base64"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"strings"
) )
// HandleSourceLanguages godoc // HandleSourceLanguages godoc
@ -107,24 +109,37 @@ func HandleTranslate(c *fiber.Ctx) error {
// //
// @Summary Translate an image // @Summary Translate an image
// @Description When engine is set to all, it will return an array of libmozhi.LangOut. // @Description When engine is set to all, it will return an array of libmozhi.LangOut.
// @Param engine query string true "Engine name" // @Param engine formData string true "Engine name"
// @Param from query string true "Source language" // @Param from formData string true "Source language"
// @Param to query string true "Target language" // @Param to formData string true "Target language"
// @Param image query string true "PNG image in base64 format" // @Param image formData file true "PNG image"
// @Success 200 {object} libmozhi.ImgOut // @Success 200 {object} libmozhi.ImgOut
// @Router /api/image [post] // @Router /api/image [post]
func HandleImg(c *fiber.Ctx) error { func HandleImg(c *fiber.Ctx) error {
engine := utils.Sanitize(utils.GetQueryOrFormValue(c, "engine"), "alpha") engine := utils.Sanitize(c.FormValue("engine"), "alpha")
from := utils.Sanitize(utils.GetQueryOrFormValue(c, "from"), "alpha") from := utils.Sanitize(c.FormValue("from"), "alpha")
to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha") to := utils.Sanitize(c.FormValue("to"), "alpha")
image := utils.GetQueryOrFormValue(c, "image") file, err := c.FormFile("image")
if engine == "" || from == "" || to == "" || image == "" { if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, image are required query strings.") return err
} }
if _, err := base64.StdEncoding.DecodeString(image); err != nil { if file.Header.Get("Content-Type") != "image/png" {
return fiber.NewError(fiber.StatusBadRequest, "Invalid base64 image") return fiber.NewError(fiber.StatusBadRequest, "Invalid image, only png images are accepted")
} }
data, err := libmozhi.ImageGoogle(to, from, image) 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.")
}
imageEncoded := base64.StdEncoding.EncodeToString(image)
data, err := libmozhi.ImageGoogle(to, from, imageEncoded)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }