Spaces:
Running
Running
File size: 1,700 Bytes
968ca90 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import { FileCategory, FileNode, RawFileItem } from "@/shared/types/domain";
const VIDEO_EXTS = new Set(["mp4", "mkv", "webm", "mov", "avi", "mp3", "wav", "ogg"]);
const IMAGE_EXTS = new Set(["png", "jpg", "jpeg", "gif", "webp", "svg", "bmp", "ico"]);
const ARCHIVE_EXTS = new Set(["zip", "rar", "7z", "tar", "gz", "bz2", "xz"]);
export function formatBytes(bytes: number): string {
if (!bytes || bytes <= 0) return "0 B";
const unit = 1024;
const sizes = ["B", "KB", "MB", "GB", "TB"];
const idx = Math.min(Math.floor(Math.log(bytes) / Math.log(unit)), sizes.length - 1);
return `${parseFloat((bytes / Math.pow(unit, idx)).toFixed(2))} ${sizes[idx]}`;
}
export function detectCategory(filename: string): FileCategory {
const ext = (filename.split(".").pop() || "").toLowerCase();
if (VIDEO_EXTS.has(ext)) return "video";
if (IMAGE_EXTS.has(ext)) return "image";
if (ARCHIVE_EXTS.has(ext)) return "archive";
return "doc";
}
export function normalizeFile(item: RawFileItem): FileNode {
const cleanPath = item.path.replace(/\\/g, "/").replace(/^\/+/, "");
const owner = (item.owner || "admin").toLowerCase();
let displayPath = cleanPath;
if (displayPath.toLowerCase().startsWith(`${owner}/`)) {
displayPath = displayPath.slice(owner.length + 1);
}
const segments = displayPath.split("/");
const name = segments.pop() || displayPath;
const folder = segments.join("/");
const ext = name.includes(".") ? (name.split(".").pop() || "").toLowerCase() : "";
return {
path: cleanPath,
name,
folder,
size: item.size || 0,
updatedAt: item.updated_at || "",
ext,
category: detectCategory(name),
owner: item.owner || "admin",
};
}
|