diff --git a/server/upload/internal/logic/uploadfilebackendlogic.go b/server/upload/internal/logic/uploadfilebackendlogic.go index 8e817639..6bd578e1 100644 --- a/server/upload/internal/logic/uploadfilebackendlogic.go +++ b/server/upload/internal/logic/uploadfilebackendlogic.go @@ -91,6 +91,9 @@ func (l *UploadFileBackendLogic) UploadFileBackend(req *types.UploadFileBackendR UserId: userId, GuestId: guestId, Source: req.Source, + Metadata: req.Metadata, + Refresh: req.Refresh, + ResourceId: req.ResourceId, }) if err != nil { diff --git a/server/upload/internal/types/types.go b/server/upload/internal/types/types.go index b633d713..665d70db 100644 --- a/server/upload/internal/types/types.go +++ b/server/upload/internal/types/types.go @@ -40,6 +40,8 @@ type UploadFileBackendReq struct { FileSize int64 `form:"file_size,optional"` // 上传唯一标识信息 Metadata string `form:"meta_data,optional"` // 上传文件额外信息 Source string `form:"source"` // 上传来源 + Refresh int64 `form:"refresh,optional"` // 强制更新 10 + ResourceId string `form:"resource_id,optional"` // 资源ID } type UploadFilesReq struct { @@ -47,6 +49,8 @@ type UploadFilesReq struct { UploadBucket int64 `form:"upload_bucket,options=[1,2],default=1"` // 上传桶名:1=缓存,2=持久 UploadInfo string `form:"upload_info"` // 上传信息 json Source string `form:"source"` // 上传来源 + Refresh int64 `form:"refresh,optional"` // 强制更新 10 + ResourceId string `form:"resource_id,optional"` // 资源ID } type UploadCallbackReq struct { diff --git a/server_api/upload.api b/server_api/upload.api index 5711e58c..092a5e45 100644 --- a/server_api/upload.api +++ b/server_api/upload.api @@ -89,6 +89,8 @@ type ( FileSize int64 `form:"file_size,optional"` // 上传唯一标识信息 Metadata string `form:"meta_data,optional"` // 上传文件额外信息 Source string `form:"source"` // 上传来源 + Refresh int64 `form:"refresh,optional"` // 强制更新 10 + ResourceId string `form:"resource_id,optional"` // 资源ID } UploadFilesReq { @@ -96,6 +98,8 @@ type ( UploadBucket int64 `form:"upload_bucket,options=[1,2],default=1"` // 上传桶名:1=缓存,2=持久 UploadInfo string `form:"upload_info"` // 上传信息 json Source string `form:"source"` // 上传来源 + Refresh int64 `form:"refresh,optional"` // 强制更新 10 + ResourceId string `form:"resource_id,optional"` // 资源ID } UploadCallbackReq { UploadBucket int64 `form:"upload_bucket,options=[1,2],default=1"` // 上传桶名:1=缓存,2=持久 diff --git a/utils/file/upload.go b/utils/file/upload.go index 89b84a44..9991abaa 100644 --- a/utils/file/upload.go +++ b/utils/file/upload.go @@ -70,12 +70,14 @@ func (upload *Upload) UploadFileByBase64(req *UploadBaseReq) (*UploadBaseRes, er var uploadBaseRes = UploadBaseRes{} + // 是否强制刷新 var refresh bool - if req.ResourceId != "" { - refresh = true resourceId = req.ResourceId } + if req.Refresh == 1 { + refresh = true + } err := upload.MysqlConn.Transaction(func(tx *gorm.DB) (err error) { var resourceInfo *gmodel.FsResource @@ -198,15 +200,28 @@ func (upload *Upload) UploadFileByByte(req *UploadBaseReq) (*UploadBaseRes, erro var uploadBaseRes = UploadBaseRes{} - err := upload.MysqlConn.Transaction(func(tx *gorm.DB) error { + // 是否强制刷新 + var refresh bool + if req.ResourceId != "" { + resourceId = req.ResourceId + } + if req.Refresh == 1 { + refresh = true + } + + err := upload.MysqlConn.Transaction(func(tx *gorm.DB) (err error) { var resourceInfo *gmodel.FsResource - err := tx.Where("resource_id =?", resourceId).Take(&resourceInfo).Error - // resourceInfo, err := resourceModelTS.FindOneById(ctx, resourceId) - if err == nil && resourceInfo.ResourceId != "" { - uploadBaseRes.Status = 1 - uploadBaseRes.ResourceId = resourceId - uploadBaseRes.ResourceUrl = *resourceInfo.ResourceUrl - } else { + if !refresh { + err = tx.Where("resource_id =?", resourceId).Take(&resourceInfo).Error + if err == nil && resourceInfo.ResourceId != "" { + uploadBaseRes.Status = 1 + uploadBaseRes.ResourceId = resourceId + uploadBaseRes.ResourceUrl = *resourceInfo.ResourceUrl + } else { + refresh = true + } + } + if refresh { contentType := http.DetectContentType(req.FileByte) // 创建S3对象存储请求 s3req, _ = svc.PutObjectRequest( @@ -235,19 +250,35 @@ func (upload *Upload) UploadFileByByte(req *UploadBaseReq) (*UploadBaseRes, erro uploadBaseRes.ResourceUrl = url var version string = "0.0.1" var nowTime = time.Now().UTC() - err = tx.Create(&gmodel.FsResource{ - ResourceId: resourceId, - UserId: &req.UserId, - GuestId: &req.GuestId, - ResourceType: &contentType, - ResourceUrl: &url, - Version: &version, - UploadedAt: &nowTime, - Metadata: &req.Metadata, - ApiType: &apiType, - BucketName: bucketName, - Source: &req.Source, - }).Error + if refresh { + err = tx.Save(&gmodel.FsResource{ + ResourceId: resourceId, + UserId: &req.UserId, + GuestId: &req.GuestId, + ResourceType: &contentType, + ResourceUrl: &url, + Version: &version, + UploadedAt: &nowTime, + Metadata: &req.Metadata, + ApiType: &apiType, + BucketName: bucketName, + Source: &req.Source, + }).Error + } else { + err = tx.Create(&gmodel.FsResource{ + ResourceId: resourceId, + UserId: &req.UserId, + GuestId: &req.GuestId, + ResourceType: &contentType, + ResourceUrl: &url, + Version: &version, + UploadedAt: &nowTime, + Metadata: &req.Metadata, + ApiType: &apiType, + BucketName: bucketName, + Source: &req.Source, + }).Error + } if err != nil { logx.Errorf("err:%+v,desc:%+v", err, "fail.upload.resourceInfoAdd.mysql") return err