-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
161 lines (143 loc) · 4.4 KB
/
api.ts
File metadata and controls
161 lines (143 loc) · 4.4 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const API_BASE = process.env.NEXT_PUBLIC_API_URL || "http://127.0.0.1:8787";
export interface UploadInitRequest {
filename: string;
size: number;
mime?: string;
expires_in?: number;
max_downloads?: number;
password?: string;
}
export interface UploadInitResponse {
upload_url: string;
file_uuid: string;
expires_at: number;
}
export interface UploadFinalizeResponse {
download_link: string;
status: "active";
}
export interface FileInfoResponse {
filename: string;
size: number;
mime: string;
uploaded_at: number;
expires_at: number;
downloads_left: number | null;
has_password: boolean;
}
export interface ApiError {
error: string;
code: string;
}
export async function initUpload(
data: UploadInitRequest
): Promise<UploadInitResponse> {
const res = await fetch(`${API_BASE}/api/v1/upload/init`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
if (!res.ok) {
const err: ApiError = await res.json();
throw new Error(err.error);
}
return res.json();
}
export async function uploadFile(
uploadUrl: string,
file: Blob,
onProgress?: (percent: number) => void
): Promise<void> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("PUT", uploadUrl);
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.upload.addEventListener("progress", (e) => {
if (e.lengthComputable && onProgress) {
onProgress(Math.round((e.loaded / e.total) * 100));
}
});
xhr.addEventListener("load", () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve();
} else {
reject(new Error(`Upload failed with status ${xhr.status}`));
}
});
xhr.addEventListener("error", () => reject(new Error("Upload failed")));
xhr.addEventListener("abort", () => reject(new Error("Upload aborted")));
xhr.send(file);
});
}
export async function finalizeUpload(
fileUuid: string
): Promise<UploadFinalizeResponse> {
const res = await fetch(`${API_BASE}/api/v1/upload/finalize`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ file_uuid: fileUuid }),
});
if (!res.ok) {
const err: ApiError = await res.json();
throw new Error(err.error);
}
return res.json();
}
export async function getFileInfo(uuid: string): Promise<FileInfoResponse> {
const res = await fetch(`${API_BASE}/api/v1/file/${uuid}/info`);
if (!res.ok) {
const err: ApiError = await res.json();
throw new Error(err.error);
}
return res.json();
}
export async function downloadFile(
uuid: string,
password?: string
): Promise<{ ok: true; blob: Blob; filename: string } | { ok: false; error: string; code: string }> {
const url = getDownloadUrl(uuid, password);
const res = await fetch(url);
if (!res.ok) {
try {
const err: ApiError = await res.json();
return { ok: false, error: err.error, code: err.code };
} catch {
return { ok: false, error: "Download failed", code: "UNKNOWN" };
}
}
const blob = await res.blob();
const disposition = res.headers.get("Content-Disposition") || "";
let filename = "";
if (disposition) {
const filenameMatch = disposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/i);
if (filenameMatch && filenameMatch[1]) {
filename = filenameMatch[1].replace(/['"]/g, '');
try {
filename = decodeURIComponent(filename);
} catch {
// Keep as-is if decoding fails
}
}
}
return { ok: true, blob, filename };
}
export function getDownloadUrl(uuid: string, password?: string): string {
const params = password ? `?password=${encodeURIComponent(password)}` : "";
return `${API_BASE}/api/v1/file/${uuid}/download${params}`;
}
export function formatBytes(bytes: number): string {
if (bytes === 0) return "0 B";
const k = 1024;
const sizes = ["B", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(1))} ${sizes[i]}`;
}
export function formatTimeLeft(expiresAt: number): string {
const now = Math.floor(Date.now() / 1000);
const diff = expiresAt - now;
if (diff <= 0) return "Expired";
if (diff < 60) return `${diff}s`;
if (diff < 3600) return `${Math.floor(diff / 60)}m`;
if (diff < 86400) return `${Math.floor(diff / 3600)}h`;
return `${Math.floor(diff / 86400)}d`;
}