Merge pull request #38 from 1005281342/main

降低圈复杂度
pull/40/head
ROC 3 years ago committed by GitHub
commit ca6d543407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -53,6 +53,21 @@ func GetImageSize(img image.Rectangle) (int, int) {
return width, height return width, height
} }
func fileCheck(uploadType string, size int64) error {
if uploadType != "public/video" &&
uploadType != "public/image" &&
uploadType != "public/avatar" &&
uploadType != "attachment" {
return errcode.InvalidParams
}
if size > 1024*1024*100 {
return errcode.FileInvalidSize.WithDetails("最大允许100MB")
}
return nil
}
func UploadAttachment(c *gin.Context) { func UploadAttachment(c *gin.Context) {
response := app.NewResponse(c) response := app.NewResponse(c)
svc := service.New(c) svc := service.New(c)
@ -66,16 +81,9 @@ func UploadAttachment(c *gin.Context) {
} }
defer file.Close() defer file.Close()
if uploadType != "public/video" && if err = fileCheck(uploadType, fileHeader.Size); err != nil {
uploadType != "public/image" && cErr, _ := err.(*errcode.Error)
uploadType != "public/avatar" && response.ToErrorResponse(cErr)
uploadType != "attachment" {
response.ToErrorResponse(errcode.InvalidParams)
return
}
if fileHeader.Size > 1024*1024*100 {
response.ToErrorResponse(errcode.FileInvalidSize.WithDetails("最大允许100MB"))
return return
} }
@ -129,20 +137,21 @@ func UploadAttachment(c *gin.Context) {
attachment.UserID = userID.(int64) attachment.UserID = userID.(int64)
} }
if uploadType == "public/image" || uploadType == "public/avatar" { var uploadAttachmentTypeMap = map[string]model.AttachmentType{
attachment.Type = model.ATTACHMENT_TYPE_IMAGE "public/image": model.ATTACHMENT_TYPE_IMAGE,
"public/avatar": model.ATTACHMENT_TYPE_IMAGE,
"public/video": model.ATTACHMENT_TYPE_VIDEO,
"attachment": model.ATTACHMENT_TYPE_OTHER,
}
src, err := imaging.Decode(file) attachment.Type = uploadAttachmentTypeMap[uploadType]
if attachment.Type == model.ATTACHMENT_TYPE_IMAGE {
var src image.Image
src, err = imaging.Decode(file)
if err == nil { if err == nil {
attachment.ImgWidth, attachment.ImgHeight = GetImageSize(src.Bounds()) attachment.ImgWidth, attachment.ImgHeight = GetImageSize(src.Bounds())
} }
} }
if uploadType == "public/video" {
attachment.Type = model.ATTACHMENT_TYPE_VIDEO
}
if uploadType == "attachment" {
attachment.Type = model.ATTACHMENT_TYPE_OTHER
}
attachment, err = svc.CreateAttachment(attachment) attachment, err = svc.CreateAttachment(attachment)
if err != nil { if err != nil {

@ -186,21 +186,22 @@ func (svc *Service) DeletePostComment(comment *model.Comment) error {
return svc.dao.DeleteComment(comment) return svc.dao.DeleteComment(comment)
} }
func (svc *Service) CreatePostCommentReply(commentID int64, content string, userID, atUserID int64) (*model.CommentReply, error) { func (svc *Service) createPostPreHandler(commentID int64, userID, atUserID int64) (*model.Post, *model.Comment, int64,
error) {
// 加载Comment // 加载Comment
comment, err := svc.dao.GetCommentByID(commentID) comment, err := svc.dao.GetCommentByID(commentID)
if err != nil { if err != nil {
return nil, err return nil, nil, atUserID, err
} }
// 加载comment的post // 加载comment的post
post, err := svc.dao.GetPostByID(comment.PostID) post, err := svc.dao.GetPostByID(comment.PostID)
if err != nil { if err != nil {
return nil, err return nil, nil, atUserID, err
} }
if post.CommentCount >= global.AppSetting.MaxCommentCount { if post.CommentCount >= global.AppSetting.MaxCommentCount {
return nil, errcode.MaxCommentCount return nil, nil, atUserID, errcode.MaxCommentCount
} }
if userID == atUserID { if userID == atUserID {
@ -215,6 +216,19 @@ func (svc *Service) CreatePostCommentReply(commentID int64, content string, user
} }
} }
return post, comment, atUserID, nil
}
func (svc *Service) CreatePostCommentReply(commentID int64, content string, userID, atUserID int64) (*model.CommentReply, error) {
var (
post *model.Post
comment *model.Comment
err error
)
if post, comment, atUserID, err = svc.createPostPreHandler(commentID, userID, atUserID); err != nil {
return nil, err
}
// 创建评论 // 创建评论
ip := svc.ctx.ClientIP() ip := svc.ctx.ClientIP()
reply := &model.CommentReply{ reply := &model.CommentReply{

@ -57,6 +57,25 @@ type PostContentItem struct {
Sort int64 `json:"sort" binding:"required"` Sort int64 `json:"sort" binding:"required"`
} }
// Check 检查PostContentItem属性
func (p *PostContentItem) Check() error {
// 检查附件是否是本站资源
if p.Type == model.CONTENT_TYPE_IMAGE || p.Type == model.CONTENT_TYPE_VIDEO || p.Type == model.
CONTENT_TYPE_ATTACHMENT {
if strings.Index(p.Content, "https://"+global.AliossSetting.AliossDomain) != 0 {
return fmt.Errorf("附件非本站资源")
}
}
// 检查链接是否合法
if p.Type == model.CONTENT_TYPE_LINK {
if strings.Index(p.Content, "http://") != 0 && strings.Index(p.Content, "https://") != 0 {
return fmt.Errorf("链接不合法")
}
}
return nil
}
func (svc *Service) CreatePost(userID int64, param PostCreationReq) (*model.Post, error) { func (svc *Service) CreatePost(userID int64, param PostCreationReq) (*model.Post, error) {
ip := svc.ctx.ClientIP() ip := svc.ctx.ClientIP()
@ -82,18 +101,9 @@ func (svc *Service) CreatePost(userID int64, param PostCreationReq) (*model.Post
} }
for _, item := range param.Contents { for _, item := range param.Contents {
if err = item.Check(); err != nil {
// 检查附件是否是本站资源 // 属性非法
if item.Type == model.CONTENT_TYPE_IMAGE || item.Type == model.CONTENT_TYPE_VIDEO || item.Type == model.CONTENT_TYPE_ATTACHMENT { continue
if strings.Index(item.Content, "https://"+global.AliossSetting.AliossDomain) != 0 {
continue
}
}
// 检查链接是否合法
if item.Type == model.CONTENT_TYPE_LINK {
if strings.Index(item.Content, "http://") != 0 && strings.Index(item.Content, "https://") != 0 {
continue
}
} }
if item.Type == model.CONTENT_TYPE_ATTACHMENT && param.AttachmentPrice > 0 { if item.Type == model.CONTENT_TYPE_ATTACHMENT && param.AttachmentPrice > 0 {

Loading…
Cancel
Save