feat: improve error messages for user upload size validation

This commit is contained in:
Kirill
2026-05-15 20:19:00 +05:00
parent d73d88d034
commit dc448d6538
3 changed files with 14 additions and 1 deletions
@@ -1,4 +1,5 @@
import { apiClient } from '@/shared/api/client' import { apiClient } from '@/shared/api/client'
import { OTHER_UPLOAD_MAX_FILE_BYTES, formatOtherUploadMaxSizeHint } from '@/shared/constants/upload-limits'
export async function postProductReview( export async function postProductReview(
productId: string, productId: string,
@@ -8,6 +9,10 @@ export async function postProductReview(
} }
export async function uploadReviewImage(file: File): Promise<{ url: string }> { export async function uploadReviewImage(file: File): Promise<{ url: string }> {
if (file.size > OTHER_UPLOAD_MAX_FILE_BYTES) {
throw new Error(`Файл «${file.name}» слишком большой (максимум ${formatOtherUploadMaxSizeHint()}).`)
}
const fd = new FormData() const fd = new FormData()
fd.append('file', file, file.name) fd.append('file', file, file.name)
const { data } = await apiClient.post<{ url: string }>('reviews/upload-image', fd) const { data } = await apiClient.post<{ url: string }>('reviews/upload-image', fd)
@@ -128,7 +128,9 @@ export function ReviewDialog({
)} )}
{uploadError ? ( {uploadError ? (
<Alert severity="error" sx={{ mt: 2 }}> <Alert severity="error" sx={{ mt: 2 }}>
Не удалось загрузить фото. Разрешены png, jpg, jpeg, webp. {uploadError instanceof Error
? uploadError.message
: 'Не удалось загрузить фото. Разрешены png, jpg, jpeg, webp.'}
</Alert> </Alert>
) : null} ) : null}
{error ? ( {error ? (
@@ -2,6 +2,12 @@ import { ADMIN_UPLOAD_IMAGE_MAX_FILE_BYTES_DEFAULT } from '@shared/constants/upl
export const ADMIN_UPLOAD_IMAGE_MAX_BYTES = ADMIN_UPLOAD_IMAGE_MAX_FILE_BYTES_DEFAULT export const ADMIN_UPLOAD_IMAGE_MAX_BYTES = ADMIN_UPLOAD_IMAGE_MAX_FILE_BYTES_DEFAULT
export const OTHER_UPLOAD_MAX_FILE_BYTES = 2 * 1024 * 1024 // 2 MB
export function formatAdminImageMaxSizeHint(): string { export function formatAdminImageMaxSizeHint(): string {
return `${Math.round(ADMIN_UPLOAD_IMAGE_MAX_BYTES / (1024 * 1024))} МБ` return `${Math.round(ADMIN_UPLOAD_IMAGE_MAX_BYTES / (1024 * 1024))} МБ`
} }
export function formatOtherUploadMaxSizeHint(): string {
return `${Math.round(OTHER_UPLOAD_MAX_FILE_BYTES / (1024 * 1024))} МБ`
}