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

277 lines
11 KiB

3 years ago
package apiThird
import (
api "Open_IM/pkg/apistruct"
3 years ago
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/log"
2 years ago
"Open_IM/pkg/common/tokenverify"
_ "Open_IM/pkg/common/tokenverify"
3 years ago
"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"
2 years ago
"strconv"
"strings"
3 years ago
)
2 years ago
// @Summary minio上传文件(web api)
// @Description minio上传文件(web api), 请注意本api请求为form并非json
// @Tags 第三方服务相关
// @ID MinioUploadFile
// @Accept json
// @Param token header string true "im token"
// @Param file formData file true "要上传的文件文件"
// @Param fileType formData int true "文件类型"
// @Param operationID formData string true "操作唯一ID"
// @Produce json
// @Success 0 {object} api.MinioUploadFileResp ""
2 years ago
// @Failure 500 {object} api.Swagger500Resp "errCode为500 一般为服务器内部错误"
// @Failure 400 {object} api.Swagger400Resp "errCode为400 一般为参数输入错误, token未带上等"
2 years ago
// @Router /third/minio_upload [post]
3 years ago
func MinioUploadFile(c *gin.Context) {
var (
2 years ago
req api.MinioUploadFileReq
2 years ago
resp api.MinioUploadFile
3 years ago
)
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
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req)
3 years ago
var ok bool
var errInfo string
2 years ago
ok, _, errInfo = tokenverify.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)
2 years ago
c.JSON(http.StatusBadRequest, 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})
3 years ago
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
log.NewError(req.OperationID, utils.GetSelfFuncName(), "FormFile failed", err.Error())
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(), config.Config.Credential.Minio.Bucket, newName, fileObj, file.Size, newType, MinioClient.EndpointURL())
3 years ago
_, err = MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.Bucket, newName, fileObj, file.Size, minio.PutObjectOptions{ContentType: newType})
3 years ago
if err != nil {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "upload file error", err.Error())
3 years ago
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "upload file error" + 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 (
2 years ago
req api.MinioStorageCredentialReq
resp api.MiniostorageCredentialResp
3 years ago
)
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
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req)
3 years ago
var ok bool
var errInfo string
2 years ago
ok, _, errInfo = tokenverify.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 {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "NewSTSAssumeRole failed", err.Error(), stsOpts, config.Config.Credential.Minio.Endpoint)
3 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
v, err := li.Get()
if err != nil {
2 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "li.Get error", err.Error())
3 years ago
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
resp.StorageTime = config.Config.Credential.Minio.StorageTime
2 years ago
resp.IsDistributedMod = config.Config.Credential.Minio.IsDistributedMod
3 years ago
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
3 years ago
}
3 years ago
func UploadUpdateApp(c *gin.Context) {
var (
2 years ago
req api.UploadUpdateAppReq
resp api.UploadUpdateAppResp
3 years ago
)
if err := c.Bind(&req); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req)
2 years ago
3 years ago
var yamlName string
if req.Yaml == nil {
yamlName = ""
} else {
yamlName = req.Yaml.Filename
}
3 years ago
fileObj, err := req.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": "Open file error" + err.Error()})
return
}
3 years ago
_, err = MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, req.File.Filename, fileObj, req.File.Size, minio.PutObjectOptions{})
3 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "PutObject file error")
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "PutObject file error" + err.Error()})
return
}
3 years ago
if yamlName != "" {
3 years ago
yamlObj, err := req.Yaml.Open()
if err == nil {
3 years ago
_, err = MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, yamlName, yamlObj, req.Yaml.Size, minio.PutObjectOptions{})
3 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "PutObject yaml error")
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "PutObject yaml error" + err.Error()})
return
}
} else {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
3 years ago
}
3 years ago
}
3 years ago
if err := imdb.UpdateAppVersion(req.Type, req.Version, req.ForceUpdate, req.File.Filename, yamlName, req.UpdateLog); err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "UpdateAppVersion error", err.Error())
resp.ErrCode = http.StatusInternalServerError
resp.ErrMsg = err.Error()
c.JSON(http.StatusInternalServerError, resp)
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName())
c.JSON(http.StatusOK, resp)
}
2 years ago
func version2Int(version string) (int, error) {
versions := strings.Split(version, ".")
s := strings.Join(versions, "")
versionInt, err := strconv.Atoi(s)
return versionInt, err
}
3 years ago
func GetDownloadURL(c *gin.Context) {
var (
2 years ago
req api.GetDownloadURLReq
resp api.GetDownloadURLResp
3 years ago
)
defer func() {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp)
}()
if err := c.Bind(&req); err != nil {
log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req)
app, err := imdb.GetNewestVersion(req.Type)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "getNewestVersion failed", err.Error())
}
3 years ago
log.Debug(req.OperationID, utils.GetSelfFuncName(), "app: ", app)
3 years ago
if app != nil {
2 years ago
appVersion, err := version2Int(app.Version)
reqVersion, err := version2Int(req.Version)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), "req version", req.Version, "app version", app.Version)
}
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "req version:", reqVersion, "app version:", appVersion)
if appVersion > reqVersion && app.Version != "" {
3 years ago
resp.Data.HasNewVersion = true
if app.ForceUpdate == true {
resp.Data.ForceUpdate = true
}
3 years ago
if app.YamlName != "" {
resp.Data.YamlURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + app.YamlName
}
3 years ago
resp.Data.FileURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + app.FileName
3 years ago
resp.Data.Version = app.Version
resp.Data.UpdateLog = app.UpdateLog
3 years ago
c.JSON(http.StatusOK, resp)
3 years ago
return
3 years ago
} else {
resp.Data.HasNewVersion = false
c.JSON(http.StatusOK, resp)
3 years ago
return
3 years ago
}
}
3 years ago
c.JSON(http.StatusBadRequest, gin.H{"errCode": 0, "errMsg": "not found app version"})
3 years ago
}