-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.html
More file actions
411 lines (385 loc) · 12.4 KB
/
upload.html
File metadata and controls
411 lines (385 loc) · 12.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="website icon" href="https://arancia313.github.io/BarnyShare/BarnyWarp%20Icon.svg"> <!-- Just use the right icon for the page, bro!! -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BarnyShare - Upload</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
img {
size: 0.5em;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #6e2800 0%, #000000 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
background: white;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
max-width: 500px;
width: 100%;
padding: 40px;
}
h1 {
color: #333;
margin-bottom: 10px;
font-size: 28px;
}
.subtitle {
color: #666;
margin-bottom: 30px;
font-size: 14px;
}
.upload-area {
border: 2px dashed #ff1e00;
border-radius: 8px;
padding: 40px 20px;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
margin-bottom: 20px;
background: #f8f9ff;
}
.upload-area:hover {
border-color: #ff2600;
background: #f0f2ff;
}
.upload-area.dragover {
border-color: #ff7300;
background: #e8ebff;
}
.upload-icon {
font-size: 40px;
margin-bottom: 10px;
}
.upload-text {
color: #333;
font-weight: 500;
margin-bottom: 5px;
}
.upload-hint {
color: #999;
font-size: 12px;
}
input[type="file"] {
display: none;
}
.file-input-label {
display: inline-block;
padding: 10px 20px;
background: #ff9100;
color: white;
border-radius: 6px;
cursor: pointer;
font-weight: 500;
transition: background 0.3s ease;
margin-top: 10px;
}
.file-input-label:hover {
background: #ff5e00;
}
.file-list {
margin-top: 20px;
}
.file-item {
background: #f8f9fa;
border-left: 4px solid #ff5e00;
padding: 12px;
margin-bottom: 10px;
border-radius: 4px;
display: flex;
justify-content: space-between;
align-items: center;
}
.file-name {
color: #333;
font-size: 14px;
font-weight: 500;
word-break: break-all;
}
.file-size {
color: #999;
font-size: 12px;
margin-top: 4px;
}
.upload-btn {
width: 100%;
padding: 12px;
background: #ff9100;
color: white;
border: none;
border-radius: 6px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: background 0.3s ease;
margin-top: 20px;
}
.upload-btn:hover {
background: #ff4800;
}
.upload-btn:disabled {
background: #ccc;
cursor: not-allowed;
}
.result {
margin-top: 20px;
padding: 15px;
border-radius: 6px;
display: none;
}
.result.success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
display: block;
}
.result.error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
display: block;
}
.result-title {
font-weight: 600;
margin-bottom: 8px;
}
.result-link {
color: #ff7300;
text-decoration: none;
word-break: break-all;
}
.result-link:hover {
text-decoration: underline;
}
.progress-bar {
width: 100%;
height: 4px;
background: #e0e0e0;
border-radius: 2px;
overflow: hidden;
margin-top: 10px;
display: none;
}
.progress-bar.active {
display: block;
}
.progress-fill {
height: 100%;
background: #ff6600;
width: 0%;
transition: width 0.1s ease;
}
.file-status { min-width: 26px; display:flex; align-items:center; justify-content:center; }
.spinner-small { width:16px; height:16px; border:2px solid #e6e6e6; border-top-color:#667eea; border-radius:50%; animation:spin 0.8s linear infinite; }
</style>
</head>
<body>
<div class="container">
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;">
<h1 style="margin:0"><img src = "https://arancia313.github.io/BarnyShare/BarnyWarp%20Icon.svg"> BarnyShare Upload</h1>
<a href="/admin/logout" style="background:#ff6b6b;color:white;padding:8px 12px;border-radius:6px;text-decoration:none;font-weight:600;">🔒 Logout</a>
</div>
<p class="subtitle">Drag & drop or click to select files</p>
<div class="upload-area" id="uploadArea">
<div class="upload-icon">📁</div>
<div class="upload-text">Drag files here!</div>
<div class="upload-hint">or click to browse.</div>
<label class="file-input-label">
Choose File
<input type="file" id="fileInput" multiple>
</label>
</div>
<div class="file-list" id="fileList"></div>
<button class="upload-btn" id="uploadBtn" disabled>Upload Files</button>
<div class="progress-bar" id="progressBar">
<div class="progress-fill" id="progressFill"></div>
</div>
<div class="result" id="result"></div>
</div>
<script>
// Check auth status and redirect to login if needed
(async function() {
try {
const res = await fetch('/admin/status');
if (!res.ok) throw new Error('status failed');
const js = await res.json();
if (!js.authenticated) {
// redirect to login and return to this page after login
location.href = '/admin/login.html?next=/upload.html';
return;
}
// fetch CSRF token for authenticated session
const t = await fetch('/admin/csrf-token');
if (t.ok) {
const body = await t.json();
window.CSRF_TOKEN = body.csrfToken;
} else {
console.warn('Could not obtain CSRF token');
}
} catch (e) {
// If status endpoint not available, allow page but server will reject uploads.
console.warn('Auth status check failed', e);
}
})();
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
const fileList = document.getElementById('fileList');
const uploadBtn = document.getElementById('uploadBtn');
const result = document.getElementById('result');
const progressBar = document.getElementById('progressBar');
const progressFill = document.getElementById('progressFill');
let selectedFiles = [];
// Click to upload
uploadArea.addEventListener('click', () => fileInput.click());
// File input change
fileInput.addEventListener('change', (e) => {
selectedFiles = Array.from(e.target.files);
updateFileList();
});
// Drag over
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
// Drop
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
selectedFiles = Array.from(e.dataTransfer.files);
updateFileList();
});
function updateFileList() {
fileList.innerHTML = '';
if (selectedFiles.length === 0) {
uploadBtn.disabled = true;
return;
}
uploadBtn.disabled = false;
selectedFiles.forEach((file, index) => {
const item = document.createElement('div');
item.className = 'file-item';
item.innerHTML = `
<div style="display:flex;gap:12px;align-items:center;">
<div>
<div class="file-name">${file.name}</div>
<div class="file-size">${(file.size / 1024).toFixed(2)} KB</div>
</div>
<div id="status-${index}" class="file-status" aria-hidden="true"></div>
</div>
<button onclick="removeFile(${index})" id="remove-${index}" style="background: #ff6b6b; color: white; border: none; padding: 5px 10px; border-radius: 4px; cursor: pointer; font-size: 12px;">Remove</button>
`;
fileList.appendChild(item);
});
}
function removeFile(index) {
selectedFiles.splice(index, 1);
updateFileList();
result.className = 'result';
result.innerHTML = '';
}
uploadBtn.addEventListener('click', async () => {
if (selectedFiles.length === 0) return;
uploadBtn.disabled = true;
result.className = 'result';
result.innerHTML = '';
progressBar.classList.add('active');
progressFill.style.width = '0%';
let successCount = 0;
let errorCount = 0;
const uploaded = [];
const errors = [];
const uploads = selectedFiles.map((file, index) => {
const statusEl = document.getElementById(`status-${index}`);
const removeBtn = document.getElementById(`remove-${index}`);
if (statusEl) statusEl.innerHTML = '<span class="spinner-small" aria-hidden="true"></span>';
if (removeBtn) removeBtn.disabled = true;
return uploadFile(file).then((resJson) => {
successCount++;
uploaded.push({ file, res: resJson });
if (statusEl) statusEl.innerHTML = '✅';
if (removeBtn) removeBtn.disabled = false;
progressFill.style.width = ((successCount + errorCount) / selectedFiles.length * 100) + '%';
}).catch((err) => {
errorCount++;
errors.push({ file, message: err.message });
if (statusEl) statusEl.innerHTML = '❌';
if (removeBtn) removeBtn.disabled = false;
progressFill.style.width = ((successCount + errorCount) / selectedFiles.length * 100) + '%';
});
});
await Promise.all(uploads);
progressBar.classList.remove('active');
uploadBtn.disabled = false;
if (errorCount === 0) {
result.className = 'result success';
result.innerHTML = `
<div class="result-title">✅ Upload Successful!</div>
<p>All ${successCount} file(s) uploaded successfully.</p>
`;
} else {
result.className = 'result error';
let html = `
<div class="result-title">⚠️ Upload Completed with Errors</div>
<p>Uploaded: ${successCount} | Failed: ${errorCount}</p>
`;
if (errors.length) {
html += '<ul style="margin-top:10px">';
errors.forEach(e => {
html += `<li style="margin-bottom:6px">${escapeHtml(e.file.name)}: ${escapeHtml(e.message)}</li>`;
});
html += '</ul>';
}
result.innerHTML = html;
}
if (uploaded.length) {
const links = uploaded.map(u => `<div><a class="result-link" href="${u.res.path}" target="_blank">${escapeHtml(u.file.name)}</a></div>`).join('');
result.innerHTML += `<div style="margin-top:10px">${links}</div>`;
}
selectedFiles = [];
fileInput.value = '';
updateFileList();
});
async function uploadFile(file) {
const formData = new FormData();
formData.append('file', file);
const response = await fetch('/upload', {
method: 'POST',
body: formData,
headers: window.CSRF_TOKEN ? { 'x-csrf-token': window.CSRF_TOKEN } : {}
});
if (!response.ok) {
// try to extract a helpful error message
let msg = response.statusText || 'Upload failed';
try {
const j = await response.json();
if (j && j.error) msg = j.error;
} catch (e) {
// ignore
}
throw new Error(msg || `Upload failed for ${file.name}`);
}
return response.json();
}
function escapeHtml(text) {
if (!text) return '';
return String(text).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
.replace(/"/g, '"').replace(/'/g, ''');
}
</script>
</body>
</html>