You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/api/third/minio_storage_credential.go

149 lines
5.8 KiB

3 years ago
package apiThird
import (
apiStruct "Open_IM/pkg/base_info"
3 years ago
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/log"
3 years ago
"Open_IM/pkg/common/token_verify"
3 years ago
_ "Open_IM/pkg/common/token_verify"
"Open_IM/pkg/utils"
3 years ago
"context"
3 years ago
"github.com/gin-gonic/gin"
3 years ago
"github.com/minio/minio-go/v7"
3 years ago
_ "github.com/minio/minio-go/v7"
cr "github.com/minio/minio-go/v7/pkg/credentials"
"net/http"
3 years ago
)
3 years ago
func MinioUploadFile(c *gin.Context) {
var (
req apiStruct.MinioUploadFileReq
resp apiStruct.MinioUploadFileResp
)
3 years ago
defer func() {
if r := recover(); r != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), r)
3 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing file or snapShot args"})
3 years ago
return
}
}()
if err := c.Bind(&req); err != nil {
3 years ago
log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
3 years ago
var ok bool
var errInfo string
ok, _, errInfo = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
3 years ago
if !ok {
3 years ago
errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
log.NewError(req.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
3 years ago
return
}
3 years ago
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req)
switch req.FileType {
3 years ago
// videoType upload snapShot
3 years ago
case constant.VideoType:
3 years ago
snapShotFile, err := c.FormFile("snapShot")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing snapshot arg: " + err.Error()})
return
}
3 years ago
snapShotFileObj, err := snapShotFile.Open()
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "Open file error", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
3 years ago
}
3 years ago
snapShotNewName, snapShotNewType := utils.GetNewFileNameAndContentType(snapShotFile.Filename, constant.ImageType)
3 years ago
log.Debug(req.OperationID, utils.GetSelfFuncName(), snapShotNewName, snapShotNewType)
3 years ago
_, err = minioClient.PutObject(context.Background(), config.Config.Credential.Minio.Bucket, snapShotNewName, snapShotFileObj, snapShotFile.Size, minio.PutObjectOptions{ContentType: snapShotNewType})
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "PutObject snapShotFile error", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
3 years ago
}
resp.SnapshotURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.Bucket + "/" + snapShotNewName
resp.SnapshotNewName = snapShotNewName
}
3 years ago
file, err := c.FormFile("file")
if err != nil {
3 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing file arg: " + err.Error()})
3 years ago
return
}
3 years ago
fileObj, err := file.Open()
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "Open file error", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "invalid file path" + err.Error()})
return
}
3 years ago
newName, newType := utils.GetNewFileNameAndContentType(file.Filename, req.FileType)
3 years ago
log.Debug(req.OperationID, utils.GetSelfFuncName(), newName, newType)
3 years ago
_, err = minioClient.PutObject(context.Background(), config.Config.Credential.Minio.Bucket, newName, fileObj, file.Size, minio.PutObjectOptions{ContentType: newType})
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "open file error")
3 years ago
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "invalid file path" + err.Error()})
3 years ago
return
}
resp.NewName = newName
resp.URL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.Bucket + "/" + newName
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
return
}
3 years ago
func MinioStorageCredential(c *gin.Context) {
var (
3 years ago
req apiStruct.MinioStorageCredentialReq
3 years ago
resp apiStruct.MiniostorageCredentialResp
)
3 years ago
if err := c.BindJSON(&req); err != nil {
log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
3 years ago
return
}
3 years ago
var ok bool
var errInfo string
ok, _, errInfo = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
3 years ago
if !ok {
3 years ago
errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
log.NewError(req.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
3 years ago
return
}
3 years ago
3 years ago
var stsOpts cr.STSAssumeRoleOptions
stsOpts.AccessKey = config.Config.Credential.Minio.AccessKeyID
stsOpts.SecretKey = config.Config.Credential.Minio.SecretAccessKey
stsOpts.DurationSeconds = constant.MinioDurationTimes
var endpoint string
if config.Config.Credential.Minio.EndpointInnerEnable {
endpoint = config.Config.Credential.Minio.EndpointInner
} else {
endpoint = config.Config.Credential.Minio.Endpoint
}
li, err := cr.NewSTSAssumeRole(endpoint, stsOpts)
3 years ago
if err != nil {
log.NewError("", utils.GetSelfFuncName(), "NewSTSAssumeRole failed", err.Error(), stsOpts, config.Config.Credential.Minio.Endpoint)
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
v, err := li.Get()
if err != nil {
log.NewError("0", utils.GetSelfFuncName(), "li.Get error", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
resp.SessionToken = v.SessionToken
resp.SecretAccessKey = v.SecretAccessKey
resp.AccessKeyID = v.AccessKeyID
resp.BucketName = config.Config.Credential.Minio.Bucket
resp.StsEndpointURL = config.Config.Credential.Minio.Endpoint
3 years ago
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
3 years ago
}