Make UPLOAD_FOLDER env-configurable; resolve relative paths from project root

This commit is contained in:
jessikitty committed 2026-09-24 19:48:09 +10:00
1 parent 6dcd163edb
commit 04c5eddd17
1 file changed
+22 -4
+22 -4
View File
@@ -2,9 +2,20 @@ import os
from dotenv import load_dotenv from dotenv import load_dotenv
basedir = os.path.abspath(os.path.dirname(__file__)) basedir = os.path.abspath(os.path.dirname(__file__))
projectdir = os.path.dirname(basedir)
# Look for .env beside app/ first (project root), then inside app/.
load_dotenv(os.path.join(projectdir, '.env'))
load_dotenv(os.path.join(basedir, '.env')) load_dotenv(os.path.join(basedir, '.env'))
def _resolve_path(value, default):
"""Absolute value as-is; relative resolved against the project root."""
if not value:
return default
return value if os.path.isabs(value) else os.path.join(projectdir, value)
class Config: class Config:
"""Base configuration.""" """Base configuration."""
@@ -17,8 +28,14 @@ class Config:
SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_TRACK_MODIFICATIONS = False
# File Upload # File Upload
# basedir is E:\LIM\app, so we just need 'static/uploads' to get E:\LIM\app\static\uploads # Defaults to <app>/static/uploads (E:\LIM\app\static\uploads on the
UPLOAD_FOLDER = os.path.join(basedir, 'static', 'uploads') # Windows install). Set UPLOAD_FOLDER to override — the Docker image
# points it at a mounted volume, e.g. /data/uploads.
# A relative value is resolved against the project root, NOT the working
# directory, so `UPLOAD_FOLDER=app/static/uploads` in .env means the same
# thing no matter where you run flask from.
UPLOAD_FOLDER = _resolve_path(os.environ.get('UPLOAD_FOLDER'),
os.path.join(basedir, 'static', 'uploads'))
MAX_CONTENT_LENGTH = int(os.environ.get('MAX_CONTENT_LENGTH', 52428800)) # 50MB MAX_CONTENT_LENGTH = int(os.environ.get('MAX_CONTENT_LENGTH', 52428800)) # 50MB
ALLOWED_EXTENSIONS = {'pdf', 'png', 'jpg', 'jpeg', 'gif', 'webp'} ALLOWED_EXTENSIONS = {'pdf', 'png', 'jpg', 'jpeg', 'gif', 'webp'}
@@ -31,8 +48,9 @@ class Config:
# Backups # Backups
# Defaults to <project root>/backups so archives never land inside the # Defaults to <project root>/backups so archives never land inside the
# uploads tree (which would make each backup include the previous one). # uploads tree (which would make each backup include the previous one).
BACKUP_FOLDER = os.environ.get('BACKUP_FOLDER') or \ # Relative values resolve against the project root, as with UPLOAD_FOLDER.
os.path.join(os.path.dirname(basedir), 'backups') BACKUP_FOLDER = _resolve_path(os.environ.get('BACKUP_FOLDER'),
os.path.join(projectdir, 'backups'))
# Keep only the newest N archives after a backup made from the web UI. # Keep only the newest N archives after a backup made from the web UI.
# Set to 0 to keep everything. # Set to 0 to keep everything.
BACKUP_KEEP = int(os.environ.get('BACKUP_KEEP', 5)) BACKUP_KEEP = int(os.environ.get('BACKUP_KEEP', 5))