From ff86cceb17479653c6c08b06ff4a8c8030bb9a80 Mon Sep 17 00:00:00 2001 From: Hiven Date: Tue, 8 Aug 2023 10:53:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/logic/uploadfilebaselogic.go | 6 +----- .../internal/logic/uploadfilesbackendlogic.go | 6 +----- .../internal/logic/uploadfilesfrontendlogic.go | 18 +----------------- utils/hash/hash.go | 11 +++++++++++ 4 files changed, 14 insertions(+), 27 deletions(-) diff --git a/server/upload/internal/logic/uploadfilebaselogic.go b/server/upload/internal/logic/uploadfilebaselogic.go index dbb0be24..ab38fe4c 100644 --- a/server/upload/internal/logic/uploadfilebaselogic.go +++ b/server/upload/internal/logic/uploadfilebaselogic.go @@ -1,7 +1,6 @@ package logic import ( - "fmt" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" @@ -47,7 +46,6 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null // 定义用户ID和S3键名格式 - var uid int64 var userId int64 var guestId int64 @@ -55,11 +53,9 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri if userinfo.IsGuest() { // 如果是,使用游客ID和游客键名格式 guestId = userinfo.GuestId - uid = guestId } else { // 否则,使用用户ID和用户键名格式 userId = userinfo.UserId - uid = userId } if guestId == 0 { guestId = req.GuestId @@ -89,7 +85,7 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri // 定义S3请求和当前时间 var s3req *request.Request - var resourceId string = hash.JsonHashKey(fmt.Sprintf("%s%d", req.FileKey, uid)) + var resourceId string = hash.JsonHashKey(req.FileKey) var uploadUrl = UploadUrl{} resourceModel := gmodel.NewFsResourceModel(l.svcCtx.MysqlConn) diff --git a/server/upload/internal/logic/uploadfilesbackendlogic.go b/server/upload/internal/logic/uploadfilesbackendlogic.go index e113c6e8..a0fba113 100644 --- a/server/upload/internal/logic/uploadfilesbackendlogic.go +++ b/server/upload/internal/logic/uploadfilesbackendlogic.go @@ -2,7 +2,6 @@ package logic import ( "encoding/json" - "fmt" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" @@ -57,7 +56,6 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, } // 定义用户ID和S3键名格式 - var uid int64 var userId int64 var guestId int64 @@ -65,11 +63,9 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, if userinfo.IsGuest() { // 如果是,使用游客ID和游客键名格式 guestId = userinfo.GuestId - uid = guestId } else { // 否则,使用用户ID和用户键名格式 userId = userinfo.UserId - uid = userId } var uploadInfoList []UploadInfo @@ -132,7 +128,7 @@ func (l *UploadFilesBackendLogic) UploadFilesBackend(req *types.UploadFilesReq, // 一系列业务逻辑....验证类型,文件大小 - var hashKey string = hash.JsonHashKey(fmt.Sprintf("%s%d", info.FileKeys, uid)) + var hashKey string = hash.JsonHashKey(info.FileKeys) source <- UploadData{ FileKey: info.FileKeys, FileType: fileType, diff --git a/server/upload/internal/logic/uploadfilesfrontendlogic.go b/server/upload/internal/logic/uploadfilesfrontendlogic.go index 6ca20550..e0a9d6e2 100644 --- a/server/upload/internal/logic/uploadfilesfrontendlogic.go +++ b/server/upload/internal/logic/uploadfilesfrontendlogic.go @@ -2,7 +2,6 @@ package logic import ( "encoding/json" - "fmt" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/hash" @@ -45,21 +44,6 @@ func NewUploadFilesFrontendLogic(ctx context.Context, svcCtx *svc.ServiceContext func (l *UploadFilesFrontendLogic) UploadFilesFrontend(req *types.UploadFilesReq, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null - // 定义用户ID和S3键名格式 - var uid int64 - var userId int64 - var guestId int64 - - // 检查用户是否是游客 - if userinfo.IsGuest() { - // 如果是,使用游客ID和游客键名格式 - guestId = userinfo.GuestId - uid = guestId - } else { - // 否则,使用用户ID和用户键名格式 - userId = userinfo.UserId - uid = userId - } var uploadInfoList []UploadInfo err := json.Unmarshal([]byte(req.UploadInfo), &uploadInfoList) @@ -98,7 +82,7 @@ func (l *UploadFilesFrontendLogic) UploadFilesFrontend(req *types.UploadFilesReq for _, info := range uploadInfoList { if info.FileSize <= 1024*1024*500 { // 一系列业务逻辑....验证类型,文件大小 - var hashKey string = hash.JsonHashKey(fmt.Sprintf("%s%d", info.FileKeys, uid)) + var hashKey string = hash.JsonHashKey(info.FileKeys) source <- UploadData{ FileKey: info.FileKeys, FileSize: info.FileSize, diff --git a/utils/hash/hash.go b/utils/hash/hash.go index 37ea1c00..968decf1 100644 --- a/utils/hash/hash.go +++ b/utils/hash/hash.go @@ -5,10 +5,21 @@ import ( "crypto/sha256" "encoding/json" "fmt" + "reflect" "sort" ) func JsonHashKey(v interface{}) string { + + if reflect.TypeOf(v).Kind() == reflect.String { + var obj interface{} + err := json.Unmarshal([]byte(v.(string)), &obj) + if err == nil { + // 反序列化成功,直接替换v + v = obj + } + } + h := sha256.New() h.Write(marshalOrdered(v)) return fmt.Sprintf("%x", h.Sum(nil))