minio upload api

pull/218/head
wangchuxiao 3 years ago
parent 4bf5c77374
commit e7b95892ff

@ -47,7 +47,7 @@ func MinioUploadFile(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return return
} }
snapShotNewName, snapShotNewType := utils.GetNewFileNameAndContentType(snapShotFile.Filename) snapShotNewName, snapShotNewType := utils.GetNewFileNameAndContentType(snapShotFile.Filename, constant.ImageType)
log.Debug(req.OperationID, utils.GetSelfFuncName(), snapShotNewName, snapShotNewType) 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}) _, err = minioClient.PutObject(context.Background(), config.Config.Credential.Minio.Bucket, snapShotNewName, snapShotFileObj, snapShotFile.Size, minio.PutObjectOptions{ContentType: snapShotNewType})
if err != nil { if err != nil {
@ -60,7 +60,7 @@ func MinioUploadFile(c *gin.Context) {
} }
file, err := c.FormFile("file") file, err := c.FormFile("file")
if err != nil { 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 return
} }
fileObj, err := file.Open() 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()}) c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "invalid file path" + err.Error()})
return return
} }
newName, newType := utils.GetNewFileNameAndContentType(file.Filename) newName, newType := utils.GetNewFileNameAndContentType(file.Filename, req.FileType)
log.Debug(req.OperationID, utils.GetSelfFuncName(), newName, newType) 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}) _, err = minioClient.PutObject(context.Background(), config.Config.Credential.Minio.Bucket, newName, fileObj, file.Size, minio.PutObjectOptions{ContentType: newType})
if err != nil { if err != nil {

@ -163,6 +163,7 @@ const (
// minioUpload // minioUpload
OtherType = 1 OtherType = 1
VideoType = 2 VideoType = 2
ImageType = 3
) )
var ContentType2PushContent = map[int64]string{ var ContentType2PushContent = map[int64]string{

@ -1,9 +1,11 @@
package utils package utils
import ( import (
"Open_IM/pkg/common/constant"
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"path"
"time" "time"
) )
@ -26,11 +28,12 @@ func MkDir(path string) error {
return os.MkdirAll(path, os.ModePerm) return os.MkdirAll(path, os.ModePerm)
} }
func GetNewFileNameAndContentType(fileType string) (string, string) { func GetNewFileNameAndContentType(fileName string, fileType int) (string, string) {
newName := fmt.Sprintf("%d-%d%s", time.Now().UnixNano(), rand.Int(), fileType) suffix := path.Ext(fileName)
newName := fmt.Sprintf("%d-%d%s", time.Now().UnixNano(), rand.Int(), fileName)
contentType := "" contentType := ""
if fileType == "img" { if fileType == constant.ImageType {
contentType = "image/" + fileType[1:] contentType = "image/" + suffix[1:]
} }
return newName, contentType return newName, contentType
} }

Loading…
Cancel
Save