mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
feat(backend): add image compression endpoints for generating and retrieving compressed images
This commit is contained in:
74
backend/internal/controllers/image.go
Normal file
74
backend/internal/controllers/image.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"backend/internal/models"
|
||||
"backend/internal/utils"
|
||||
"backend/middleware"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type GenCompressImageRequest struct {
|
||||
FileId string `json:"file_id"`
|
||||
}
|
||||
|
||||
func GenCompressImage(c echo.Context) error {
|
||||
cc := c.(*middleware.CustomContext)
|
||||
r := new(GenCompressImageRequest)
|
||||
if err := cc.Bind(r); err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
if r.FileId == "" {
|
||||
return utils.HTTPErrorHandler(c, errors.New("调用接口参数错误"))
|
||||
}
|
||||
client := utils.GetQueueClient()
|
||||
json, err := json.Marshal(map[string]any{
|
||||
"file_id": r.FileId,
|
||||
})
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
info, err := client.Enqueue(asynq.NewTask("image:compress", json))
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
|
||||
return utils.HTTPSuccessHandler(c, map[string]any{
|
||||
"id": info.ID,
|
||||
})
|
||||
}
|
||||
|
||||
func GetCompressImage(c echo.Context) error {
|
||||
cc := c.(*middleware.CustomContext)
|
||||
taskId := cc.Param("id")
|
||||
if taskId == "" {
|
||||
return utils.HTTPErrorHandler(c, errors.New("调用接口参数错误"))
|
||||
}
|
||||
|
||||
taskInfo, err := models.GetRedisTaskInfo(taskId)
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, err)
|
||||
}
|
||||
if taskInfo == nil {
|
||||
client := utils.GetQueueInspector()
|
||||
|
||||
queneTaskInfo, err := client.GetTaskInfo("default", taskId)
|
||||
if err != nil {
|
||||
return utils.HTTPErrorHandler(c, errors.New("任务已过期"))
|
||||
}
|
||||
if queneTaskInfo != nil {
|
||||
return utils.HTTPSuccessHandler(c, map[string]any{
|
||||
"status": "processing",
|
||||
"state": queneTaskInfo.State,
|
||||
})
|
||||
}
|
||||
}
|
||||
return utils.HTTPSuccessHandler(c, map[string]any{
|
||||
"status": "success",
|
||||
"data": taskInfo,
|
||||
})
|
||||
}
|
||||
@@ -35,5 +35,8 @@ func main() {
|
||||
e.GET("/download", controllers.DownloadShare)
|
||||
e.POST("/download", controllers.VaildateShare)
|
||||
|
||||
e.POST("/image/compress", controllers.GenCompressImage)
|
||||
e.GET("/image/compress/:id", controllers.GetCompressImage)
|
||||
|
||||
e.Logger.Fatal(e.Start(":1323"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user