mirror of
https://codeberg.org/aryak/mozhi
synced 2024-11-23 09:12:57 +05:30
use multipart form for image translation
This commit is contained in:
parent
41c6ec9712
commit
555567ccdb
45
pages/api.go
45
pages/api.go
@ -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())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user