bulk upload
This commit is contained in:
+19
-12
@@ -256,8 +256,8 @@ try {
|
||||
id="upload-drop"
|
||||
class="border-2 border-dashed border-zinc-700 rounded-lg p-8 text-center text-zinc-500 text-sm cursor-pointer hover:border-zinc-500 hover:text-zinc-300 transition-colors"
|
||||
>
|
||||
<div id="upload-label">Drop a FIT, GPX, or TCX file<br/>or click to browse</div>
|
||||
<input id="upload-input" type="file" accept=".fit,.gpx,.tcx,.fit.gz,.gpx.gz,.tcx.gz" class="hidden" />
|
||||
<div id="upload-label">Drop FIT, GPX, or TCX files<br/>or click to browse</div>
|
||||
<input id="upload-input" type="file" accept=".fit,.gpx,.tcx,.fit.gz,.gpx.gz,.tcx.gz" class="hidden" multiple />
|
||||
</div>
|
||||
<p id="upload-status" class="mt-3 text-xs text-center" style="color: var(--text-5); min-height: 1.25rem"></p>
|
||||
</div>
|
||||
@@ -437,24 +437,31 @@ try {
|
||||
e.preventDefault();
|
||||
drop.style.borderColor = '';
|
||||
drop.style.color = '';
|
||||
if (e.dataTransfer?.files[0]) doUpload(e.dataTransfer.files[0]);
|
||||
if (e.dataTransfer?.files.length) doUpload(e.dataTransfer.files);
|
||||
});
|
||||
input.addEventListener('change', () => { if (input.files?.[0]) doUpload(input.files[0]); });
|
||||
input.addEventListener('change', () => { if (input.files?.length) doUpload(input.files); });
|
||||
|
||||
async function doUpload(file) {
|
||||
label.textContent = file.name;
|
||||
fileStatus.textContent = 'Uploading…';
|
||||
async function doUpload(files) {
|
||||
const n = files.length;
|
||||
label.textContent = n === 1 ? files[0].name : `${n} files selected`;
|
||||
fileStatus.textContent = `Uploading ${n} file${n > 1 ? 's' : ''}…`;
|
||||
fileStatus.style.color = 'var(--text-4)';
|
||||
drop.style.pointerEvents = 'none';
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
for (const f of files) fd.append('files', f);
|
||||
try {
|
||||
const r = await fetch(`${editUrl}/api/upload`, { method: 'POST', body: fd });
|
||||
const r = await fetch(`${editUrl}/api/upload`, { method: 'POST', credentials: 'include', body: fd });
|
||||
if (!r.ok) throw new Error(await r.text());
|
||||
const d = await r.json();
|
||||
fileStatus.textContent = 'Done! Opening activity…';
|
||||
fileStatus.style.color = '#4ade80';
|
||||
setTimeout(() => { window.location.href = `${baseUrl}activity/${d.id}/`; }, 600);
|
||||
const dupes = d.results.filter(r => r.error === 'duplicate').length;
|
||||
const errors = d.results.filter(r => !r.ok && r.error !== 'duplicate').length;
|
||||
let msg = `${d.added} added`;
|
||||
if (dupes) msg += `, ${dupes} duplicate${dupes > 1 ? 's' : ''}`;
|
||||
if (errors) msg += `, ${errors} failed`;
|
||||
fileStatus.textContent = msg;
|
||||
fileStatus.style.color = d.added > 0 ? '#4ade80' : '#a1a1aa';
|
||||
if (d.added > 0) setTimeout(() => { window.location.reload(); }, 1200);
|
||||
else drop.style.pointerEvents = '';
|
||||
} catch (e) {
|
||||
fileStatus.textContent = 'Error: ' + e.message;
|
||||
fileStatus.style.color = '#f87171';
|
||||
|
||||
Reference in New Issue
Block a user