Files
Ten31-Portal/backend/ten31portal/storage.py
T
Jonathan Kirkwood 8247c28243 Implement adjudicated DO items across backend, frontend, deploy
From the ROADMAP adjudication (12 of 13 DO items; D2 is a commit action).

Backend:
- B3: pytest suite (auth, entity CRUD, rollup) + dev deps + pytest config
- B4: cap document uploads at TEN31_MAX_UPLOAD_SIZE (default 50MB), stream-
  checked with partial-file cleanup, 413 on overflow
- B7: type AuditLog.detail as dict|list|str|None to match the JSON column
- B10: index foreign-key columns (migration a7b8c9d0e1f2 + index=True)
- B11: cli delete-user logs file-removal errors instead of swallowing them

Frontend:
- F2: distinguish "server unreachable" from "logged out"; retry prompt
- F4: confirm before destructive holdings-replace on import; step progress
- F6: expandable audit-log detail with full JSON
- F7: empty-state on the Investments page
- F8: shared role helpers (WRITER_ROLES/canEditRound/isApprover), used by
  EntitiesList, AuditLog, Import, ValuationWorkflow

Deploy:
- D5: run tsc --noEmit before packaging (build script)
- D6: TEN31_LOG_LEVEL env var (defaults to info)

Verified: 8/8 backend tests pass; alembic upgrades to head with 13 FK
indexes; upload limit rejects oversized + cleans up; frontend tsc + vite
build clean; dev server serves and proxies to the API.
2026-07-01 13:33:40 -05:00

63 lines
1.8 KiB
Python

"""File storage on the data volume for uploaded documents."""
import os
import shutil
from pathlib import Path
from uuid import uuid4
from fastapi import UploadFile
from ten31portal.config import DOCS_DIR, MAX_UPLOAD_SIZE
class UploadTooLarge(Exception):
"""Raised when an upload exceeds MAX_UPLOAD_SIZE. The partial file is removed first."""
def ensure_docs_dir() -> Path:
path = Path(DOCS_DIR)
path.mkdir(parents=True, exist_ok=True)
return path
def save_upload(file: UploadFile) -> tuple[str, int]:
"""Stream an upload to disk under an opaque name. Returns (storage_path, size_bytes).
Enforces MAX_UPLOAD_SIZE as it streams so a runaway upload can't fill the data volume;
the partial file is deleted before UploadTooLarge propagates.
"""
docs = ensure_docs_dir()
suffix = Path(file.filename or "").suffix
storage_name = f"{uuid4().hex}{suffix}"
dest = docs / storage_name
size = 0
try:
with dest.open("wb") as out:
while chunk := file.file.read(1024 * 1024):
size += len(chunk)
if size > MAX_UPLOAD_SIZE:
raise UploadTooLarge(
f"Upload exceeds the {MAX_UPLOAD_SIZE}-byte limit."
)
out.write(chunk)
except UploadTooLarge:
dest.unlink(missing_ok=True)
raise
return storage_name, size
def full_path(storage_path: str) -> Path:
"""Resolve a stored file, guarding against path traversal."""
docs = ensure_docs_dir().resolve()
candidate = (docs / storage_path).resolve()
if not str(candidate).startswith(str(docs) + os.sep):
raise ValueError("Invalid storage path")
return candidate
def delete_file(storage_path: str) -> None:
try:
full_path(storage_path).unlink(missing_ok=True)
except ValueError:
pass