fusenapi/server/upload/internal/logic/uploadlogologic.go

227 lines
6.1 KiB
Go
Raw Normal View History

2023-08-02 10:14:53 +00:00
package logic
import (
2023-08-08 06:55:17 +00:00
"encoding/json"
2023-08-15 10:35:16 +00:00
"errors"
2023-08-02 10:14:53 +00:00
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-08-10 06:01:22 +00:00
"fusenapi/utils/curl"
2023-08-10 11:08:06 +00:00
"fusenapi/utils/file"
"fusenapi/utils/hash"
2023-08-08 06:55:17 +00:00
"io"
2023-08-10 11:08:06 +00:00
"net/http"
2023-08-08 06:55:17 +00:00
"strings"
2023-08-02 10:14:53 +00:00
"time"
"context"
"fusenapi/server/upload/internal/svc"
"fusenapi/server/upload/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UploadLogoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
2023-08-10 11:08:06 +00:00
r *http.Request
2023-08-02 10:14:53 +00:00
}
2023-08-10 11:08:06 +00:00
func NewUploadLogoLogic(r *http.Request, svcCtx *svc.ServiceContext) *UploadLogoLogic {
2023-08-02 10:14:53 +00:00
return &UploadLogoLogic{
2023-08-10 11:08:06 +00:00
Logger: logx.WithContext(r.Context()),
ctx: r.Context(),
2023-08-02 10:14:53 +00:00
svcCtx: svcCtx,
2023-08-10 11:08:06 +00:00
r: r,
2023-08-02 10:14:53 +00:00
}
}
// 处理进入前逻辑w,r
// func (l *UploadLogoLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UploadLogoLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if userinfo.IsOnlooker() {
// 如果是,返回未授权的错误码
return resp.SetStatus(basic.CodeUnAuth)
}
2023-08-03 06:38:17 +00:00
var userId int64
var guestId int64
2023-08-02 10:14:53 +00:00
// 检查用户是否是游客
if userinfo.IsGuest() {
// 如果是使用游客ID和游客键名格式
2023-08-03 06:38:17 +00:00
guestId = userinfo.GuestId
2023-08-02 10:14:53 +00:00
} else {
// 否则使用用户ID和用户键名格式
2023-08-03 06:38:17 +00:00
userId = userinfo.UserId
2023-08-02 10:14:53 +00:00
}
2023-08-10 11:08:06 +00:00
//设置内存大小
l.r.ParseMultipartForm(32 << 20)
2023-08-15 10:35:16 +00:00
fileObject, fileHeader, err := l.r.FormFile("file")
2023-08-10 11:08:06 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,no files")
}
2023-08-15 10:35:16 +00:00
defer fileObject.Close()
// 获取文件的MIME类型
fileType := fileHeader.Header.Get("Content-Type")
var imageTypes = make(map[string]struct{}, 7)
imageTypes["image/jpg"] = struct{}{}
imageTypes["image/jpeg"] = struct{}{}
imageTypes["image/png"] = struct{}{}
imageTypes["image/gif"] = struct{}{}
imageTypes["image/bmp"] = struct{}{}
imageTypes["image/tiff"] = struct{}{}
imageTypes["image/webp"] = struct{}{}
imageTypes["image/svg+xml"] = struct{}{}
2023-08-17 04:14:19 +00:00
2023-08-15 10:35:16 +00:00
// 判断文件类型是否为图片
_, ok := imageTypes[fileType]
if !ok {
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,file is not image")
}
2023-08-10 11:08:06 +00:00
2023-08-21 05:42:11 +00:00
// 限制上传文件大小 50k
2023-08-21 11:01:33 +00:00
// maxSize := 100 * 1024
// if fileHeader.Size > int64(maxSize) {
// return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,The file size exceeds the maximum limit of 100k")
// }
2023-08-21 05:42:11 +00:00
2023-08-10 11:08:06 +00:00
// 读取数据流
ioData, err := io.ReadAll(fileObject)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,no files")
}
// 上传文件
var upload = file.Upload{
Ctx: l.ctx,
MysqlConn: l.svcCtx.MysqlConn,
AwsSession: l.svcCtx.AwsSession,
}
var resourceId string = hash.JsonHashKey(req.FileKey)
uploadRes, err := upload.UploadFileByByte(&file.UploadBaseReq{
FileHash: resourceId,
FileByte: ioData,
UploadBucket: 1,
ApiType: 2,
UserId: userId,
GuestId: guestId,
2023-08-17 03:46:17 +00:00
Source: "upload-logo",
2023-08-10 11:08:06 +00:00
})
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeFileUploadErr, "upload file failed")
}
2023-08-02 10:14:53 +00:00
var logoWidth int64
var logoHeight int64
// 查看sku是否存在
if req.SkuId > 0 {
// 查询出产品模板信息
productTemplateV2Model := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
productTemplateV2Info, err := productTemplateV2Model.FindOne(l.ctx, req.SkuId)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeFileUploadLogoErr, "logo upload err,no product template")
}
logoWidth = *productTemplateV2Info.LogoWidth
logoHeight = *productTemplateV2Info.LogoHeight
}
// 设置默认宽高
if logoWidth == 0 || logoHeight == 0 {
logoWidth = 300
logoHeight = 200
}
var resultStr string
2023-08-10 06:01:22 +00:00
2023-08-08 06:55:17 +00:00
var postMap = make(map[string]interface{}, 1)
2023-08-10 11:08:06 +00:00
postMap["logo_url"] = uploadRes.ResourceUrl
2023-08-08 06:55:17 +00:00
postMapB, _ := json.Marshal(postMap)
2023-08-02 10:14:53 +00:00
2023-08-10 06:01:22 +00:00
var headerData = make(map[string]string, 1)
headerData["Content-Type"] = "application/json"
2023-08-18 08:47:22 +00:00
result, err := curl.ApiCall(l.svcCtx.Config.BLMService.ImageProcess.Url, "POST", headerData, strings.NewReader(string(postMapB)), time.Minute*5)
2023-08-10 06:01:22 +00:00
2023-08-08 06:55:17 +00:00
if err != nil {
logx.Error(err)
2023-08-18 08:47:22 +00:00
return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 01")
2023-08-08 06:55:17 +00:00
}
defer result.Body.Close()
b, err := io.ReadAll(result.Body)
if err != nil {
logx.Error(err)
2023-08-18 08:47:22 +00:00
return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 02")
2023-08-08 06:55:17 +00:00
}
2023-08-07 11:41:44 +00:00
2023-08-15 10:35:16 +00:00
if string(b) == "Internal Server Error" {
err = errors.New("BLMService fail Internal Server Error")
logx.Error(err)
2023-08-18 08:47:22 +00:00
return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 03")
2023-08-15 10:35:16 +00:00
} else {
var resData map[string]interface{}
err = json.Unmarshal(b, &resData)
if err != nil || resData == nil {
logx.Error(err)
2023-08-18 08:47:22 +00:00
return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 04")
2023-08-15 10:35:16 +00:00
}
if resData != nil {
if resData["code"].(string) == "200" {
resultStr = resData["data"].(string)
} else {
logx.Error(err)
2023-08-18 08:47:22 +00:00
return resp.SetStatus(basic.CodeFileUploadLogoErrType, "service fail 05")
2023-08-15 10:35:16 +00:00
}
} else {
logx.Error(err)
2023-08-18 08:47:22 +00:00
return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail 06")
2023-08-15 10:35:16 +00:00
}
2023-08-09 10:05:39 +00:00
}
2023-08-02 10:14:53 +00:00
var module = "logo"
var nowTime = time.Now().Unix()
// 新增记录
userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn)
2023-08-07 08:45:46 +00:00
_, err = userMaterialModel.CreateOrUpdate(l.ctx, &gmodel.FsUserMaterial{
2023-08-02 10:14:53 +00:00
Module: &module,
2023-08-03 06:38:17 +00:00
UserId: &userId,
GuestId: &guestId,
2023-08-10 11:08:06 +00:00
ResourceId: &uploadRes.ResourceId,
ResourceUrl: &uploadRes.ResourceUrl,
2023-08-02 10:14:53 +00:00
Metadata: &resultStr,
CreateAt: &nowTime,
})
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeFileUploadLogoErr, "service fail")
}
2023-08-11 09:22:14 +00:00
// 返回成功的响应和上传URL
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"upload_data": UploadUrl{
Status: 1,
ResourceId: uploadRes.ResourceId,
ResourceUrl: uploadRes.ResourceUrl,
},
})
2023-08-02 10:14:53 +00:00
}