bettersight-statement-extra.../backend/repositories/file_repository.py

38 lines
1.4 KiB
Python

# Local filesystem access for an uploaded statement - saving it for the
# duration of processing, removing it once done. Generated Excel exports
# no longer touch disk at all (see routes/extraction.py's /api/export -
# served from an in-memory buffer) or this repository - the frontend
# persists them straight to PocketBase/R2 instead (documentsService.js's
# saveExportFile), so there's nothing on the VPS's own disk to clean up.
import os
import uuid
from werkzeug.utils import secure_filename
def save_upload(file_storage, upload_folder):
"""
Saves an uploaded Werkzeug FileStorage to disk under a UUID-prefixed,
sanitized filename (avoids both path traversal and same-name
collisions between concurrent uploads).
Data flow:
file_storage (FileStorage) - the uploaded file from a Flask request
upload_folder (str) - directory to save into
returns (str) - the full saved path
"""
path = os.path.join(upload_folder, f"{uuid.uuid4()}_{secure_filename(file_storage.filename)}")
file_storage.save(path)
return path
def remove_upload(path):
"""Deletes a previously-saved upload - called once processing (success
or failure) is done with it, so uploads/ doesn't accumulate."""
os.remove(path)
def new_output_filename():
"""A fresh, collision-safe filename for a generated Excel export - just a name, no disk I/O."""
return f"Statement_Extraction_{uuid.uuid4().hex[:8]}.xlsx"