mirror of
https://github.com/keven1024/015.git
synced 2026-05-31 17:39:35 +00:00
feat(backend): add image conversion handler and refactor image compression logic into a new file
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
var handleTaskMap = map[string]func(c *echo.Context) ([]byte, error){
|
||||
"image:compress": task.HandleImageCompress,
|
||||
"image:convert": task.HandleImageConvert,
|
||||
}
|
||||
|
||||
func CreateTask(c *echo.Context) error {
|
||||
|
||||
53
backend/internal/controllers/task/image.go
Normal file
53
backend/internal/controllers/task/image.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
type BaseImageRequest struct {
|
||||
FileId string `json:"file_id"`
|
||||
}
|
||||
|
||||
func HandleImageCompress(c *echo.Context) ([]byte, error) {
|
||||
r := new(BaseImageRequest)
|
||||
if err := c.Bind(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if r.FileId == "" {
|
||||
return nil, errors.New("调用接口参数错误")
|
||||
}
|
||||
json, err := json.Marshal(map[string]any{
|
||||
"file_id": r.FileId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json, nil
|
||||
}
|
||||
|
||||
type ImageConvertRequest struct {
|
||||
BaseImageRequest
|
||||
TargetExt string `json:"target_ext"`
|
||||
}
|
||||
|
||||
func HandleImageConvert(c *echo.Context) ([]byte, error) {
|
||||
r := new(ImageConvertRequest)
|
||||
if err := c.Bind(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if r.FileId == "" {
|
||||
return nil, errors.New("调用接口参数错误")
|
||||
}
|
||||
json, err := json.Marshal(map[string]any{
|
||||
"file_id": r.FileId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json, nil
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
type GenCompressImageRequest struct {
|
||||
FileId string `json:"file_id"`
|
||||
}
|
||||
|
||||
func HandleImageCompress(c *echo.Context) ([]byte, error) {
|
||||
|
||||
r := new(GenCompressImageRequest)
|
||||
if err := c.Bind(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r.FileId == "" {
|
||||
return nil, errors.New("调用接口参数错误")
|
||||
}
|
||||
|
||||
json, err := json.Marshal(map[string]any{
|
||||
"file_id": r.FileId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json, nil
|
||||
}
|
||||
Reference in New Issue
Block a user