From fdcd8c46e0751e199af9a2e823c56ec1342178e1 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 12 Jun 2023 20:05:51 +0800 Subject: [PATCH] fix --- .../internal/handler/uploadlogohandler.go | 2 +- .../internal/logic/uploadlogologic.go | 30 +++++++++++++++++-- server/data-transfer/internal/types/types.go | 10 +++---- server_api/data-transfer.api | 7 +++-- utils/image/image_format.go | 10 +++++++ 5 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 utils/image/image_format.go diff --git a/server/data-transfer/internal/handler/uploadlogohandler.go b/server/data-transfer/internal/handler/uploadlogohandler.go index 8481ae57..2d0258c6 100644 --- a/server/data-transfer/internal/handler/uploadlogohandler.go +++ b/server/data-transfer/internal/handler/uploadlogohandler.go @@ -53,7 +53,7 @@ func UploadLogoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } // 创建一个业务逻辑层实例 l := logic.NewUploadLogoLogic(r.Context(), svcCtx) - resp := l.UploadLogo(&req, userinfo) + resp := l.UploadLogo(&req, userinfo, r) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; // 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。 if resp != nil { diff --git a/server/data-transfer/internal/logic/uploadlogologic.go b/server/data-transfer/internal/logic/uploadlogologic.go index 7e266dd2..46e61030 100644 --- a/server/data-transfer/internal/logic/uploadlogologic.go +++ b/server/data-transfer/internal/logic/uploadlogologic.go @@ -4,6 +4,11 @@ import ( "context" "fusenapi/utils/auth" "fusenapi/utils/basic" + "fusenapi/utils/image" + "io/ioutil" + "net/http" + "os" + "strings" "fusenapi/server/data-transfer/internal/svc" "fusenapi/server/data-transfer/internal/types" @@ -25,8 +30,29 @@ func NewUploadLogoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upload } } -func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, loginInfo *auth.UserInfo) (resp *basic.Response) { - // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) +func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, loginInfo *auth.UserInfo, r *http.Request) (resp *basic.Response) { + req.LogoFile = strings.Trim(req.LogoFile, " ") + //图片是否做缩放[php代码不是bool是字符串??然后给python处理] + onlyScale := "false" + imageUrl := "" + if req.LogoFile != "" { + onlyScale = "true" + imageUrl = req.LogoFile + } else { + file, fileInfo, err := r.FormFile("upfile") + if err != nil { + return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()) + } + defer file.Close() + if !image.CheckUploadImageFormat(fileInfo.Filename) { + return resp.SetStatusWithMessage(basic.CodeApiErr, "format of the file is invalid ") + } + fileData, err := ioutil.ReadAll(file) + if err != nil { + return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()) + } + _ = os.WriteFile("a.jpeg", fileData, 0666) + } return resp.SetStatus(basic.CodeOK) } diff --git a/server/data-transfer/internal/types/types.go b/server/data-transfer/internal/types/types.go index d50c9a7c..48aae78c 100644 --- a/server/data-transfer/internal/types/types.go +++ b/server/data-transfer/internal/types/types.go @@ -27,9 +27,10 @@ type UploadQrcodeRsp struct { } type UploadLogoReq struct { - SkuId int64 `form:"skuId"` - IsRemoveBg bool `form:"is_remove_bg"` - Proportion int64 `form:"proportion"` + LogoFile string `form:"logo_file"` //logo地址路径 + SkuId int64 `form:"skuId"` + IsRemoveBg bool `form:"is_remove_bg"` + Proportion int64 `form:"proportion"` } type UploadLogoRsp struct { @@ -39,9 +40,6 @@ type UploadLogoRsp struct { ImgColor []string `json:"img_color"` } -type Request struct { -} - type Response struct { Code int `json:"code"` Message string `json:"msg"` diff --git a/server_api/data-transfer.api b/server_api/data-transfer.api index 8315a875..30433cb3 100644 --- a/server_api/data-transfer.api +++ b/server_api/data-transfer.api @@ -47,9 +47,10 @@ type UploadQrcodeRsp { //上传logo type UploadLogoReq { - SkuId int64 `form:"skuId"` - IsRemoveBg bool `form:"is_remove_bg"` - Proportion int64 `form:"proportion"` + LogoFile string `form:"logo_file"` //logo地址路径 + SkuId int64 `form:"skuId"` + IsRemoveBg bool `form:"is_remove_bg"` + Proportion int64 `form:"proportion"` } type UploadLogoRsp { NobgUrl string `json:"nobg_url"` diff --git a/utils/image/image_format.go b/utils/image/image_format.go new file mode 100644 index 00000000..d307b597 --- /dev/null +++ b/utils/image/image_format.go @@ -0,0 +1,10 @@ +package image + +// 校验上传图片类型合法不合法 +func CheckUploadImageFormat(fileName string) bool { + switch ext { + case "jpg", "png", "fbx", "glb", "hdr", "jpeg", "svg", "pdf", "psd", "webp", "gif": + return true + } + return false +}