Spaces:
Running
Running
Download frontend/src/shared/lib/format.ts from TwanAPI/UploadFile: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/spaces/TwanAPI/UploadFile/resolve/main/frontend/src/shared/lib/format.ts
- Command line
-
hf download hf://spaces/TwanAPI/UploadFile/frontend/src/shared/lib/format.ts
-
curl -L -o format.ts https://huggingface.co/spaces/TwanAPI/UploadFile/resolve/main/frontend/src/shared/lib/format.ts
1.7 kB
| 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", | |
| }; | |
| } | |