From 3d82709b7e35b18cefaa3a38f9e0c82ee59a97cb Mon Sep 17 00:00:00 2001 From: Hiven Date: Mon, 7 Aug 2023 12:02:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0--base6?= =?UTF-8?q?4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/logic/uploadfilebaselogic.go | 8 +++----- utils/file/base64.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 utils/file/base64.go diff --git a/server/upload/internal/logic/uploadfilebaselogic.go b/server/upload/internal/logic/uploadfilebaselogic.go index 28a8ae1c..46c210c0 100644 --- a/server/upload/internal/logic/uploadfilebaselogic.go +++ b/server/upload/internal/logic/uploadfilebaselogic.go @@ -1,13 +1,12 @@ package logic import ( - "encoding/base64" "fmt" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" + "fusenapi/utils/file" "fusenapi/utils/hash" - "strings" "time" "context" @@ -97,12 +96,11 @@ func (l *UploadFileBaseLogic) UploadFileBase(req *types.UploadFileBaseReq, useri uploadUrl.ResourceId = resourceId uploadUrl.ResourceUrl = *resourceInfo.ResourceUrl } else { - RBase64Point := strings.LastIndex(req.FileData, ";base64,") + 8 - req.FileData = req.FileData[RBase64Point:] - dist, err := base64.StdEncoding.DecodeString(req.FileData) + dist, err := file.FileBase64ToByte(req.FileData) if err != nil { logx.Error(err) + return resp.SetStatus(basic.CodeFileUploadErr, "file upload err,base64tobyte error") } // 创建S3对象存储请求 diff --git a/utils/file/base64.go b/utils/file/base64.go new file mode 100644 index 00000000..84825501 --- /dev/null +++ b/utils/file/base64.go @@ -0,0 +1,17 @@ +package file + +import ( + "encoding/base64" + "strings" +) + +func FileBase64ToByte(fileData string) ([]byte, error) { + RBase64Point := strings.LastIndex(fileData, ";base64,") + 8 + fileDataStr := fileData[RBase64Point:] + dist, err := base64.StdEncoding.DecodeString(fileDataStr) + + if err != nil { + return nil, err + } + return dist, nil +}