From b02b165b3db56ff4ec4c1a68d58d03c1db57c428 Mon Sep 17 00:00:00 2001 From: Iu Date: Sat, 10 Oct 2026 19:15:43 +0300 Subject: [PATCH] fix(upload): clear error and higher limit for large files - raise upload limit to 100 MB - return 413 file_too_large on MaxBytesError instead of misleading "expected multipart form" - client-side per-file and total size pre-check with a clear message --- backend/internal/photos/handlers.go | 10 ++++++++-- frontend/src/lib/components/Uploader.svelte | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/backend/internal/photos/handlers.go b/backend/internal/photos/handlers.go index 671bfbe..5c90772 100644 --- a/backend/internal/photos/handlers.go +++ b/backend/internal/photos/handlers.go @@ -1,6 +1,7 @@ package photos import ( + "errors" "io" "net/http" "strconv" @@ -12,7 +13,7 @@ import ( "shared-album/internal/httpx" ) -const maxUploadSize = 50 << 20 +const maxUploadSize = 100 << 20 type Handler struct { svc *Service @@ -43,7 +44,12 @@ func (h *Handler) Upload(c *gin.Context) { c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxUploadSize) form, err := c.MultipartForm() if err != nil { - httpx.RespondError(c, httpx.BadRequest("invalid_request", "expected multipart form")) + var maxErr *http.MaxBytesError + if errors.As(err, &maxErr) { + httpx.RespondError(c, httpx.NewError(http.StatusRequestEntityTooLarge, "file_too_large", "files exceed the maximum upload size (100 MB)")) + return + } + httpx.RespondError(c, httpx.BadRequest("invalid_request", "invalid multipart form")) return } files := form.File["files"] diff --git a/frontend/src/lib/components/Uploader.svelte b/frontend/src/lib/components/Uploader.svelte index 0e7eec0..0de5fce 100644 --- a/frontend/src/lib/components/Uploader.svelte +++ b/frontend/src/lib/components/Uploader.svelte @@ -14,6 +14,8 @@ let progress = $state(0); let error = $state(''); + const MAX_SIZE = 100 * 1024 * 1024; + export function pick() { fileInput?.click(); } @@ -21,6 +23,16 @@ export function upload(files: FileList | File[]) { const list = Array.from(files); if (list.length === 0) return; + const tooBig = list.filter((f) => f.size > MAX_SIZE); + if (tooBig.length > 0) { + error = `Файл слишком большой: ${tooBig.map((f) => f.name).join(', ')}. Максимум 100 МБ.`; + return; + } + const total = list.reduce((sum, f) => sum + f.size, 0); + if (total > MAX_SIZE) { + error = 'Суммарный размер файлов превышает 100 МБ. Загрузите меньшими порциями.'; + return; + } error = ''; busy = true; progress = 0;