Files
photography/backend/internal/handler/photo/uploadPhotoHandler.go
xujiang 604b9e59ba fix
2025-07-10 18:09:11 +08:00

54 lines
1.2 KiB
Go

package photo
import (
"fmt"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"photography-backend/internal/logic/photo"
"photography-backend/internal/svc"
"photography-backend/internal/types"
)
// 上传照片
func UploadPhotoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析 multipart form
err := r.ParseMultipartForm(32 << 20) // 32MB
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
// 获取文件
file, header, err := r.FormFile("file")
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
defer file.Close()
// 获取表单参数
req := types.UploadPhotoRequest{
Title: r.FormValue("title"),
Description: r.FormValue("description"),
}
// 解析 category_id
if categoryIdStr := r.FormValue("category_id"); categoryIdStr != "" {
var categoryId int64
if _, err := fmt.Sscanf(categoryIdStr, "%d", &categoryId); err == nil {
req.CategoryId = categoryId
}
}
l := photo.NewUploadPhotoLogic(r.Context(), svcCtx)
resp, err := l.UploadPhoto(&req, file, header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}