Files
open-im-server/internal/api/third/minio_storage_credential.go
T

246 lines
9.7 KiB
Go
Raw Normal View History

2022-02-18 17:06:27 +08:00
package apiThird
import (
apiStruct "Open_IM/pkg/base_info"
2022-02-19 18:48:15 +08:00
"Open_IM/pkg/common/config"
2022-02-18 17:06:27 +08:00
"Open_IM/pkg/common/constant"
2022-05-10 16:53:58 +08:00
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
2022-02-18 17:06:27 +08:00
"Open_IM/pkg/common/log"
2022-02-28 17:57:03 +08:00
"Open_IM/pkg/common/token_verify"
2022-02-19 18:48:15 +08:00
_ "Open_IM/pkg/common/token_verify"
"Open_IM/pkg/utils"
2022-03-23 15:44:34 +08:00
"context"
2022-02-18 17:06:27 +08:00
"github.com/gin-gonic/gin"
2022-03-23 15:44:34 +08:00
"github.com/minio/minio-go/v7"
2022-02-19 18:48:15 +08:00
_ "github.com/minio/minio-go/v7"
cr "github.com/minio/minio-go/v7/pkg/credentials"
"net/http"
2022-02-18 17:06:27 +08:00
)
2022-03-23 15:44:34 +08:00
func MinioUploadFile(c *gin.Context) {
var (
req apiStruct.MinioUploadFileReq
resp apiStruct.MinioUploadFileResp
)
2022-03-23 16:13:18 +08:00
defer func() {
if r := recover(); r != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), r)
2022-03-23 17:15:46 +08:00
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing file or snapShot args"})
2022-03-23 16:13:18 +08:00
return
}
}()
if err := c.Bind(&req); err != nil {
2022-03-23 15:44:34 +08:00
log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
return
}
2022-05-10 10:44:43 +08:00
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req)
2022-05-07 11:55:19 +08:00
var ok bool
var errInfo string
ok, _, errInfo = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
2022-03-25 18:46:27 +08:00
if !ok {
2022-05-07 11:55:19 +08:00
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})
2022-03-25 18:46:27 +08:00
return
}
2022-05-07 11:55:19 +08:00
2022-03-23 15:44:34 +08:00
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req)
switch req.FileType {
2022-03-25 18:46:27 +08:00
// videoType upload snapShot
2022-03-23 15:44:34 +08:00
case constant.VideoType:
2022-03-23 17:15:46 +08:00
snapShotFile, err := c.FormFile("snapShot")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing snapshot arg: " + err.Error()})
return
}
2022-03-23 15:44:34 +08:00
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()})
2022-03-23 16:13:18 +08:00
return
2022-03-23 15:44:34 +08:00
}
2022-03-23 17:40:51 +08:00
snapShotNewName, snapShotNewType := utils.GetNewFileNameAndContentType(snapShotFile.Filename, constant.ImageType)
2022-03-23 17:15:46 +08:00
log.Debug(req.OperationID, utils.GetSelfFuncName(), snapShotNewName, snapShotNewType)
2022-05-10 10:44:43 +08:00
_, err = MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.Bucket, snapShotNewName, snapShotFileObj, snapShotFile.Size, minio.PutObjectOptions{ContentType: snapShotNewType})
2022-03-23 15:44:34 +08:00
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()})
2022-03-23 16:13:18 +08:00
return
2022-03-23 15:44:34 +08:00
}
resp.SnapshotURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.Bucket + "/" + snapShotNewName
resp.SnapshotNewName = snapShotNewName
}
2022-03-23 17:15:46 +08:00
file, err := c.FormFile("file")
if err != nil {
2022-05-10 10:44:43 +08:00
log.NewError(req.OperationID, utils.GetSelfFuncName(), "FormFile failed", err.Error())
2022-03-23 17:40:51 +08:00
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing file arg: " + err.Error()})
2022-03-23 17:15:46 +08:00
return
}
2022-03-23 15:44:34 +08:00
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
}
2022-03-23 17:40:51 +08:00
newName, newType := utils.GetNewFileNameAndContentType(file.Filename, req.FileType)
2022-05-10 16:07:58 +08:00
log.Debug(req.OperationID, utils.GetSelfFuncName(), config.Config.Credential.Minio.Bucket, newName, fileObj, file.Size, newType, MinioClient.EndpointURL())
2022-05-10 10:44:43 +08:00
_, err = MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.Bucket, newName, fileObj, file.Size, minio.PutObjectOptions{ContentType: newType})
2022-03-23 15:44:34 +08:00
if err != nil {
2022-05-10 10:44:43 +08:00
log.NewError(req.OperationID, utils.GetSelfFuncName(), "upload file error")
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "upload file error" + err.Error()})
2022-03-23 15:44:34 +08:00
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
}
2022-02-18 17:06:27 +08:00
func MinioStorageCredential(c *gin.Context) {
var (
2022-03-17 11:57:21 +08:00
req apiStruct.MinioStorageCredentialReq
2022-02-18 17:06:27 +08:00
resp apiStruct.MiniostorageCredentialResp
)
2022-02-19 18:48:15 +08:00
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()})
2022-02-18 17:06:27 +08:00
return
}
2022-05-10 10:44:43 +08:00
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req)
2022-05-07 11:55:19 +08:00
var ok bool
var errInfo string
ok, _, errInfo = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
2022-02-28 17:57:03 +08:00
if !ok {
2022-05-07 11:55:19 +08:00
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})
2022-02-28 17:57:03 +08:00
return
}
2022-05-07 11:55:19 +08:00
2022-02-19 18:48:15 +08:00
var stsOpts cr.STSAssumeRoleOptions
stsOpts.AccessKey = config.Config.Credential.Minio.AccessKeyID
stsOpts.SecretKey = config.Config.Credential.Minio.SecretAccessKey
stsOpts.DurationSeconds = constant.MinioDurationTimes
2022-04-11 17:08:54 +08:00
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)
2022-02-19 18:48:15 +08:00
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
2022-03-17 11:57:21 +08:00
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
2022-02-18 17:06:27 +08:00
}
2022-05-10 16:53:58 +08:00
func UploadUpdateApp(c *gin.Context) {
var (
req apiStruct.UploadUpdateAppReq
resp apiStruct.UploadUpdateAppResp
)
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)
2022-05-10 18:28:57 +08:00
var yamlName string
if req.Yaml == nil {
yamlName = ""
} else {
yamlName = req.Yaml.Filename
}
2022-05-10 18:22:18 +08:00
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
}
2022-05-11 11:07:04 +08:00
_, err = MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, req.File.Filename, fileObj, req.File.Size, minio.PutObjectOptions{})
2022-05-10 16:53:58 +08:00
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
}
2022-05-11 10:46:54 +08:00
if yamlName != "" {
2022-05-10 18:28:57 +08:00
yamlObj, err := req.Yaml.Open()
if err == nil {
2022-05-11 11:07:04 +08:00
_, err = MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, yamlName, yamlObj, req.Yaml.Size, minio.PutObjectOptions{})
2022-05-10 18:28:57 +08:00
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())
2022-05-10 18:22:18 +08:00
}
2022-05-10 16:53:58 +08:00
}
2022-05-11 10:46:54 +08:00
if err := imdb.UpdateAppVersion(req.Type, req.Version, req.ForceUpdate, req.File.Filename, yamlName, req.UpdateLog); err != nil {
2022-05-10 16:53:58 +08:00
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)
}
func GetDownloadURL(c *gin.Context) {
var (
req apiStruct.GetDownloadURLReq
resp apiStruct.GetDownloadURLResp
)
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())
}
2022-05-10 17:23:28 +08:00
log.Debug(req.OperationID, utils.GetSelfFuncName(), "app: ", app)
2022-05-10 17:17:20 +08:00
if app != nil {
if app.Version != req.Version && app.Version != "" {
resp.Data.HasNewVersion = true
if app.ForceUpdate == true {
resp.Data.ForceUpdate = true
}
2022-05-10 18:22:18 +08:00
if app.YamlName != "" {
resp.Data.YamlURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + app.YamlName
}
2022-05-10 17:17:20 +08:00
resp.Data.FileURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + app.FileName
2022-05-11 10:19:58 +08:00
resp.Data.Version = app.Version
resp.Data.UpdateLog = app.UpdateLog
2022-05-10 17:17:20 +08:00
c.JSON(http.StatusOK, resp)
2022-05-10 17:23:28 +08:00
return
2022-05-10 17:17:20 +08:00
} else {
resp.Data.HasNewVersion = false
c.JSON(http.StatusOK, resp)
2022-05-10 17:23:28 +08:00
return
2022-05-10 16:53:58 +08:00
}
}
2022-05-10 17:17:20 +08:00
c.JSON(http.StatusBadRequest, gin.H{"errCode": 0, "errMsg": "not found app version"})
2022-05-10 16:53:58 +08:00
}