feat: Organize public uploads into daily directories based on timezone

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2025-11-23 10:43:16 -07:00
parent 979354f87b
commit be5c8dec26
2 changed files with 23 additions and 2 deletions

View File

@@ -33,6 +33,11 @@ try:
except Exception: except Exception:
qrcode = None qrcode = None
try:
import pytz
except ImportError:
pytz = None
from app.config import Settings, load_settings from app.config import Settings, load_settings
# ---- App & static ---- # ---- 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.""" """Get or create a directory for an album. Returns the path."""
if not album_name or not isinstance(album_name, str): if not album_name or not isinstance(album_name, str):
album_name = "public" 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) os.makedirs(save_dir, exist_ok=True)
return save_dir return save_dir

View File

@@ -24,6 +24,7 @@ class Settings:
log_level: str = "INFO" log_level: str = "INFO"
chunked_uploads_enabled: bool = False chunked_uploads_enabled: bool = False
chunk_size_mb: int = 95 chunk_size_mb: int = 95
timezone: str = "UTC"
def _hash_password(pw: str) -> str: def _hash_password(pw: str) -> str:
"""Return PBKDF2-SHA256 hash of a password.""" """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")) chunk_size_mb = int(os.getenv("CHUNK_SIZE_MB", "95"))
except ValueError: except ValueError:
chunk_size_mb = 95 chunk_size_mb = 95
timezone = os.getenv("TIMEZONE", "UTC")
return Settings( return Settings(
admin_password=admin_password, admin_password=admin_password,
max_concurrent=maxc, max_concurrent=maxc,
@@ -81,4 +83,5 @@ def load_settings() -> Settings:
log_level=log_level, log_level=log_level,
chunked_uploads_enabled=chunked_uploads_enabled, chunked_uploads_enabled=chunked_uploads_enabled,
chunk_size_mb=chunk_size_mb, chunk_size_mb=chunk_size_mb,
timezone=timezone,
) )