"keep data on the server" opt-in/out
This commit is contained in:
+14
-3
@@ -7,7 +7,7 @@ import shutil
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import FastAPI, File, HTTPException, Request, UploadFile
|
||||
from fastapi import FastAPI, File, Form, HTTPException, Request, UploadFile
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.middleware.gzip import GZipMiddleware
|
||||
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
|
||||
@@ -517,7 +517,10 @@ def _file_suffix(name: str) -> str:
|
||||
|
||||
|
||||
@app.post("/api/upload")
|
||||
async def upload_activity(file: UploadFile = File(...)) -> JSONResponse:
|
||||
async def upload_activity(
|
||||
file: UploadFile = File(...),
|
||||
store_original: bool = Form(False),
|
||||
) -> JSONResponse:
|
||||
"""Accept a FIT/GPX/TCX file, extract it, update index.json, and re-merge."""
|
||||
dd = _get_data_dir()
|
||||
|
||||
@@ -536,6 +539,7 @@ async def upload_activity(file: UploadFile = File(...)) -> JSONResponse:
|
||||
staged = staging / name
|
||||
staged.write_bytes(contents)
|
||||
|
||||
kept = False
|
||||
try:
|
||||
from bincio.extract.metrics import compute
|
||||
from bincio.extract.parsers.factory import parse_file
|
||||
@@ -563,6 +567,12 @@ async def upload_activity(file: UploadFile = File(...)) -> JSONResponse:
|
||||
existing[activity_id] = summary
|
||||
write_index(list(existing.values()), dd, owner)
|
||||
|
||||
if store_original:
|
||||
originals_dir = dd / "originals"
|
||||
originals_dir.mkdir(exist_ok=True)
|
||||
staged.rename(originals_dir / name)
|
||||
kept = True
|
||||
|
||||
from bincio.render.merge import merge_all
|
||||
merge_all(dd)
|
||||
|
||||
@@ -571,7 +581,8 @@ async def upload_activity(file: UploadFile = File(...)) -> JSONResponse:
|
||||
except Exception as exc:
|
||||
raise HTTPException(422, f"Failed to process activity file: {type(exc).__name__}")
|
||||
finally:
|
||||
staged.unlink(missing_ok=True)
|
||||
if not kept:
|
||||
staged.unlink(missing_ok=True)
|
||||
|
||||
return JSONResponse({"ok": True, "id": activity_id})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user