mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-28 04:27:24 +00:00
Enhance comment functionality with image support and validation.
This commit is contained in:
@@ -7,7 +7,19 @@ import (
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
)
|
||||
|
||||
func CreateComment(content string, userID uint, resourceID uint) (*model.CommentView, error) {
|
||||
const (
|
||||
maxImagePerComment = 9
|
||||
)
|
||||
|
||||
type CommentRequest struct {
|
||||
Content string `json:"content"`
|
||||
Images []uint `json:"images"`
|
||||
}
|
||||
|
||||
func CreateComment(req CommentRequest, userID uint, resourceID uint) (*model.CommentView, error) {
|
||||
if len(req.Images) > maxImagePerComment {
|
||||
return nil, model.NewRequestError("Too many images, maximum is 9")
|
||||
}
|
||||
resourceExists, err := dao.ExistsResource(resourceID)
|
||||
if err != nil {
|
||||
log.Error("Error checking resource existence:", err)
|
||||
@@ -24,7 +36,7 @@ func CreateComment(content string, userID uint, resourceID uint) (*model.Comment
|
||||
if !userExists {
|
||||
return nil, model.NewNotFoundError("User not found")
|
||||
}
|
||||
c, err := dao.CreateComment(content, userID, resourceID)
|
||||
c, err := dao.CreateComment(req.Content, userID, resourceID, req.Images)
|
||||
if err != nil {
|
||||
log.Error("Error creating comment:", err)
|
||||
return nil, model.NewInternalServerError("Error creating comment")
|
||||
@@ -70,7 +82,10 @@ func ListCommentsWithUser(username string, page int) ([]model.CommentWithResourc
|
||||
return res, totalPages, nil
|
||||
}
|
||||
|
||||
func UpdateComment(commentID, userID uint, content string) (*model.CommentView, error) {
|
||||
func UpdateComment(commentID, userID uint, req CommentRequest) (*model.CommentView, error) {
|
||||
if len(req.Images) > maxImagePerComment {
|
||||
return nil, model.NewRequestError("Too many images, maximum is 9")
|
||||
}
|
||||
comment, err := dao.GetCommentByID(commentID)
|
||||
if err != nil {
|
||||
return nil, model.NewNotFoundError("Comment not found")
|
||||
@@ -78,7 +93,7 @@ func UpdateComment(commentID, userID uint, content string) (*model.CommentView,
|
||||
if comment.UserID != userID {
|
||||
return nil, model.NewRequestError("You can only update your own comments")
|
||||
}
|
||||
updated, err := dao.UpdateCommentContent(commentID, content)
|
||||
updated, err := dao.UpdateCommentContent(commentID, req.Content, req.Images)
|
||||
if err != nil {
|
||||
return nil, model.NewInternalServerError("Error updating comment")
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@ import (
|
||||
"nysoure/server/utils"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3/log"
|
||||
@@ -53,14 +54,51 @@ func init() {
|
||||
}()
|
||||
}
|
||||
|
||||
func CreateImage(uid uint, data []byte) (uint, error) {
|
||||
var (
|
||||
imageUploadsByIP = make(map[string]uint)
|
||||
imageUploadsLock = sync.RWMutex{}
|
||||
)
|
||||
|
||||
const maxUploadsPerIP = 100
|
||||
|
||||
func init() {
|
||||
// Initialize the map with a cleanup function to remove old entries
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(24 * time.Hour) // Cleanup every 24 hours
|
||||
imageUploadsLock.Lock()
|
||||
imageUploadsByIP = make(map[string]uint) // Clear the map
|
||||
imageUploadsLock.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func addIpUploadCount(ip string) bool {
|
||||
imageUploadsLock.Lock()
|
||||
defer imageUploadsLock.Unlock()
|
||||
|
||||
count, exists := imageUploadsByIP[ip]
|
||||
if !exists {
|
||||
count = 0
|
||||
}
|
||||
if count >= maxUploadsPerIP {
|
||||
return false // Exceeded upload limit for this IP
|
||||
}
|
||||
imageUploadsByIP[ip] = count + 1
|
||||
return true // Upload count incremented successfully
|
||||
}
|
||||
|
||||
func CreateImage(uid uint, ip string, data []byte) (uint, error) {
|
||||
canUpload, err := checkUserCanUpload(uid)
|
||||
if err != nil {
|
||||
log.Error("Error checking user upload permission:", err)
|
||||
return 0, model.NewInternalServerError("Error checking user upload permission")
|
||||
}
|
||||
if !canUpload {
|
||||
return 0, model.NewUnAuthorizedError("User cannot upload images")
|
||||
// For a normal user, check the IP upload limit
|
||||
if !addIpUploadCount(ip) {
|
||||
return 0, model.NewUnAuthorizedError("You have reached the maximum upload limit")
|
||||
}
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
|
Reference in New Issue
Block a user