diff --git a/cmd/open_im_api/main.go b/cmd/open_im_api/main.go index e5d7c9930..cd774627b 100644 --- a/cmd/open_im_api/main.go +++ b/cmd/open_im_api/main.go @@ -106,6 +106,8 @@ func main() { thirdGroup.POST("/ali_oss_credential", apiThird.AliOSSCredential) thirdGroup.POST("/minio_storage_credential", apiThird.MinioStorageCredential) thirdGroup.POST("/minio_upload", apiThird.MinioUploadFile) + thirdGroup.POST("/upload_update_app", apiThird.UploadUpdateApp) + thirdGroup.POST("/get_download_url", apiThird.GetDownloadURL) } //Message chatGroup := r.Group("/msg") diff --git a/internal/api/auth/auth.go b/internal/api/auth/auth.go index c4e944acc..59ad579ca 100644 --- a/internal/api/auth/auth.go +++ b/internal/api/auth/auth.go @@ -10,6 +10,7 @@ import ( open_im_sdk "Open_IM/pkg/proto/sdk_ws" "Open_IM/pkg/utils" "context" + "github.com/fatih/structs" "github.com/gin-gonic/gin" "net/http" "strings" @@ -118,7 +119,8 @@ func ParseToken(c *gin.Context) { return } - resp := api.ParseTokenResp{CommResp: api.CommResp{ErrCode: 0, ErrMsg: ""}, ExpireTime: expireTime} + resp := api.ParseTokenResp{CommResp: api.CommResp{ErrCode: 0, ErrMsg: ""}, ExpireTime: api.ExpireTime{ExpireTimeSeconds: uint32(expireTime)}} + resp.Data = structs.Map(&resp.ExpireTime) log.NewInfo(params.OperationID, "ParseToken return ", resp) c.JSON(http.StatusOK, resp) } diff --git a/internal/api/third/minio_init.go b/internal/api/third/minio_init.go index e9aa7c1aa..a93ad9bb7 100644 --- a/internal/api/third/minio_init.go +++ b/internal/api/third/minio_init.go @@ -7,7 +7,6 @@ import ( "context" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" - "github.com/minio/minio-go/v7/pkg/policy" url2 "net/url" ) @@ -30,11 +29,16 @@ func MinioInit() { log.NewError(operationID, utils.GetSelfFuncName(), "parse failed, please check config/config.yaml", err.Error()) return } + opts := &minio.Options{ + Creds: credentials.NewStaticV4(config.Config.Credential.Minio.AccessKeyID, config.Config.Credential.Minio.SecretAccessKey, ""), + } + if minioUrl.Scheme == "http" { + opts.Secure = false + } else if minioUrl.Scheme == "https" { + opts.Secure = true + } log.NewInfo(operationID, utils.GetSelfFuncName(), "Parse ok ", config.Config.Credential.Minio) - MinioClient, err = minio.New(minioUrl.Host, &minio.Options{ - Creds: credentials.NewStaticV4(config.Config.Credential.Minio.AccessKeyID, config.Config.Credential.Minio.SecretAccessKey, ""), - Secure: false, - }) + MinioClient, err = minio.New(minioUrl.Host, opts) log.NewInfo(operationID, utils.GetSelfFuncName(), "new ok ", config.Config.Credential.Minio) if err != nil { log.NewError(operationID, utils.GetSelfFuncName(), "init minio client failed", err.Error()) @@ -74,10 +78,11 @@ func MinioInit() { } } // 自动化桶public的代码 - err = MinioClient.SetBucketPolicy(context.Background(), config.Config.Credential.Minio.Bucket, policy.BucketPolicyReadWrite) - if err != nil { - log.NewDebug("", utils.GetSelfFuncName(), "SetBucketPolicy failed please set in web", err.Error()) - return - } + //err = MinioClient.SetBucketPolicy(context.Background(), config.Config.Credential.Minio.Bucket, policy.BucketPolicyReadWrite) + //err = MinioClient.SetBucketPolicy(context.Background(), config.Config.Credential.Minio.AppBucket, policy.BucketPolicyReadWrite) + //if err != nil { + // log.NewDebug("", utils.GetSelfFuncName(), "SetBucketPolicy failed please set in web", err.Error()) + // return + //} log.NewInfo(operationID, utils.GetSelfFuncName(), "minio create and set policy success") } diff --git a/internal/api/third/minio_storage_credential.go b/internal/api/third/minio_storage_credential.go index d6552518a..c80035c5a 100644 --- a/internal/api/third/minio_storage_credential.go +++ b/internal/api/third/minio_storage_credential.go @@ -4,6 +4,7 @@ import ( apiStruct "Open_IM/pkg/base_info" "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" + imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model" "Open_IM/pkg/common/log" "Open_IM/pkg/common/token_verify" _ "Open_IM/pkg/common/token_verify" @@ -14,6 +15,7 @@ import ( _ "github.com/minio/minio-go/v7" cr "github.com/minio/minio-go/v7/pkg/credentials" "net/http" + "path" ) func MinioUploadFile(c *gin.Context) { @@ -83,7 +85,7 @@ func MinioUploadFile(c *gin.Context) { return } newName, newType := utils.GetNewFileNameAndContentType(file.Filename, req.FileType) - log.Debug(req.OperationID, utils.GetSelfFuncName(), newName, newType) + log.Debug(req.OperationID, utils.GetSelfFuncName(), config.Config.Credential.Minio.Bucket, newName, fileObj, file.Size, newType, MinioClient.EndpointURL()) _, 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(), "upload file error") @@ -147,3 +149,103 @@ func MinioStorageCredential(c *gin.Context) { resp.StsEndpointURL = config.Config.Credential.Minio.Endpoint c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp}) } + +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) + var yamlName string + if req.Yaml == nil { + yamlName = "" + } else { + yamlName = req.Yaml.Filename + } + newFileName, newYamlName, err := utils.GetUploadAppNewName(req.Type, req.Version, req.File.Filename, yamlName) + if err != nil { + log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUploadAppNewName failed", err.Error()) + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "invalid file type" + err.Error()}) + return + } + 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 + } + _, err = MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, newFileName, fileObj, req.File.Size, minio.PutObjectOptions{ContentType: path.Ext(newFileName)}) + 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 + } + if newYamlName != "" { + yamlObj, err := req.Yaml.Open() + if err == nil { + _, err = MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, newYamlName, yamlObj, req.Yaml.Size, minio.PutObjectOptions{ContentType: path.Ext(newYamlName)}) + 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()) + newYamlName = "" + } + } + if err := imdb.UpdateAppVersion(req.Type, req.Version, req.ForceUpdate, newFileName, newYamlName); err != nil { + 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()) + } + log.Debug(req.OperationID, utils.GetSelfFuncName(), "app: ", app) + if app != nil { + if app.Version != req.Version && app.Version != "" { + resp.Data.HasNewVersion = true + if app.ForceUpdate == true { + resp.Data.ForceUpdate = true + } + if app.YamlName != "" { + resp.Data.YamlURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + app.YamlName + } + resp.Data.FileURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + app.FileName + c.JSON(http.StatusOK, resp) + return + } else { + resp.Data.HasNewVersion = false + c.JSON(http.StatusOK, resp) + return + } + } + c.JSON(http.StatusBadRequest, gin.H{"errCode": 0, "errMsg": "not found app version"}) +} diff --git a/internal/cms_api/admin/admin.go b/internal/cms_api/admin/admin.go index eb5ccd156..6390fe166 100644 --- a/internal/cms_api/admin/admin.go +++ b/internal/cms_api/admin/admin.go @@ -11,14 +11,53 @@ import ( pbAdmin "Open_IM/pkg/proto/admin_cms" "Open_IM/pkg/utils" "context" + "fmt" "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" "net/http" + "path" "strings" - "Open_IM/internal/api/third" "github.com/gin-gonic/gin" + url2 "net/url" ) +var ( + minioClient *minio.Client +) + +func init() { + operationID := utils.OperationIDGenerator() + log.NewInfo(operationID, utils.GetSelfFuncName(), "minio config: ", config.Config.Credential.Minio) + var initUrl string + if config.Config.Credential.Minio.EndpointInnerEnable { + initUrl = config.Config.Credential.Minio.EndpointInner + } else { + initUrl = config.Config.Credential.Minio.Endpoint + } + log.NewInfo(operationID, utils.GetSelfFuncName(), "use initUrl: ", initUrl) + minioUrl, err := url2.Parse(initUrl) + if err != nil { + log.NewError(operationID, utils.GetSelfFuncName(), "parse failed, please check config/config.yaml", err.Error()) + return + } + opts := &minio.Options{ + Creds: credentials.NewStaticV4(config.Config.Credential.Minio.AccessKeyID, config.Config.Credential.Minio.SecretAccessKey, ""), + } + if minioUrl.Scheme == "http" { + opts.Secure = false + } else if minioUrl.Scheme == "https" { + opts.Secure = true + } + log.NewInfo(operationID, utils.GetSelfFuncName(), "Parse ok ", config.Config.Credential.Minio) + minioClient, err = minio.New(minioUrl.Host, opts) + log.NewInfo(operationID, utils.GetSelfFuncName(), "new ok ", config.Config.Credential.Minio) + if err != nil { + log.NewError(operationID, utils.GetSelfFuncName(), "init minio client failed", err.Error()) + return + } +} + // register func AdminLogin(c *gin.Context) { var ( @@ -51,34 +90,74 @@ func UploadUpdateApp(c *gin.Context) { resp apiStruct.UploadUpdateAppResp ) if err := c.Bind(&req); err != nil { - log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error()) + 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) - newFileName, newYamlName, err := utils.GetUploadAppNewName(req.Type, req.Version) + + //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 + //} + //yamlObj, err := req.Yaml.Open() + //if err != nil { + // log.NewError(req.OperationID, utils.GetSelfFuncName(), "Open Yaml error", err.Error()) + // c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "Open Yaml error" + err.Error()}) + // return + //} + + // v2.0.9_app_linux v2.0.9_yaml_linux + file, err := c.FormFile("file") if err != nil { - log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUploadAppNewName failed", err.Error()) - c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "invalid file type" + err.Error()}) + log.NewError(req.OperationID, utils.GetSelfFuncName(), "FormFile failed", err.Error()) + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing file arg: " + err.Error()}) return } - fileObj, err := req.File.Open() - yamlObj, err := req.Yaml.Open() + 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 } - log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "name: ", newFileName, newYamlName) - // v2.0.9_app_linux v2.0.9_yaml_linux - _, err = apiThird.MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, newFileName, fileObj, req.File.Size, minio.PutObjectOptions{}) - _, err = apiThird.MinioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, newYamlName, yamlObj, req.Yaml.Size, minio.PutObjectOptions{}) + + yaml, err := c.FormFile("yaml") if err != nil { - log.NewError(req.OperationID, utils.GetSelfFuncName(), "open file error") - c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "invalid file path" + err.Error()}) + log.NewError(req.OperationID, utils.GetSelfFuncName(), "FormFile failed", err.Error()) + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "missing file arg: " + err.Error()}) return } - if err := imdb.UpdateAppVersion(req.Type, req.Version, req.ForceUpdate); err != nil { + yamlObj, err := yaml.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 + } + newFileName, newYamlName, err := utils.GetUploadAppNewName(req.Type, req.Version, file.Filename, yaml.Filename) + if err != nil { + log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUploadAppNewName failed", err.Error()) + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "invalid file type" + err.Error()}) + return + } + + fmt.Println(req.OperationID, utils.GetSelfFuncName(), "name: ", config.Config.Credential.Minio.AppBucket, newFileName, fileObj, file.Size) + fmt.Println(req.OperationID, utils.GetSelfFuncName(), "name: ", config.Config.Credential.Minio.AppBucket, newYamlName, yamlObj, yaml.Size) + + _, err = minioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, newFileName, fileObj, file.Size, minio.PutObjectOptions{ContentType: path.Ext(newFileName)}) + 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 + } + _, err = minioClient.PutObject(context.Background(), config.Config.Credential.Minio.AppBucket, newYamlName, yamlObj, yaml.Size, minio.PutObjectOptions{ContentType: path.Ext(newYamlName)}) + 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 + } + if err := imdb.UpdateAppVersion(req.Type, req.Version, req.ForceUpdate, newFileName, newYamlName); err != nil { log.NewError(req.OperationID, utils.GetSelfFuncName(), "UpdateAppVersion error", err.Error()) resp.ErrCode = http.StatusInternalServerError resp.ErrMsg = err.Error() @@ -103,28 +182,25 @@ func GetDownloadURL(c *gin.Context) { return } log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req) - fileName, yamlName, err := utils.GetUploadAppNewName(req.Type, req.Version) - if err != nil { - log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetUploadAppNewName failed", err.Error()) - c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "invalid file type" + err.Error()}) - return - } app, err := imdb.GetNewestVersion(req.Type) if err != nil { log.NewError(req.OperationID, utils.GetSelfFuncName(), "getNewestVersion failed", err.Error()) - c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "getNewestVersion failed" + err.Error()}) - return } - if app.Version != req.Version { - resp.Data.HasNewVersion = true - if app.ForceUpdate == true { - resp.Data.ForceUpdate = true + if app != nil { + if app.Version != req.Version && app.Version != "" { + resp.Data.HasNewVersion = true + if app.ForceUpdate == true { + resp.Data.ForceUpdate = true + } + resp.Data.YamlURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + app.YamlName + resp.Data.FileURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + app.FileName + c.JSON(http.StatusOK, resp) + return + } else { + resp.Data.HasNewVersion = false + c.JSON(http.StatusOK, resp) + return } - resp.Data.YamlURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + yamlName - resp.Data.FileURL = config.Config.Credential.Minio.Endpoint + "/" + config.Config.Credential.Minio.AppBucket + "/" + fileName - c.JSON(http.StatusOK, resp) - } else { - resp.Data.HasNewVersion = false - c.JSON(http.StatusOK, resp) } + c.JSON(http.StatusBadRequest, gin.H{"errCode": 0, "errMsg": "not found app version"}) } diff --git a/pkg/base_info/auth_api_struct.go b/pkg/base_info/auth_api_struct.go index 54c0025ef..431a00c2c 100644 --- a/pkg/base_info/auth_api_struct.go +++ b/pkg/base_info/auth_api_struct.go @@ -42,7 +42,17 @@ type ParseTokenReq struct { OperationID string `json:"operationID" binding:"required"` } +//type ParseTokenResp struct { +// CommResp +// ExpireTime int64 `json:"expireTime" binding:"required"` +//} + +type ExpireTime struct { + ExpireTimeSeconds uint32 `json:"expireTimeSeconds" ` +} + type ParseTokenResp struct { CommResp - ExpireTime int64 `json:"token" binding:"required"` + Data map[string]interface{} `json:"data"` + ExpireTime ExpireTime `json:"-"` } diff --git a/pkg/base_info/minio_api_struct.go b/pkg/base_info/minio_api_struct.go index 6d3b0ecac..a95b4c427 100644 --- a/pkg/base_info/minio_api_struct.go +++ b/pkg/base_info/minio_api_struct.go @@ -1,5 +1,7 @@ package base_info +import "mime/multipart" + type MinioStorageCredentialReq struct { OperationID string `json:"operationID"` } @@ -23,3 +25,32 @@ type MinioUploadFileResp struct { SnapshotURL string `json:"snapshotURL,omitempty"` SnapshotNewName string `json:"snapshotName,omitempty"` } + +type UploadUpdateAppReq struct { + OperationID string `form:"operationID" binding:"required"` + Type int `form:"type" binding:"required"` + Version string `form:"version" binding:"required"` + File *multipart.FileHeader `form:"file" binding:"required"` + Yaml *multipart.FileHeader `form:"yaml"` + ForceUpdate bool `form:"forceUpdate"` +} + +type UploadUpdateAppResp struct { + CommResp +} + +type GetDownloadURLReq struct { + OperationID string `json:"operationID" binding:"required"` + Type int `json:"type" binding:"required"` + Version string `json:"version" binding:"required"` +} + +type GetDownloadURLResp struct { + CommResp + Data struct { + HasNewVersion bool `json:"hasNewVersion"` + ForceUpdate bool `json:"forceUpdate"` + FileURL string `json:"fileURL"` + YamlURL string `json:"yamlURL"` + } `json:"data"` +} diff --git a/pkg/cms_api_struct/admin.go b/pkg/cms_api_struct/admin.go index e06a04324..02463f6e5 100644 --- a/pkg/cms_api_struct/admin.go +++ b/pkg/cms_api_struct/admin.go @@ -2,7 +2,6 @@ package cms_api_struct import ( apiStruct "Open_IM/pkg/base_info" - "mime/multipart" ) type AdminLoginRequest struct { @@ -15,12 +14,12 @@ type AdminLoginResponse struct { } type UploadUpdateAppReq struct { - OperationID string `form:"operationID" binding:"required"` - Type int `form:"type" binding:"required"` - Version string `form:"version" binding:"required"` - File multipart.FileHeader `form:"file" binding:"required"` - Yaml multipart.FileHeader `form:"yaml" binding:"required"` - ForceUpdate bool `form:"forceUpdate" binding:"required"` + OperationID string `form:"operationID" binding:"required"` + Type int `form:"type" binding:"required"` + Version string `form:"version" binding:"required"` + //File *multipart.FileHeader `form:"file" binding:"required"` + //Yaml *multipart.FileHeader `form:"yaml" binding:"required"` + ForceUpdate bool `form:"forceUpdate" binding:"required"` } type UploadUpdateAppResp struct { diff --git a/pkg/common/db/model_struct.go b/pkg/common/db/model_struct.go index 6ed3f2dc1..03709a67d 100644 --- a/pkg/common/db/model_struct.go +++ b/pkg/common/db/model_struct.go @@ -274,10 +274,12 @@ func (DepartmentMember) TableName() string { } type AppVersion struct { - Version string `gorm:"column:user_id;size:64"` - Type int `gorm:"column:user_id;primary_key"` - UpdateTime int `gorm:"column:update_time"` - ForceUpdate bool `gorm:"column:force_update"` + Version string `gorm:"column:version;size:64" json:"version"` + Type int `gorm:"column:type;primary_key" json:"type"` + UpdateTime int `gorm:"column:update_time" json:"update_time"` + ForceUpdate bool `gorm:"column:force_update" json:"force_update"` + FileName string `gorm:"column:file_name" json:"file_name"` + YamlName string `gorm:"column:yaml_name" json:"yaml_name"` } func (AppVersion) TableName() string { diff --git a/pkg/common/db/mysql_model/im_mysql_model/file_model.go b/pkg/common/db/mysql_model/im_mysql_model/file_model.go index 8265ee9de..983dec328 100644 --- a/pkg/common/db/mysql_model/im_mysql_model/file_model.go +++ b/pkg/common/db/mysql_model/im_mysql_model/file_model.go @@ -5,18 +5,22 @@ import ( "time" ) -func UpdateAppVersion(appType int, version string, forceUpdate bool) error { +func UpdateAppVersion(appType int, version string, forceUpdate bool, fileName, yamlName string) error { dbConn, err := db.DB.MysqlDB.DefaultGormDB() if err != nil { return err } + updateTime := int(time.Now().Unix()) app := db.AppVersion{ Version: version, Type: appType, - UpdateTime: int(time.Now().Unix()), + UpdateTime: updateTime, + FileName: fileName, + YamlName: yamlName, ForceUpdate: forceUpdate, } - result := dbConn.Model(db.AppVersion{}).Where("app_type = ?", appType).Updates(&app) + result := dbConn.Model(db.AppVersion{}).Where("type = ?", appType).Update(map[string]interface{}{"force_update": forceUpdate, + "version": version, "update_time": int(time.Now().Unix()), "file_name": fileName, "yaml_name": yamlName, "type": appType}) if result.Error != nil { return result.Error } @@ -29,10 +33,10 @@ func UpdateAppVersion(appType int, version string, forceUpdate bool) error { func GetNewestVersion(appType int) (*db.AppVersion, error) { dbConn, err := db.DB.MysqlDB.DefaultGormDB() + app := db.AppVersion{} if err != nil { - return nil, err + return &app, err } dbConn.LogMode(true) - app := db.AppVersion{} return &app, dbConn.Model(db.AppVersion{}).First(&app, appType).Error } diff --git a/pkg/utils/file.go b/pkg/utils/file.go index c5db776d0..9108ede8f 100644 --- a/pkg/utils/file.go +++ b/pkg/utils/file.go @@ -39,32 +39,39 @@ func GetNewFileNameAndContentType(fileName string, fileType int) (string, string return newName, contentType } -func GetUploadAppNewName(appType int, version string) (string, string, error) { - var newFileName, newYamlName = version + "_app_", version + "_yaml_" +func GetUploadAppNewName(appType int, version, fileName, yamlName string) (string, string, error) { + var newFileName, newYamlName = "_" + version + "_app", "_" + version + "_yaml" switch appType { case constant.IOSPlatformID: - newFileName += constant.IOSPlatformStr - newYamlName += constant.IOSPlatformStr + newFileName = constant.IOSPlatformStr + newFileName + newYamlName = constant.IOSPlatformStr + newYamlName case constant.AndroidPlatformID: - newFileName += constant.AndroidPlatformStr - newYamlName += constant.AndroidPlatformStr + newFileName = constant.AndroidPlatformStr + newFileName + newYamlName = constant.AndroidPlatformStr + newYamlName case constant.WindowsPlatformID: - newFileName += constant.WindowsPlatformStr - newYamlName += constant.WindowsPlatformStr + newFileName = constant.WindowsPlatformStr + newFileName + newYamlName = constant.WindowsPlatformStr + newYamlName case constant.OSXPlatformID: - newFileName += constant.OSXPlatformStr - newYamlName += constant.OSXPlatformStr + newFileName = constant.OSXPlatformStr + newFileName + newYamlName = constant.OSXPlatformStr + newYamlName case constant.WebPlatformID: - newFileName += constant.WebPlatformStr - newYamlName += constant.WebPlatformStr + newFileName = constant.WebPlatformStr + newFileName + newYamlName = constant.WebPlatformStr + newYamlName case constant.MiniWebPlatformID: - newFileName += constant.MiniWebPlatformStr - newYamlName += constant.MiniWebPlatformStr + newFileName = constant.MiniWebPlatformStr + newFileName + newYamlName = constant.MiniWebPlatformStr + newYamlName case constant.LinuxPlatformID: - newFileName += constant.LinuxPlatformStr - newYamlName += constant.LinuxPlatformStr + newFileName = constant.LinuxPlatformStr + newFileName + newYamlName = constant.LinuxPlatformStr + newYamlName default: return "", "", errors.New("invalid app type") } + suffixFile := path.Ext(fileName) + suffixYaml := path.Ext(yamlName) + newFileName = fmt.Sprintf("%s%s", newFileName, suffixFile) + newYamlName = fmt.Sprintf("%s%s", newYamlName, suffixYaml) + if yamlName == "" { + newYamlName = "" + } return newFileName, newYamlName, nil }