-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
132 lines (117 loc) · 3.71 KB
/
app.js
File metadata and controls
132 lines (117 loc) · 3.71 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
import { upload } from "https://esm.sh/@vercel/blob/client";
const form = document.getElementById("upload-form");
const fileInput = document.getElementById("zip-file");
const fileMeta = document.getElementById("file-meta");
const statusEl = document.getElementById("status");
const resultBox = document.getElementById("result");
const resultToken = document.getElementById("result-token");
const submitBtn = document.getElementById("submit-btn");
const isLocalFile = window.location.protocol === "file:";
const apiUrl = isLocalFile ? "http://localhost:8000/api/decode" : "/api/decode";
const MAX_UPLOAD_BYTES = 150 * 1024 * 1024;
const ZIP_NAME_REGEX = /\.zip$/i;
const isZipFile = (file) => ZIP_NAME_REGEX.test(file.name || "");
const setStatus = (message, tone = "muted") => {
statusEl.textContent = message;
statusEl.dataset.tone = tone;
};
const resetResult = () => {
resultBox.hidden = true;
resultToken.textContent = "";
};
if (isLocalFile) {
setStatus("Local mode: start the server with python local_server.py");
}
fileInput.addEventListener("change", () => {
const file = fileInput.files[0];
if (!file) {
fileMeta.textContent = "No file selected";
return;
}
if (!isZipFile(file)) {
fileMeta.textContent = `${file.name} (unsupported)`;
resetResult();
setStatus("Please select a .zip file.");
return;
}
if (file.size > MAX_UPLOAD_BYTES) {
fileMeta.textContent = `${file.name} (${Math.round(file.size / 1024)} KB)`;
resetResult();
setStatus("File is too large. Max 150 MB.");
return;
}
fileMeta.textContent = `${file.name} (${Math.round(file.size / 1024)} KB)`;
resetResult();
setStatus("Ready to decode.");
});
const uploadViaBlob = async (file) => {
const blob = await upload(file.name, file, {
access: "public",
handleUploadUrl: "/api/upload",
contentType: file.type || "application/zip",
});
return blob.url;
};
const decodeWithUrl = async (fileUrl) => {
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ file_url: fileUrl }),
});
return response;
};
form.addEventListener("submit", async (event) => {
event.preventDefault();
const file = fileInput.files[0];
if (!file) {
setStatus("Please select a zip file first.");
return;
}
if (!isZipFile(file)) {
setStatus("Please select a .zip file.");
return;
}
if (file.size > MAX_UPLOAD_BYTES) {
setStatus("File is too large. Max 150 MB.");
return;
}
submitBtn.disabled = true;
resetResult();
setStatus("Uploading and decoding...", "busy");
const formData = new FormData();
formData.append("file", file);
try {
let response = null;
if (isLocalFile) {
response = await fetch(apiUrl, {
method: "POST",
body: formData,
});
} else {
const blobUrl = await uploadViaBlob(file);
response = await decodeWithUrl(blobUrl);
}
const rawText = await response.text();
let payload = null;
try {
payload = rawText ? JSON.parse(rawText) : null;
} catch (parseError) {
if (!response.ok) {
throw new Error(rawText || "Decode failed.");
}
throw new Error("Unexpected response from server.");
}
if (!response.ok) {
throw new Error((payload && payload.error) || "Decode failed.");
}
resultToken.textContent = (payload && payload.decoded_token) || "";
resultBox.hidden = false;
setStatus("Decoded successfully.");
} catch (error) {
setStatus(error.message || "Something went wrong.");
} finally {
submitBtn.disabled = false;
}
});