From f1dec39851b460b3601d2948860bd509ae1425c9 Mon Sep 17 00:00:00 2001 From: keven1024 Date: Fri, 27 Feb 2026 14:59:09 +0800 Subject: [PATCH] feat(backend): add image conversion handler and refactor image compression logic into a new file --- backend/internal/controllers/task.go | 1 + backend/internal/controllers/task/image.go | 53 +++++++++++++++++++ .../controllers/task/image_compress.go | 33 ------------ 3 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 backend/internal/controllers/task/image.go delete mode 100644 backend/internal/controllers/task/image_compress.go diff --git a/backend/internal/controllers/task.go b/backend/internal/controllers/task.go index 18c208f..f8f8f7a 100644 --- a/backend/internal/controllers/task.go +++ b/backend/internal/controllers/task.go @@ -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 { diff --git a/backend/internal/controllers/task/image.go b/backend/internal/controllers/task/image.go new file mode 100644 index 0000000..257baae --- /dev/null +++ b/backend/internal/controllers/task/image.go @@ -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 +} diff --git a/backend/internal/controllers/task/image_compress.go b/backend/internal/controllers/task/image_compress.go deleted file mode 100644 index 22eb5e5..0000000 --- a/backend/internal/controllers/task/image_compress.go +++ /dev/null @@ -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 -}