add 20mb max file size

This commit is contained in:
harold 2025-06-30 23:01:37 +05:00
parent efd2cb04ee
commit 66a546b465

View File

@ -6,6 +6,7 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"log/slog" "log/slog"
"net/http" "net/http"
"strings"
) )
// AddNewFile godoc // AddNewFile godoc
@ -15,11 +16,12 @@ import (
// @Accept multipart/form-data // @Accept multipart/form-data
// @Produce json // @Produce json
// @Security BearerAuth // @Security BearerAuth
// @Param new_file formData file true "New file to upload" // @Param new_file formData file true "New file to upload (max 20 MB)"
// @Param entity formData string false "Optional entity type (defaults to 'widget')" // @Param entity formData string false "Optional entity type (defaults to 'widget')"
// @Success 200 {object} model.DataFile "File has been uploaded successfully!" // @Success 200 {object} model.DataFile "File has been uploaded successfully!"
// @Failure 400 {object} echo.HTTPError "Bad request (e.g., missing file, invalid form data)" // @Failure 400 {object} echo.HTTPError "Bad request (e.g., missing file, invalid form data)"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token" // @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
// @Failure 413 {object} echo.HTTPError "File size exceeds 20 MB limit"
// @Failure 422 {object} echo.HTTPError "Validation error (specific cases, might overlap with 400/500)" // @Failure 422 {object} echo.HTTPError "Validation error (specific cases, might overlap with 400/500)"
// @Failure 500 {object} echo.HTTPError "Internal server error (e.g., failed to save file, DB error)" // @Failure 500 {object} echo.HTTPError "Internal server error (e.g., failed to save file, DB error)"
// @Router /files [post] // @Router /files [post]
@ -38,16 +40,28 @@ func AddNewFile(fileService model.FileService, donatService model.DonatService)
slog.Info("Entity parameter not provided, defaulting to 'widget'") slog.Info("Entity parameter not provided, defaulting to 'widget'")
} }
const maxFileSize = 20 << 20 // 20 MB
request.Request().Body = http.MaxBytesReader(request.Response(), request.Request().Body, maxFileSize)
newFile, err := request.FormFile("new_file") newFile, err := request.FormFile("new_file")
if err != nil { if err != nil {
if err == http.ErrMissingFile { if err == http.ErrMissingFile {
slog.Error("Missing 'new_file' in form data", "error", err) slog.Error("Missing 'new_file' in form data", "error", err)
return echo.NewHTTPError(http.StatusBadRequest, "Required form field 'new_file' is missing") return echo.NewHTTPError(http.StatusBadRequest, "Required form field 'new_file' is missing")
} }
if strings.Contains(err.Error(), "request body too large") {
slog.Error("File size exceeds 20 MB limit", "error", err)
return echo.NewHTTPError(http.StatusRequestEntityTooLarge, "File size exceeds 20 MB limit")
}
slog.Error("Failed to get file from form", "error", err) slog.Error("Failed to get file from form", "error", err)
return echo.NewHTTPError(http.StatusBadRequest, "Cannot process uploaded file: "+err.Error()) return echo.NewHTTPError(http.StatusBadRequest, "Cannot process uploaded file: "+err.Error())
} }
if newFile.Size > maxFileSize {
slog.Error("File size exceeds 20 MB limit", "size", newFile.Size)
return echo.NewHTTPError(http.StatusRequestEntityTooLarge, "File size exceeds 20 MB limit")
}
fileId, err := fileService.AddNewFile( fileId, err := fileService.AddNewFile(
ctx, ctx,
*newFile, *newFile,