Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 异步删除活动图片 #26

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 16 additions & 2 deletions app/services/objectService/minioObjectService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 实例
Expand Down Expand Up @@ -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)
}
}
Loading