fix(dashboard): Limit file uploads to 1MB (#13981)

## Summary

**What** — What changes are introduced in this PR?

Limit file uploads to 1MB

**Why** — Why are these changes relevant or necessary?  

Prevent large file uploads in the Admin 

**How** — How have these changes been implemented?

Set size limits on the file uploads

**Testing** — How have these changes been tested, or how can the reviewer test the feature?

---

## Checklist

Please ensure the following before requesting a review:

- [ ] I have added a **changeset** for this PR
    - Every non-breaking change should be marked as a **patch**
    - To add a changeset, run `yarn changeset` and follow the prompts
- [ ] The changes are covered by relevant **tests**
- [ ] I have verified the code works as intended locally
- [ ] I have linked the related issue(s) if applicable

---

## Additional Context

CLOSES CORE-1270


---

> [!NOTE]
> Adds a 1MB default file size limit to `FileUpload`, surfaces size/type rejections in media forms via new i18n, and allows unlimited size for product import.
> 
> - **Components**:
>   - `components/common/file-upload/file-upload.tsx`:
>     - Add `maxFileSize` (default 1MB) and size validation; return `rejectedFiles` alongside valid files.
> - **Products › Media Upload**:
>   - `upload-media-form-item.tsx`:
>     - Handle `rejectedFiles` and set form errors for invalid type and oversized files.
> - **Products › Import**:
>   - `upload-import.tsx`: Use `maxFileSize={Infinity}` to disable size limit for CSV import.
> - **i18n**:
>   - Schema and EN translations: add `products.media.fileTooLarge` message.
> 
> <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit c8c67f4d329f8767e99694649bf0b3fe4cf400e9. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup>
This commit is contained in:
juanzgc
2025-11-06 09:57:24 -05:00
committed by GitHub
parent 3ca1e1df33
commit e42e6f0daa
5 changed files with 53 additions and 13 deletions

View File

@@ -5,6 +5,7 @@ import { z } from "zod"
import {
FileType,
FileUpload,
RejectedFile,
} from "../../../../../components/common/file-upload"
import { Form } from "../../../../../components/common/form"
import { MediaSchema } from "../../../product-create/constants"
@@ -47,11 +48,13 @@ export const UploadMediaFormItem = ({
const { t } = useTranslation()
const hasInvalidFiles = useCallback(
(fileList: FileType[]) => {
(fileList: FileType[] = [], rejectedFiles: RejectedFile[] = []) => {
const invalidFile = fileList.find(
(f) => !SUPPORTED_FORMATS.includes(f.file.type)
(f) => !SUPPORTED_FORMATS.includes(f?.file?.type)
)
let hasInvalidFile = false;
if (invalidFile) {
form.setError("media", {
type: "invalid_file",
@@ -61,22 +64,37 @@ export const UploadMediaFormItem = ({
}),
})
return true
hasInvalidFile = true;
}
return false
const fileSizeRejections = rejectedFiles.filter((f) => f?.reason === "size")
if (fileSizeRejections.length) {
const fileNames = fileSizeRejections.map((f) => f.file.name).join(", ")
form.setError("media", {
type: "file_too_large",
message: t("products.media.fileTooLarge", {
name: fileNames,
size: "1MB",
}),
})
hasInvalidFile = true;
}
return hasInvalidFile;
},
[form, t]
)
const onUploaded = useCallback(
(files: FileType[]) => {
(files: FileType[] = [], rejectedFiles: RejectedFile[] = []) => {
form.clearErrors("media")
if (hasInvalidFiles(files)) {
if (hasInvalidFiles(files, rejectedFiles)) {
return
}
files.forEach((f) => append({ ...f, isThumbnail: false }))
files?.forEach((f) => append({ ...f, isThumbnail: false }))
},
[form, append, hasInvalidFiles]
)

View File

@@ -48,6 +48,7 @@ export const UploadImport = ({
}
onUploaded(files[0].file)
}}
maxFileSize={Infinity}
/>
{error && (