Add admin config page and runtime-tunable upload/rate-limit settings
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"ResendIt/internal/config"
|
||||
"ResendIt/internal/util"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -12,11 +13,19 @@ import (
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
service *Service
|
||||
configService ConfigService
|
||||
}
|
||||
|
||||
func NewHandler(s *Service) *Handler {
|
||||
return &Handler{service: s}
|
||||
// ConfigService is the small interface we need from the config package.
|
||||
// Keeping it as an interface avoids import cycles and keeps file.Handler easy to test.
|
||||
type ConfigService interface {
|
||||
GetIntDefault(key string, def int) int
|
||||
GetInt64Default(key string, def int64) int64
|
||||
}
|
||||
|
||||
func NewHandler(s *Service, cfg ConfigService) *Handler {
|
||||
return &Handler{service: s, configService: cfg}
|
||||
}
|
||||
|
||||
func (h *Handler) Upload(c *gin.Context) {
|
||||
@@ -32,6 +41,12 @@ func (h *Handler) Upload(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
maxSize := h.configService.GetInt64Default(config.KeyUploadMaxFileSizeBytes, config.DefaultUploadMaxFileSizeBytes)
|
||||
if file.Size > maxSize {
|
||||
c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": "file too large"})
|
||||
return
|
||||
}
|
||||
|
||||
f, err := file.Open()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "cannot open file"})
|
||||
@@ -46,6 +61,10 @@ func (h *Handler) Upload(c *gin.Context) {
|
||||
if err != nil || hours <= 0 {
|
||||
hours = 24 // default
|
||||
}
|
||||
maxHours := h.configService.GetIntDefault(config.KeyUploadMaxHours, config.DefaultUploadMaxHours)
|
||||
if hours > maxHours {
|
||||
hours = maxHours
|
||||
}
|
||||
|
||||
duration := time.Duration(hours) * time.Hour
|
||||
|
||||
|
||||
Reference in New Issue
Block a user