Spaces:
Running
Running
Download frontend/src/lib/api.ts from TwanAPI/UploadFile: direct link, hf CLI and curl.
- Browser
- Download file 2.88 kB
-
https://huggingface.co/spaces/TwanAPI/UploadFile/resolve/main/frontend/src/lib/api.ts
- Command line
-
hf download hf://spaces/TwanAPI/UploadFile/frontend/src/lib/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/TwanAPI/UploadFile/resolve/main/frontend/src/lib/api.ts
2.88 kB
| import { FileNode, RawFileItem } from "@/types/cloud"; | |
| export function getBackendOrigin(): string { | |
| if (typeof window !== "undefined") { | |
| if (window.location.port === "3000") { | |
| return `${window.location.protocol}//${window.location.hostname}:8080`; | |
| } | |
| return window.location.origin; | |
| } | |
| return process.env.BACKEND_URL || "http://127.0.0.1:8080"; | |
| } | |
| export async function apiFetch(path: string, init?: RequestInit): Promise<Response> { | |
| return fetch(`${getBackendOrigin()}${path}`, init); | |
| } | |
| export function formatBytes(bytes: number): string { | |
| if (!bytes || bytes <= 0) return "0 B"; | |
| const k = 1024; | |
| const sizes = ["B", "KB", "MB", "GB", "TB"]; | |
| const i = Math.floor(Math.log(bytes) / Math.log(k)); | |
| return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i]; | |
| } | |
| export function detectCategory(filename: string): "video" | "image" | "archive" | "doc" { | |
| const ext = (filename.split(".").pop() || "").toLowerCase(); | |
| if (["mp4", "mkv", "webm", "mov", "avi", "mp3", "wav", "ogg"].includes(ext)) return "video"; | |
| if (["png", "jpg", "jpeg", "gif", "webp", "svg", "bmp", "ico"].includes(ext)) return "image"; | |
| if (["zip", "rar", "7z", "tar", "gz", "bz2", "xz"].includes(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 parts = displayPath.split("/"); | |
| const name = parts.pop() || displayPath; | |
| const folder = parts.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", | |
| }; | |
| } | |
| export function buildDownloadUrl(path: string, token: string, inline = false): string { | |
| const params = new URLSearchParams({ path }); | |
| if (token) params.set("token", token); | |
| if (inline) params.set("inline", "1"); | |
| return `${getBackendOrigin()}/api/download?${params.toString()}`; | |
| } | |
| export function buildFrontendShareUrl(shareId: string): string { | |
| if (typeof window !== "undefined") { | |
| return `${window.location.origin}/share?id=${encodeURIComponent(shareId)}`; | |
| } | |
| return `/share?id=${encodeURIComponent(shareId)}`; | |
| } | |
| export function buildPublicShareDownloadUrl(shareId: string, visitorToken: string, inline = false): string { | |
| const params = new URLSearchParams({ id: shareId }); | |
| if (visitorToken) params.set("visitor_token", visitorToken); | |
| if (inline) params.set("inline", "1"); | |
| return `${getBackendOrigin()}/api/public/share/download?${params.toString()}`; | |
| } | |