-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
388 lines (318 loc) · 10.5 KB
/
main.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// - Variables
const uploadElement: Element | null = document.querySelector(".upload-main");
const loader: Element | null = document.querySelector(".loader");
const latest: HTMLElement | null = document.querySelector("#latest");
const historyElement: HTMLElement | null = document.querySelector("#history");
const dropdown: HTMLElement | null = document.querySelector(".dropdown");
const dropdownButton: HTMLButtonElement | null =
document.querySelector(".dropdown button");
const dropdownContent: HTMLUListElement | null =
document.querySelector(".dropdown ul");
const popup: HTMLElement | null = document.querySelector(".popup");
const popupButton: HTMLButtonElement | null = document.querySelector(
".popup .content button",
);
const overlay: HTMLElement | null = document.querySelector(".overlay");
let host: string = "fileio";
// - Create logs
let logs: string[] = [];
function log(type: string, message: any) {
logs.push(
`<span style="color: #a084e8;">[${type}]</span> <span style="color: #80C4E9;">[${new Date().toLocaleString()}]</span> - ${String(
message,
)}\n`,
);
}
// - Open And Close Logs
function closeLogs(): void {
const modal = document.querySelector(".modal") as HTMLElement | null;
if (modal) {
modal.style.display = "none";
document.body.classList.toggle("no-scroll");
}
}
async function openLogs(): Promise<void> {
document.body.classList.toggle("no-scroll");
const modal = document.querySelector(".modal") as HTMLElement | null;
if (modal) {
modal.style.display = "block";
const frontendLogs = logs.join("").replaceAll("\n", "<br>");
const leftDiv = document.querySelector(
".modal .content .left div",
) as HTMLElement;
leftDiv.innerHTML =
frontendLogs === "" ? '<p class="none">NO LOGS YET!</p>' : frontendLogs;
try {
const response = await fetch("/logs");
const data = await response.json();
const backendLogs = String(data.output.join(""))
.replace(/^(.*[^\-])$/gm, "<span>$1</span>")
.replaceAll("\n", "<br>");
const rightDiv = document.querySelector(
".modal .content .right div",
) as HTMLElement;
rightDiv.innerHTML =
backendLogs === "" ? '<p class="none">NO LOGS YET!</p>' : backendLogs;
} catch (error) {
log("ERROR", error);
}
}
}
// - Dropdown
if (dropdownButton && dropdownContent && dropdown) {
dropdownButton.addEventListener("click", () => {
if (dropdownContent.style.display === "grid") {
dropdownContent.style.display = "none";
dropdown.classList.toggle("active");
} else {
dropdownContent.style.display = "grid";
dropdown.classList.toggle("active");
}
});
}
// - Change Host
function changeHost(name: string): void {
host = name;
const selectedElement = document.querySelector(".selected");
if (selectedElement) {
selectedElement.classList.remove("selected");
}
const hostElement = document.querySelector(`.host-${name}`);
if (hostElement) {
hostElement.classList.add("selected");
}
}
// - Upload File
async function uploadFile(event: Event): Promise<void> {
event.preventDefault();
let files: FileList | null = null;
if (event.type === "change") {
files = (event.target as HTMLInputElement).files;
} else if (event.type === "drop") {
const dropEvent = event as DragEvent;
files = dropEvent.dataTransfer?.files;
}
if (!files || files.length === 0) {
log("NORMAL", "No files selected/dropped.");
return;
}
const uploadElement = document.querySelector(".upload-main") as HTMLElement;
const loader = document.querySelector(".loader") as HTMLElement;
if (uploadElement) uploadElement.style.display = "none";
if (loader) loader.style.display = "flex";
const uploadPromises: Promise<any>[] = [];
for (const file of Array.from(files)) {
const formData = new FormData();
formData.append("file", file);
const url = "/upload" + `?host=${host}`;
const uploadPromise = fetch(url, {
method: "POST",
body: formData,
}).then((response) => response.json());
uploadPromises.push(uploadPromise);
}
try {
const uploadResponses = await Promise.all(uploadPromises);
if (uploadElement) uploadElement.style.display = "flex";
if (loader) loader.style.display = "none";
let total_content = "";
for (let i = 0; i < files.length; i++) {
const file = files[i];
const data = uploadResponses[i];
if (data.error) {
if (uploadElement) uploadElement.style.display = "flex";
if (loader) loader.style.display = "none";
}
let content = `<li><p class="t">Name: </p><p class="c">${file.name}</p></li>`;
Object.keys(data).forEach((key) => {
content += `<li><p class="t">${key}: </p><p class="c">${data[key]}</p></li>`;
});
total_content += `
<div class="card">
<ul>
${content}
</ul>
</div>`;
}
const historyNoneElement = document.querySelector("#history .none");
if (historyNoneElement) {
historyNoneElement.remove();
}
const latest = document.querySelector("#latest");
const historyElement = document.querySelector("#history");
if (latest) latest.innerHTML = total_content;
if (historyElement) historyElement.innerHTML += total_content;
log(
"SUCCESS",
`File(s) uploaded successfully: ${JSON.stringify(
uploadResponses,
null,
4,
)}`,
);
} catch (error) {
if (uploadElement) uploadElement.style.display = "flex";
if (loader) loader.style.display = "none";
log("ERROR", `Error uploading file(s): ${String(error)}`);
}
}
// - Drag & Drop
function allowDrop(event: DragEvent): void {
event.preventDefault();
document.body.classList.add("dragging");
}
function dragLeave(): void {
document.body.classList.remove("dragging");
}
document.querySelectorAll(".dropzone").forEach((zone): void => {
zone.addEventListener("drop", uploadFile);
zone.addEventListener("dragover", allowDrop);
zone.addEventListener("dragleave", dragLeave);
});
// - File Input
const fileInput = document.getElementById("upload-file") as HTMLInputElement;
fileInput.addEventListener("change", (event: Event) => {
uploadFile(event);
});
// - Clear History
function clearHistory(): void {
fetch("/clear", {
method: "DELETE",
})
.then((res) => res.json())
.then((data) => {
if (data.success) {
const historyElement = document.querySelector("#history");
if (historyElement) {
historyElement.innerHTML = `<div class="none flex-1"><p>No history!</p></div>`;
}
}
})
.catch((error) => {
log("ERROR", error);
});
}
// - Add host
function addHost(name: string, access: boolean): void {
fetch(`/add-host/${name}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
get_link_from_json: access.toString(),
}),
}).then(() => {
window.location.reload();
});
}
// - Add host DOM
if (popupButton) {
popupButton.addEventListener("click", () => {
const newNameInput = document.querySelector<HTMLInputElement>(
".popup #new-host-name",
);
const newLambdaInput = document.querySelector<HTMLInputElement>(
".popup #new-host-lambda",
);
if (newNameInput && newLambdaInput) {
addHost(newNameInput.value, newLambdaInput.value === "true");
}
});
}
// - Toggle add host popup
function toggleAddHostPopup(): void {
const popup = document.querySelector<HTMLElement>(".popup");
const overlay = document.querySelector<HTMLElement>(".overlay");
if (popup && overlay) {
if (popup.classList.contains("active")) {
popup.classList.remove("active");
overlay.classList.remove("active");
} else {
popup.classList.add("active");
overlay.classList.add("active");
}
}
}
// - Accordin
const accordin = document.querySelectorAll<HTMLElement>(".accordin");
const accordinContent =
document.querySelectorAll<HTMLElement>(".accordin .content");
const accordinButton =
document.querySelectorAll<HTMLButtonElement>(".accordin button");
for (let i = 0; i < accordinButton.length; i++) {
accordinButton[i].addEventListener("click", () => {
if (accordinContent[i].style.display === "block") {
accordinContent[i].style.display = "none";
accordin[i].classList.remove("active");
} else {
accordinContent[i].style.display = "block";
accordin[i].classList.add("active");
}
});
}
// - History
function loadHistory(): void {
fetch("/history")
.then((res) => res.json())
.then((data: Record<string, string> | string[]) => {
const historyElement = document.querySelector("#history");
if (Array.isArray(data) || Object.keys(data).length === 0) {
historyElement.innerHTML = `<div class="none flex-1"><p>No history!</p></div>`;
} else {
Object.keys(data).forEach((key) => {
historyElement.innerHTML += `
<div class="card">
<ul>
<li><p class="t">Name: </p><p class="c">${key}</p></li>
<li><p class="t">Link: </p><p class="c">${data[key]}</p></li>
</ul>
</div>
`;
});
}
})
.catch((error) => {
log("ERROR", error);
});
}
loadHistory();
// - Load hosts in dropdown
fetch("/hosts")
.then((res) => res.json())
.then((data: string[]) => {
const dropdownContent = document.querySelector(".dropdown ul");
if (dropdownContent) {
data.forEach((key) => {
dropdownContent.innerHTML += `
<li><button class="${
host === key ? "selected" : ""
} host-${key}" onclick="changeHost('${key}')">${key}</button></li>
`;
});
dropdownContent.innerHTML += `
<li><button class="add" onclick="toggleAddHostPopup()"><span>+</span> Add</button></li>
`;
}
})
.catch((error) => {
log("ERROR", error);
});
// - API Location
const urlOrigin: string = window.location.origin + "/";
document.querySelectorAll(".location").forEach((e: Element) => {
if (e instanceof HTMLElement) {
e.innerText = urlOrigin;
}
});
// - Prevent Default
document.addEventListener("contextmenu", (e: Event) => e.preventDefault());
function ctrlShiftKey(e: KeyboardEvent, keyCode: string): boolean {
return e.ctrlKey && e.shiftKey && e.key === keyCode;
}
document.onkeydown = (e: KeyboardEvent) => {
if (e.key === "F12" || (e.ctrlKey && e.shiftKey)) {
e.preventDefault();
return false;
}
};