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:
22
app/app.py
22
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
|
||||
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user