feat: 添加可配置的文件上传大小限制
新增 MAX_FILE_SIZE_MB 环境变量统一控制文件上传大小,默认 50MB
This commit is contained in:
@@ -1823,10 +1823,11 @@ func (h *InitializationHandler) TestMultimodalFunction(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证文件大小 (10MB)
|
||||
if header.Size > 10*1024*1024 {
|
||||
// 验证文件大小 (default 50MB, configurable via MAX_FILE_SIZE_MB)
|
||||
maxSize := utils.GetMaxFileSize()
|
||||
if header.Size > maxSize {
|
||||
logger.Error(ctx, "File size too large")
|
||||
c.Error(errors.NewBadRequestError("图片文件大小不能超过10MB"))
|
||||
c.Error(errors.NewBadRequestError(fmt.Sprintf("图片文件大小不能超过%dMB", utils.GetMaxFileSizeMB())))
|
||||
return
|
||||
}
|
||||
logger.Infof(ctx, "Processing image: %s", utils.SanitizeForLog(header.Filename))
|
||||
|
||||
@@ -118,6 +118,14 @@ func (h *KnowledgeHandler) CreateKnowledgeFromFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Validate file size (configurable via MAX_FILE_SIZE_MB)
|
||||
maxSize := secutils.GetMaxFileSize()
|
||||
if file.Size > maxSize {
|
||||
logger.Error(ctx, "File size too large")
|
||||
c.Error(errors.NewBadRequestError(fmt.Sprintf("文件大小不能超过%dMB", secutils.GetMaxFileSizeMB())))
|
||||
return
|
||||
}
|
||||
|
||||
// Get custom filename if provided (for folder uploads with path)
|
||||
customFileName := c.PostForm("fileName")
|
||||
customFileName = secutils.SanitizeForLog(customFileName)
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GetMaxFileSize returns the maximum file upload size in bytes.
|
||||
// Default is 50MB, can be configured via MAX_FILE_SIZE_MB environment variable.
|
||||
func GetMaxFileSize() int64 {
|
||||
if sizeStr := os.Getenv("MAX_FILE_SIZE_MB"); sizeStr != "" {
|
||||
if size, err := strconv.ParseInt(sizeStr, 10, 64); err == nil && size > 0 {
|
||||
return size * 1024 * 1024
|
||||
}
|
||||
}
|
||||
return 50 * 1024 * 1024 // default 50MB
|
||||
}
|
||||
|
||||
// GetMaxFileSizeMB returns the maximum file upload size in MB.
|
||||
func GetMaxFileSizeMB() int64 {
|
||||
if sizeStr := os.Getenv("MAX_FILE_SIZE_MB"); sizeStr != "" {
|
||||
if size, err := strconv.ParseInt(sizeStr, 10, 64); err == nil && size > 0 {
|
||||
return size
|
||||
}
|
||||
}
|
||||
return 50 // default 50MB
|
||||
}
|
||||
Reference in New Issue
Block a user