diff --git a/app/controllers/activityController/delete.go b/app/controllers/activityController/delete.go index 83a8235..7e3268c 100644 --- a/app/controllers/activityController/delete.go +++ b/app/controllers/activityController/delete.go @@ -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 { diff --git a/app/controllers/activityController/update.go b/app/controllers/activityController/update.go index bd94668..23ea8ec 100644 --- a/app/controllers/activityController/update.go +++ b/app/controllers/activityController/update.go @@ -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" @@ -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 diff --git a/app/controllers/objectController/upload.go b/app/controllers/objectController/upload.go index bb3805b..900ec9f 100644 --- a/app/controllers/objectController/upload.go +++ b/app/controllers/objectController/upload.go @@ -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 { @@ -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) diff --git a/app/services/objectService/minioObjectService.go b/app/services/objectService/minioObjectService.go index 00ed5c6..49cee37 100644 --- a/app/services/objectService/minioObjectService.go +++ b/app/services/objectService/minioObjectService.go @@ -2,12 +2,13 @@ package objectService import ( "context" + "fmt" "io" "strings" "4u-go/config/objectStorage" "github.com/minio/minio-go/v7" - "github.com/pkg/errors" + "go.uber.org/zap" ) // ms 是全局的 MinioService 实例 @@ -47,7 +48,20 @@ func DeleteObject(objectKey string) error { minio.RemoveObjectOptions{ForceDelete: true}, ) if err != nil { - return errors.New("failed to delete object from bucket") + return fmt.Errorf("failed to delete object: %w", err) } 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) + } +}