From be5c8dec265554ade5b56052d9bf2e072a8f3daf Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 23 Nov 2025 10:43:16 -0700 Subject: [PATCH] feat: Organize public uploads into daily directories based on timezone Co-authored-by: aider (gemini/gemini-2.5-pro) --- app/app.py | 22 ++++++++++++++++++++-- app/config.py | 3 +++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/app.py b/app/app.py index 43ed803..b840d67 100644 --- a/app/app.py +++ b/app/app.py @@ -33,6 +33,11 @@ try: except Exception: qrcode = None +try: + import pytz +except ImportError: + pytz = None + from app.config import Settings, load_settings # ---- App & static ---- @@ -256,8 +261,21 @@ def get_or_create_album_dir(album_name: str) -> str: """Get or create a directory for an album. Returns the path.""" if not album_name or not isinstance(album_name, str): album_name = "public" - safe_album_name = sanitize_filename(album_name) - save_dir = os.path.join("./data/uploads", safe_album_name) + + if album_name == "public": + try: + if not pytz: + raise ImportError("pytz is not installed") + tz = pytz.timezone(SETTINGS.timezone) + today = datetime.now(tz).strftime('%Y-%m-%d') + save_dir = os.path.join("./data/uploads", "public", today) + except Exception as e: + logger.warning("Timezone logic failed, falling back to 'public' album. Timezone: %s. Error: %s", SETTINGS.timezone, e) + save_dir = os.path.join("./data/uploads", "public") + else: + safe_album_name = sanitize_filename(album_name) + save_dir = os.path.join("./data/uploads", safe_album_name) + os.makedirs(save_dir, exist_ok=True) return save_dir diff --git a/app/config.py b/app/config.py index a5ea9b4..f2c13cc 100644 --- a/app/config.py +++ b/app/config.py @@ -24,6 +24,7 @@ class Settings: log_level: str = "INFO" chunked_uploads_enabled: bool = False chunk_size_mb: int = 95 + timezone: str = "UTC" def _hash_password(pw: str) -> str: """Return PBKDF2-SHA256 hash of a password.""" @@ -71,6 +72,7 @@ def load_settings() -> Settings: chunk_size_mb = int(os.getenv("CHUNK_SIZE_MB", "95")) except ValueError: chunk_size_mb = 95 + timezone = os.getenv("TIMEZONE", "UTC") return Settings( admin_password=admin_password, max_concurrent=maxc, @@ -81,4 +83,5 @@ def load_settings() -> Settings: log_level=log_level, chunked_uploads_enabled=chunked_uploads_enabled, chunk_size_mb=chunk_size_mb, + timezone=timezone, )