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:
@@ -8,21 +8,30 @@ export interface FileType {
|
||||
file: File
|
||||
}
|
||||
|
||||
export interface RejectedFile {
|
||||
file: File
|
||||
reason: "size" | "format"
|
||||
}
|
||||
|
||||
export interface FileUploadProps {
|
||||
label: string
|
||||
multiple?: boolean
|
||||
hint?: string
|
||||
hasError?: boolean
|
||||
formats: string[]
|
||||
onUploaded: (files: FileType[]) => void
|
||||
maxFileSize?: number // in bytes, defaults to 1MB. Set to Infinity to disable.
|
||||
onUploaded: (files: FileType[], rejectedFiles?: RejectedFile[]) => void
|
||||
}
|
||||
|
||||
const DEFAULT_MAX_FILE_SIZE = 1024 * 1024 // 1MB
|
||||
|
||||
export const FileUpload = ({
|
||||
label,
|
||||
hint,
|
||||
multiple = true,
|
||||
hasError,
|
||||
formats,
|
||||
maxFileSize = DEFAULT_MAX_FILE_SIZE,
|
||||
onUploaded,
|
||||
}: FileUploadProps) => {
|
||||
const [isDragOver, setIsDragOver] = useState<boolean>(false)
|
||||
@@ -65,18 +74,26 @@ export const FileUpload = ({
|
||||
}
|
||||
|
||||
const fileList = Array.from(files)
|
||||
const fileObj = fileList.map((file) => {
|
||||
const id = Math.random().toString(36).substring(7)
|
||||
const validFiles: FileType[] = []
|
||||
const rejectedFiles: RejectedFile[] = []
|
||||
const normalizedMaxFileSize = Math.min(maxFileSize, Infinity)
|
||||
|
||||
fileList.forEach((file) => {
|
||||
if (file.size > normalizedMaxFileSize) {
|
||||
rejectedFiles.push({ file, reason: "size" })
|
||||
return
|
||||
}
|
||||
|
||||
const id = Math.random().toString(36).substring(7)
|
||||
const previewUrl = URL.createObjectURL(file)
|
||||
return {
|
||||
validFiles.push({
|
||||
id: id,
|
||||
url: previewUrl,
|
||||
file,
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
onUploaded(fileObj)
|
||||
onUploaded(validFiles, rejectedFiles)
|
||||
}
|
||||
|
||||
const handleDrop = (event: DragEvent) => {
|
||||
|
||||
Reference in New Issue
Block a user