Skip to content

Commit

Permalink
Merge pull request #25 from zjutjh/SugarMGP/dev
Browse files Browse the repository at this point in the history
refactor: 删除活动图片时异步
  • Loading branch information
Penryn authored Dec 2, 2024
2 parents 5cfc875 + 7db831c commit 002cbc2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
9 changes: 1 addition & 8 deletions app/controllers/activityController/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,7 @@ func DeleteActivity(c *gin.Context) {
}

// 删除活动对应的图片
objectKey, ok := objectService.GetObjectKeyFromUrl(activity.Img)
if ok {
err = objectService.DeleteObject(objectKey)
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}
}
objectService.DeleteObjectByUrlAsync(activity.Img)

err = activityService.DeleteActivityById(data.ID)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/activityController/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/activityService"
"4u-go/app/services/objectService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
Expand Down Expand Up @@ -61,6 +62,10 @@ func UpdateActivity(c *gin.Context) {
return
}

if data.Img != activity.Img { // 若图片更换则删除旧图片
objectService.DeleteObjectByUrlAsync(activity.Img)
}

{ // 更新活动信息
activity.Title = data.Title
activity.Introduction = data.Introduction
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/objectController/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"4u-go/app/services/objectService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

type uploadFileData struct {
Expand All @@ -35,8 +36,7 @@ func UploadFile(c *gin.Context) {
defer func(file multipart.File) {
err := file.Close()
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
zap.L().Error("文件流关闭错误", zap.Error(err))
}
}(file)

Expand Down
14 changes: 14 additions & 0 deletions app/services/objectService/minioObjectService.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"4u-go/config/objectStorage"
"github.com/minio/minio-go/v7"
"github.com/pkg/errors"
"go.uber.org/zap"
)

// ms 是全局的 MinioService 实例
Expand Down Expand Up @@ -51,3 +52,16 @@ func DeleteObject(objectKey string) error {
}
return nil
}

// DeleteObjectByUrlAsync 通过给定的 Url 异步删除对象
func DeleteObjectByUrlAsync(url string) {
objectKey, ok := GetObjectKeyFromUrl(url)
if ok {
go func(objectKey string) {
err := DeleteObject(objectKey)
if err != nil {
zap.L().Error("Minio 删除对象错误", zap.String("objectKey", objectKey), zap.Error(err))
}
}(objectKey)
}
}

0 comments on commit 002cbc2

Please sign in to comment.