Move request limiter to middleware.

This commit is contained in:
2025-07-09 17:00:39 +08:00
parent 0021a73951
commit b568b234c4
11 changed files with 73 additions and 71 deletions

View File

@@ -3,7 +3,6 @@ package service
import (
"bytes"
"errors"
"github.com/disintegration/imaging"
"image"
"math"
"net/http"
@@ -14,14 +13,17 @@ import (
"strconv"
"time"
"github.com/disintegration/imaging"
"github.com/gofiber/fiber/v3/log"
"github.com/google/uuid"
_ "golang.org/x/image/bmp"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/webp"
"github.com/chai2010/webp"
@@ -54,24 +56,12 @@ func init() {
}()
}
var (
imageLimiter = utils.NewRequestLimiter(maxUploadsPerIP, 24*time.Hour)
)
const maxUploadsPerIP = 100
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 {
// For a normal user, check the IP upload limit
if !imageLimiter.AllowRequest(ip) {
return 0, model.NewUnAuthorizedError("You have reached the maximum upload limit")
}
}
if len(data) == 0 {
return 0, model.NewRequestError("Image data is empty")