mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Move request limiter to middleware.
This commit is contained in:
@@ -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")
|
||||
|
Reference in New Issue
Block a user