From f87842deb455a4cda0aaafc3dbcb8f30ca53c2d2 Mon Sep 17 00:00:00 2001 From: Hiven Date: Mon, 7 Aug 2023 19:41:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/upload/internal/logic/uploadfilebaselogic.go | 6 +++--- server/upload/internal/logic/uploadfilesbackendlogic.go | 3 ++- server/upload/internal/logic/uploadlogologic.go | 7 +++++++ server/upload/internal/types/types.go | 2 +- server_api/upload.api | 2 +- utils/file/base64.go | 9 +++++---- 6 files changed, 19 insertions(+), 10 deletions(-) diff --git a/server/upload/internal/logic/uploadfilebaselogic.go b/server/upload/internal/logic/uploadfilebaselogic.go index 62912c02..2b1f5d93 100644 --- a/server/upload/internal/logic/uploadfilebaselogic.go +++ b/server/upload/internal/logic/uploadfilebaselogic.go @@ -67,7 +67,7 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri // 定义存储桶名称 var bucketName *string - var apiType int64 = 2 + var apiType int64 = req.ApiType // 根据类别选择存储桶 switch req.UploadBucket { @@ -96,7 +96,7 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri uploadUrl.ResourceId = resourceId uploadUrl.ResourceUrl = *resourceInfo.ResourceUrl } else { - dist, err := file.FileBase64ToByte(req.FileData) + dist, contentType, err := file.FileBase64ToByte(req.FileData) if err != nil { logx.Error(err) @@ -134,7 +134,7 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri ResourceId: resourceId, UserId: &userId, GuestId: &guestId, - ResourceType: &req.FileType, + ResourceType: &contentType, ResourceUrl: &url, Version: &version, UploadedAt: &nowTime, diff --git a/server/upload/internal/logic/uploadfilesbackendlogic.go b/server/upload/internal/logic/uploadfilesbackendlogic.go index ac7f0e40..e113c6e8 100644 --- a/server/upload/internal/logic/uploadfilesbackendlogic.go +++ b/server/upload/internal/logic/uploadfilesbackendlogic.go @@ -177,6 +177,7 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, logx.Error(err) uploadUrl.Status = 0 } else { + contentType := http.DetectContentType(uploadDataInfo.FileData) var url = s3req.HTTPRequest.URL.String() // 打印请求URL logx.Info(url) @@ -189,7 +190,7 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, ResourceId: resourceId, UserId: &userId, GuestId: &guestId, - ResourceType: &uploadDataInfo.FileType, + ResourceType: &contentType, ResourceUrl: &url, Version: &version, UploadedAt: &nowTime, diff --git a/server/upload/internal/logic/uploadlogologic.go b/server/upload/internal/logic/uploadlogologic.go index 50415695..99084502 100644 --- a/server/upload/internal/logic/uploadlogologic.go +++ b/server/upload/internal/logic/uploadlogologic.go @@ -110,6 +110,13 @@ func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, userinfo *auth.Us // } // resultStr = string(b) + // 上传图片 + var reqs types.UploadFileBaseReq + // reqs.FileType = + + // 创建一个业务逻辑层实例 + NewUploadFileBaseLogic(l.ctx, l.svcCtx).UploadFileBase(&reqs, userinfo) + var module = "logo" var nowTime = time.Now().Unix() // 新增记录 diff --git a/server/upload/internal/types/types.go b/server/upload/internal/types/types.go index 4f083802..952e4282 100644 --- a/server/upload/internal/types/types.go +++ b/server/upload/internal/types/types.go @@ -6,7 +6,7 @@ import ( ) type UploadFileBaseReq struct { - FileType string `form:"file_type"` // 上传文件类型 + ApiType int64 `form:"api_type,options=[1,2],default=1"` // 调用类型:1=对外,2=对内 FileKey string `form:"file_key"` // 上传唯一标识信息 FileData string `form:"file_data"` // 上传文件额外信息 Metadata string `form:"meta_data,optional"` // 上传文件额外信息 diff --git a/server_api/upload.api b/server_api/upload.api index a44c6aca..d0bfdef2 100644 --- a/server_api/upload.api +++ b/server_api/upload.api @@ -46,7 +46,7 @@ service upload { type ( UploadFileBaseReq { - FileType string `form:"file_type"` // 上传文件类型 + ApiType int64 `form:"api_type,options=[1,2],default=1"` // 调用类型:1=对外,2=对内 FileKey string `form:"file_key"` // 上传唯一标识信息 FileData string `form:"file_data"` // 上传文件额外信息 Metadata string `form:"meta_data,optional"` // 上传文件额外信息 diff --git a/utils/file/base64.go b/utils/file/base64.go index 84825501..0548f6fc 100644 --- a/utils/file/base64.go +++ b/utils/file/base64.go @@ -2,16 +2,17 @@ package file import ( "encoding/base64" + "net/http" "strings" ) -func FileBase64ToByte(fileData string) ([]byte, error) { +func FileBase64ToByte(fileData string) ([]byte, string, error) { RBase64Point := strings.LastIndex(fileData, ";base64,") + 8 fileDataStr := fileData[RBase64Point:] dist, err := base64.StdEncoding.DecodeString(fileDataStr) - if err != nil { - return nil, err + return nil, "", err } - return dist, nil + contentType := http.DetectContentType(dist) + return dist, contentType, nil }