bulk upload

This commit is contained in:
Davide Scaini
2026-04-10 12:50:38 +02:00
parent 683b7d9c1b
commit 8ceb714765
2 changed files with 62 additions and 41 deletions
+31 -17
View File
@@ -465,43 +465,57 @@ _SUPPORTED_SUFFIXES = {".fit", ".gpx", ".tcx", ".fit.gz", ".gpx.gz", ".tcx.gz"}
@app.post("/api/upload") @app.post("/api/upload")
async def upload_activity( async def upload_activity(
file: UploadFile = File(...), files: list[UploadFile] = File(...),
bincio_session: Optional[str] = Cookie(default=None), bincio_session: Optional[str] = Cookie(default=None),
) -> JSONResponse: ) -> JSONResponse:
from bincio.extract.ingest import ingest_parsed
from bincio.extract.parsers.factory import parse_file
from bincio.extract.writer import make_activity_id
from bincio.render.merge import merge_all
user = _require_user(bincio_session) user = _require_user(bincio_session)
dd = _get_data_dir() / user.handle dd = _get_data_dir() / user.handle
staging = dd / "_uploads"
staging.mkdir(exist_ok=True)
results = []
any_added = False
for file in files:
name = Path(file.filename or "upload.fit").name name = Path(file.filename or "upload.fit").name
p = Path(name.lower()) p = Path(name.lower())
suffix = (p.stem.rsplit(".", 1)[-1].join([".", ".gz"]) if "." in p.stem else ".gz") if p.suffix == ".gz" else p.suffix suffix = (p.stem.rsplit(".", 1)[-1].join([".", ".gz"]) if "." in p.stem else ".gz") if p.suffix == ".gz" else p.suffix
if suffix not in _SUPPORTED_SUFFIXES: if suffix not in _SUPPORTED_SUFFIXES:
raise HTTPException(400, f"Unsupported file type '{suffix}'") results.append({"name": name, "ok": False, "error": f"Unsupported file type '{suffix}'"})
continue
contents = await file.read() contents = await file.read()
if len(contents) > 50 * 1024 * 1024: if len(contents) > 50 * 1024 * 1024:
raise HTTPException(413, "File too large (max 50 MB)") results.append({"name": name, "ok": False, "error": "File too large (max 50 MB)"})
staging = dd / "_uploads" continue
staging.mkdir(exist_ok=True)
staged = staging / name staged = staging / name
staged.write_bytes(contents) staged.write_bytes(contents)
try: try:
from bincio.extract.ingest import ingest_parsed
from bincio.extract.parsers.factory import parse_file
activity = parse_file(staged) activity = parse_file(staged)
activity_id_check = dd / "activities" / f"{activity.source_file}.json"
from bincio.extract.writer import make_activity_id
activity_id = make_activity_id(activity) activity_id = make_activity_id(activity)
if (dd / "activities" / f"{activity_id}.json").exists(): if (dd / "activities" / f"{activity_id}.json").exists():
raise HTTPException(409, f"Activity already exists: {activity_id}") results.append({"name": name, "ok": False, "error": "duplicate"})
continue
ingest_parsed(activity, dd, privacy="public") ingest_parsed(activity, dd, privacy="public")
from bincio.render.merge import merge_all results.append({"name": name, "ok": True, "id": activity_id})
merge_all(dd) any_added = True
_trigger_rebuild(user.handle)
except HTTPException:
raise
except Exception as exc: except Exception as exc:
raise HTTPException(422, f"Failed to process activity: {type(exc).__name__}: {exc}") results.append({"name": name, "ok": False, "error": f"{type(exc).__name__}: {exc}"})
finally: finally:
staged.unlink(missing_ok=True) staged.unlink(missing_ok=True)
return JSONResponse({"ok": True, "id": activity_id})
if any_added:
merge_all(dd)
_trigger_rebuild(user.handle)
added = [r for r in results if r["ok"]]
return JSONResponse({"ok": True, "added": len(added), "results": results})
@app.post("/api/strava/sync") @app.post("/api/strava/sync")
+19 -12
View File
@@ -256,8 +256,8 @@ try {
id="upload-drop" 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" 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> <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" /> <input id="upload-input" type="file" accept=".fit,.gpx,.tcx,.fit.gz,.gpx.gz,.tcx.gz" class="hidden" multiple />
</div> </div>
<p id="upload-status" class="mt-3 text-xs text-center" style="color: var(--text-5); min-height: 1.25rem"></p> <p id="upload-status" class="mt-3 text-xs text-center" style="color: var(--text-5); min-height: 1.25rem"></p>
</div> </div>
@@ -437,24 +437,31 @@ try {
e.preventDefault(); e.preventDefault();
drop.style.borderColor = ''; drop.style.borderColor = '';
drop.style.color = ''; 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) { async function doUpload(files) {
label.textContent = file.name; const n = files.length;
fileStatus.textContent = 'Uploading…'; label.textContent = n === 1 ? files[0].name : `${n} files selected`;
fileStatus.textContent = `Uploading ${n} file${n > 1 ? 's' : ''}…`;
fileStatus.style.color = 'var(--text-4)'; fileStatus.style.color = 'var(--text-4)';
drop.style.pointerEvents = 'none'; drop.style.pointerEvents = 'none';
const fd = new FormData(); const fd = new FormData();
fd.append('file', file); for (const f of files) fd.append('files', f);
try { 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()); if (!r.ok) throw new Error(await r.text());
const d = await r.json(); const d = await r.json();
fileStatus.textContent = 'Done! Opening activity…'; const dupes = d.results.filter(r => r.error === 'duplicate').length;
fileStatus.style.color = '#4ade80'; const errors = d.results.filter(r => !r.ok && r.error !== 'duplicate').length;
setTimeout(() => { window.location.href = `${baseUrl}activity/${d.id}/`; }, 600); 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) { } catch (e) {
fileStatus.textContent = 'Error: ' + e.message; fileStatus.textContent = 'Error: ' + e.message;
fileStatus.style.color = '#f87171'; fileStatus.style.color = '#f87171';