♻️ use minio instead of mongodb
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
package comment
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/ccpwcn/kgo"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"net/http"
|
||||
"path"
|
||||
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
|
||||
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
|
||||
"schisandra-album-cloud-microservices/app/auth/model/mongodb"
|
||||
"schisandra-album-cloud-microservices/app/auth/model/mysql/model"
|
||||
"strconv"
|
||||
|
||||
"schisandra-album-cloud-microservices/common/captcha/verify"
|
||||
"schisandra-album-cloud-microservices/common/constant"
|
||||
@@ -41,9 +46,6 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
|
||||
if !res {
|
||||
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "captcha.verificationFailure"))
|
||||
}
|
||||
if len(req.Images) > 3 {
|
||||
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.tooManyImages"))
|
||||
}
|
||||
userAgent := r.Header.Get("User-Agent")
|
||||
if userAgent == "" {
|
||||
return nil, errors2.New(http.StatusInternalServerError, i18n.FormatText(l.ctx, "comment.commentError"))
|
||||
@@ -74,6 +76,7 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
|
||||
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
|
||||
topicType := constant.CommentTopicType
|
||||
commentType := constant.COMMENT
|
||||
commentReply := l.svcCtx.DB.ScaCommentReply
|
||||
comment := &model.ScaCommentReply{
|
||||
Content: commentContent,
|
||||
UserID: uid,
|
||||
@@ -87,28 +90,52 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
|
||||
OperatingSystem: operatingSystem,
|
||||
Agent: userAgent,
|
||||
}
|
||||
err = l.svcCtx.DB.ScaCommentReply.Create(comment)
|
||||
err = commentReply.Create(comment)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(req.Images) > 0 {
|
||||
imagesData, err := utils.ProcessImages(req.Images)
|
||||
if req.Images != "" {
|
||||
imagesData, err := utils.Base64ToBytes(req.Images)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
commentImages := &types.CommentImages{
|
||||
UserId: uid,
|
||||
TopicId: req.TopicId,
|
||||
CommentId: comment.ID,
|
||||
Images: imagesData,
|
||||
objectKey := path.Join(
|
||||
req.TopicId,
|
||||
time.Now().Format("2006/01"), // 按年/月划分目录
|
||||
strconv.FormatInt(comment.ID, 10),
|
||||
fmt.Sprintf("%s_%s.jpg", uid, kgo.SimpleUuid()),
|
||||
)
|
||||
|
||||
exists, err := l.svcCtx.MinioClient.BucketExists(l.ctx, constant.CommentImagesBucketName)
|
||||
if err != nil || !exists {
|
||||
err = l.svcCtx.MinioClient.MakeBucket(l.ctx, constant.CommentImagesBucketName, minio.MakeBucketOptions{Region: "us-east-1", ObjectLocking: true})
|
||||
if err != nil {
|
||||
logx.Errorf("Failed to create MinIO bucket: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
newCollection := mongodb.MustNewCollection[types.CommentImages](l.svcCtx.MongoClient, constant.COMMENT_IMAGES)
|
||||
_, err = newCollection.Creator().InsertOne(l.ctx, commentImages)
|
||||
// 上传到MinIO
|
||||
_, err = l.svcCtx.MinioClient.PutObject(
|
||||
l.ctx,
|
||||
constant.CommentImagesBucketName,
|
||||
objectKey,
|
||||
bytes.NewReader(imagesData),
|
||||
int64(len(imagesData)),
|
||||
minio.PutObjectOptions{
|
||||
ContentType: "image/jpeg",
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
logx.Errorf("Failed to upload image to MinIO: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
info, err := commentReply.Where(commentReply.ID.Eq(comment.ID)).Update(commentReply.ImagePath, objectKey)
|
||||
if err != nil || info.RowsAffected == 0 {
|
||||
return nil, errors.New("update image path failed")
|
||||
}
|
||||
|
||||
}
|
||||
commentResponse := &types.CommentResponse{
|
||||
Id: comment.ID,
|
||||
|
Reference in New Issue
Block a user