From e7b95892ff4731b388ff2e35239f4aab22004065 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Wed, 23 Mar 2022 17:40:51 +0800 Subject: [PATCH] minio upload api --- internal/api/third/minio_storage_credential.go | 6 +++--- pkg/common/constant/constant.go | 1 + pkg/utils/file.go | 11 +++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/api/third/minio_storage_credential.go b/internal/api/third/minio_storage_credential.go index 56887107c..b9d311815 100644 --- a/internal/api/third/minio_storage_credential.go +++ b/internal/api/third/minio_storage_credential.go @@ -47,7 +47,7 @@ func MinioUploadFile(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) return } - snapShotNewName, snapShotNewType := utils.GetNewFileNameAndContentType(snapShotFile.Filename) + snapShotNewName, snapShotNewType := utils.GetNewFileNameAndContentType(snapShotFile.Filename, constant.ImageType) log.Debug(req.OperationID, utils.GetSelfFuncName(), snapShotNewName, snapShotNewType) _, err = minioClient.PutObject(context.Background(), config.Config.Credential.Minio.Bucket, snapShotNewName, snapShotFileObj, snapShotFile.Size, minio.PutObjectOptions{ContentType: snapShotNewType}) if err != nil { @@ -60,7 +60,7 @@ func MinioUploadFile(c *gin.Context) { } file, err := c.FormFile("file") if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing snapshot arg: " + err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing file arg: " + err.Error()}) return } fileObj, err := file.Open() @@ -69,7 +69,7 @@ func MinioUploadFile(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "invalid file path" + err.Error()}) return } - newName, newType := utils.GetNewFileNameAndContentType(file.Filename) + newName, newType := utils.GetNewFileNameAndContentType(file.Filename, req.FileType) log.Debug(req.OperationID, utils.GetSelfFuncName(), newName, newType) _, err = minioClient.PutObject(context.Background(), config.Config.Credential.Minio.Bucket, newName, fileObj, file.Size, minio.PutObjectOptions{ContentType: newType}) if err != nil { diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index 2efb3da82..53cc340e9 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -163,6 +163,7 @@ const ( // minioUpload OtherType = 1 VideoType = 2 + ImageType = 3 ) var ContentType2PushContent = map[int64]string{ diff --git a/pkg/utils/file.go b/pkg/utils/file.go index c72f6fc3d..e46050516 100644 --- a/pkg/utils/file.go +++ b/pkg/utils/file.go @@ -1,9 +1,11 @@ package utils import ( + "Open_IM/pkg/common/constant" "fmt" "math/rand" "os" + "path" "time" ) @@ -26,11 +28,12 @@ func MkDir(path string) error { return os.MkdirAll(path, os.ModePerm) } -func GetNewFileNameAndContentType(fileType string) (string, string) { - newName := fmt.Sprintf("%d-%d%s", time.Now().UnixNano(), rand.Int(), fileType) +func GetNewFileNameAndContentType(fileName string, fileType int) (string, string) { + suffix := path.Ext(fileName) + newName := fmt.Sprintf("%d-%d%s", time.Now().UnixNano(), rand.Int(), fileName) contentType := "" - if fileType == "img" { - contentType = "image/" + fileType[1:] + if fileType == constant.ImageType { + contentType = "image/" + suffix[1:] } return newName, contentType }