Browsers often report ZIP files with this MIME type instead of application/zip.
74 lines
1.8 KiB
TypeScript
74 lines
1.8 KiB
TypeScript
import type { BuildUploadHintOptions } from '@/types/BuildUploadHintOptions';
|
|
import { formatFileSize } from '@/utils/formatFileSize';
|
|
|
|
const MIME_LABEL: Record<string, string> = {
|
|
'application/json': 'JSON',
|
|
'application/pdf': 'PDF',
|
|
'application/zip': 'ZIP',
|
|
'application/x-zip-compressed': 'ZIP',
|
|
'image/gif': 'GIF',
|
|
'image/jpeg': 'JPEG',
|
|
'image/jpg': 'JPEG',
|
|
'image/png': 'PNG',
|
|
'image/vnd.microsoft.icon': 'ICO',
|
|
'image/x-icon': 'ICO',
|
|
'image/webp': 'WebP',
|
|
'text/csv': 'CSV',
|
|
'text/plain': 'plain text'
|
|
};
|
|
|
|
const formatTypeList = (types: string[]): string => {
|
|
if (types.length === 0) {
|
|
return '';
|
|
}
|
|
|
|
if (types.length === 1) {
|
|
return types[0];
|
|
}
|
|
|
|
if (types.length === 2) {
|
|
return `${types[0]} or ${types[1]}`;
|
|
}
|
|
|
|
return `${types.slice(0, -1).join(', ')}, or ${types.at(-1)}`;
|
|
};
|
|
|
|
export const buildUploadHint = ({
|
|
allowedMimesCsv,
|
|
maxFileBytes,
|
|
maxFiles,
|
|
maxFilesLabel,
|
|
encryptedAtRest
|
|
}: BuildUploadHintOptions): string => {
|
|
const types = [
|
|
...new Set(
|
|
allowedMimesCsv
|
|
.split(',')
|
|
.map(s => s.trim().toLowerCase())
|
|
.filter(Boolean)
|
|
.map(m => MIME_LABEL[m] ?? m)
|
|
)
|
|
];
|
|
|
|
const sizePart = formatFileSize(maxFileBytes);
|
|
const typePart = formatTypeList(types) || allowedMimesCsv.trim();
|
|
|
|
const parts: string[] = [];
|
|
|
|
if (typePart) {
|
|
parts.push(`${typePart} up to ${sizePart} each.`);
|
|
} else {
|
|
parts.push(`Up to ${sizePart} each.`);
|
|
}
|
|
|
|
if (maxFiles !== undefined && maxFilesLabel) {
|
|
parts.push(`Max ${maxFiles} ${maxFilesLabel}.`);
|
|
}
|
|
|
|
if (encryptedAtRest) {
|
|
parts.push('Encrypted at rest on the server.');
|
|
}
|
|
|
|
return parts.join(' ');
|
|
};
|