Spaces:
Running
Running
File size: 2,876 Bytes
7c5da52 | 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 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()}`;
}
|