-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (152 loc) · 5.43 KB
/
Copy pathscript.js
File metadata and controls
183 lines (152 loc) · 5.43 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const form = document.getElementById('extractForm');
const submitBtn = document.getElementById('submitBtn');
const statusMessage = document.getElementById('statusMessage');
const downloadSection = document.getElementById('downloadSection');
const downloadBtn = document.getElementById('downloadBtn');
const newExtractionBtn = document.getElementById('newExtractionBtn');
const durationText = document.getElementById('durationText');
const targetText = document.getElementById('targetText');
const statusText = document.getElementById('statusText');
const urlInput = document.getElementById('firmwareUrl');
const targetSelect = document.getElementById('targetSelect');
document.addEventListener('DOMContentLoaded', () => {
populateTargetSelect();
setupEventListeners();
setLoading(false);
});
function populateTargetSelect() {
if (!targetSelect) return;
SUPPORTED_FILES.forEach(key => {
const option = document.createElement('option');
option.value = key;
option.textContent = key;
targetSelect.appendChild(option);
});
}
function setupEventListeners() {
form.addEventListener('submit', handleSubmit);
newExtractionBtn.addEventListener('click', resetForm);
}
async function handleSubmit(e) {
e.preventDefault();
hideMessage();
hideDownloadSection();
const selectedTarget = targetSelect.value;
if (!selectedTarget) {
showMessage('<i class="fas fa-exclamation-circle"></i> Please select a target file to extract', 'error');
return;
}
const payload = {
url: urlInput.value,
target: selectedTarget
};
setLoading(true);
try {
const checkUrl = `${API_CONFIG.baseUrl}${API_CONFIG.endpoints.check}?url=${encodeURIComponent(payload.url)}&target=${encodeURIComponent(payload.target)}`;
const checkResponse = await fetch(checkUrl);
const checkData = await checkResponse.json();
if (checkResponse.ok && checkData.status === 'found') {
handleSuccess({
status: 'cached',
message: 'File already exists in dataset (from cache)',
download_url: checkData.download_url,
target: checkData.target,
duration_seconds: 0
});
setLoading(false);
return;
}
} catch (error) {
// Cache check failed silently — fall through to the normal extraction request.
}
try {
const response = await fetch(`${API_CONFIG.baseUrl}${API_CONFIG.endpoints.extract}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
const data = await response.json();
if (response.ok) {
handleSuccess(data);
} else {
handleError(data, response.status);
}
} catch (error) {
showMessage('<i class="fas fa-exclamation-triangle"></i> Network error: Unable to connect to the server', 'error');
} finally {
setLoading(false);
}
}
function handleSuccess(data) {
if (data.status === 'cached') {
statusText.innerHTML = '<i class="fas fa-rocket" style="color: var(--info); margin-right: 0.25rem;"></i> Retrieved from cache';
showMessage(`<i class="fas fa-bolt"></i> ${data.message}`, 'info');
} else if (data.status === 'completed') {
statusText.innerHTML = '<i class="fas fa-check" style="color: var(--success); margin-right: 0.25rem;"></i> Extraction completed';
showMessage(`<i class="fas fa-check-circle"></i> ${data.message}`, 'success');
}
downloadBtn.href = data.download_url;
downloadBtn.download = data.target;
durationText.textContent = `${data.duration_seconds}s`;
targetText.textContent = data.target;
hideForm();
showDownloadSection();
}
function handleError(data, statusCode) {
let errorIcon = '<i class="fas fa-exclamation-circle"></i>';
let errorType = 'error';
let errorText = data.message || data.detail || 'An unexpected error occurred';
if (statusCode === 429) {
errorIcon = '<i class="fas fa-clock"></i>';
errorType = 'warning';
} else if (errorText.toLowerCase().includes('capacity')) {
errorIcon = '<i class="fas fa-hourglass-half"></i>';
errorType = 'warning';
}
showMessage(`${errorIcon} ${errorText}`, errorType);
}
function showMessage(message, type) {
statusMessage.innerHTML = message;
statusMessage.className = `status-message ${type}`;
statusMessage.classList.remove('hidden');
}
function hideMessage() {
statusMessage.classList.add('hidden');
}
function showDownloadSection() {
downloadSection.classList.remove('hidden');
}
function hideDownloadSection() {
downloadSection.classList.add('hidden');
}
function hideForm() {
form.classList.add('hidden');
}
function showForm() {
form.classList.remove('hidden');
}
function resetForm() {
hideDownloadSection();
hideMessage();
showForm();
urlInput.value = '';
targetSelect.value = '';
}
function setLoading(loading) {
submitBtn.disabled = loading;
urlInput.disabled = loading;
targetSelect.disabled = loading;
if (loading) {
submitBtn.innerHTML = `
<div class="spinner"></div>
<span>Processing...</span>
`;
} else {
submitBtn.innerHTML = `
<i class="fas fa-bolt"></i>
<span>Extract Now</span>
`;
}
}