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

optimize delete media content that had upload when create tweet failed #156

Merged
merged 2 commits into from
Jul 19, 2022
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
2 changes: 1 addition & 1 deletion internal/dao/jinzhu/tweets.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (s *tweetManageServant) DeletePost(post *model.Post) ([]string, error) {
}

// 删推文
if err := (post).Delete(tx); err != nil {
if err := post.Delete(tx); err != nil {
return err
}

Expand Down
16 changes: 9 additions & 7 deletions internal/model/post_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ const (
CONTENT_TYPE_CHARGE_ATTACHMENT
)

var mediaContentType = []PostContentT{
CONTENT_TYPE_IMAGE,
CONTENT_TYPE_VIDEO,
CONTENT_TYPE_AUDIO,
CONTENT_TYPE_ATTACHMENT,
CONTENT_TYPE_CHARGE_ATTACHMENT,
}
var (
mediaContentType = []PostContentT{
CONTENT_TYPE_IMAGE,
CONTENT_TYPE_VIDEO,
CONTENT_TYPE_AUDIO,
CONTENT_TYPE_ATTACHMENT,
CONTENT_TYPE_CHARGE_ATTACHMENT,
}
)

type PostContent struct {
*Model
Expand Down
45 changes: 34 additions & 11 deletions internal/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,37 @@ func tagsFrom(originTags []string) []string {
}

// CreatePost 创建文章
// TODO: maybe have bug need optimize for use transaction to create post
func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Post, error) {
// TODO: 推文+推文内容需要在一个事务中添加,后续优化
func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (post *model.Post, err error) {
// 获取媒体内容
mediaContents := make([]string, 0, len(param.Contents))
for _, item := range param.Contents {
switch item.Type {
case model.CONTENT_TYPE_IMAGE,
model.CONTENT_TYPE_VIDEO,
model.CONTENT_TYPE_AUDIO,
model.CONTENT_TYPE_ATTACHMENT,
model.CONTENT_TYPE_CHARGE_ATTACHMENT:
mediaContents = append(mediaContents, item.Content)
}
}
defer func() {
if err != nil {
deleteOssObjects(mediaContents)
}
}()

ip := c.ClientIP()
tags := tagsFrom(param.Tags)
post := &model.Post{
post = &model.Post{
UserID: userID,
Tags: strings.Join(tags, ","),
IP: ip,
IPLoc: util.GetIPLoc(ip),
AttachmentPrice: param.AttachmentPrice,
Visibility: param.Visibility,
}
post, err := ds.CreatePost(post)
post, err = ds.CreatePost(post)
if err != nil {
return nil, err
}
Expand All @@ -137,7 +155,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos
Type: item.Type,
Sort: item.Sort,
}
if _, err := ds.CreatePostContent(postContent); err != nil {
if _, err = ds.CreatePostContent(postContent); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -199,7 +217,17 @@ func DeletePost(user *model.User, id int64) *errcode.Error {
return errcode.DeletePostFailed
}

// 删除推文的媒体内容, 宽松处理错误(就是不处理)
// 删除推文的媒体内容
deleteOssObjects(mediaContents)

// 删除索引
DeleteSearchPost(post)

return nil
}

// deleteOssObjects 删除推文的媒体内容, 宽松处理错误(就是不处理), 后续完善
func deleteOssObjects(mediaContents []string) {
mediaContentsSize := len(mediaContents)
if mediaContentsSize > 1 {
objectKeys := make([]string, 0, mediaContentsSize)
Expand All @@ -211,11 +239,6 @@ func DeletePost(user *model.User, id int64) *errcode.Error {
} else if mediaContentsSize == 1 {
oss.DeleteObject(oss.ObjectKey(mediaContents[0]))
}

// 删除索引
DeleteSearchPost(post)

return nil
}

func LockPost(id int64) error {
Expand Down