Enhance comment functionality with image support and validation.

This commit is contained in:
2025-06-24 12:00:48 +08:00
parent b818777a45
commit 953b1cf86a
12 changed files with 469 additions and 88 deletions

View File

@@ -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 {