43 lines
1.6 KiB
JavaScript
43 lines
1.6 KiB
JavaScript
const TYPE_LABELS = {
|
|
"application/pdf": "PDF",
|
|
"application/msword": "Word",
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "Word",
|
|
"application/vnd.ms-excel": "Excel",
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "Excel",
|
|
"application/vnd.ms-powerpoint": "PowerPoint",
|
|
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "PowerPoint",
|
|
"application/zip": "ZIP",
|
|
"application/json": "JSON",
|
|
"text/plain": "Text",
|
|
"text/csv": "CSV",
|
|
"image/jpeg": "JPEG",
|
|
"image/jpg": "JPEG",
|
|
"image/png": "PNG",
|
|
"image/gif": "GIF",
|
|
"image/webp": "WebP",
|
|
"image/svg+xml": "SVG",
|
|
};
|
|
|
|
export function formatFileSize(bytes) {
|
|
const size = Number(bytes);
|
|
if (!Number.isFinite(size) || size <= 0) return "";
|
|
if (size < 1024) return `${size} B`;
|
|
if (size < 1024 * 1024) return `${Math.round(size / 1024)} KB`;
|
|
if (size < 1024 * 1024 * 1024) return `${(size / (1024 * 1024)).toFixed(1)} MB`;
|
|
return `${(size / (1024 * 1024 * 1024)).toFixed(1)} GB`;
|
|
}
|
|
|
|
export function fileTypeLabel(type, filename = "") {
|
|
const mime = String(type || "").toLowerCase();
|
|
if (TYPE_LABELS[mime]) return TYPE_LABELS[mime];
|
|
if (mime.startsWith("image/")) return mime.slice(6).toUpperCase();
|
|
if (mime.startsWith("video/")) return "Video";
|
|
if (mime.startsWith("audio/")) return "Audio";
|
|
const ext = String(filename).split(".").pop();
|
|
if (ext && ext !== filename && ext.length <= 5) return ext.toUpperCase();
|
|
return "File";
|
|
}
|
|
|
|
export function fileMetaLine(type, size, filename = "") {
|
|
return [fileTypeLabel(type, filename), formatFileSize(size)].filter(Boolean).join(" · ");
|
|
}
|