fix: Resolve NameError by moving config initialization to top of file

This commit is contained in:
Khoa.vo 2025-12-30 19:43:49 +07:00
parent 7376050da7
commit c7f99e0c90

54
app.py
View file

@ -22,6 +22,34 @@ log = logging.getLogger('werkzeug')
log.setLevel(logging.WARNING)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
# Configuration Directory Setup
CONFIG_DIR = os.environ.get('CONFIG_DIR', os.path.dirname(__file__))
if not os.path.exists(CONFIG_DIR):
os.makedirs(CONFIG_DIR, exist_ok=True)
def get_config_path(filename):
return os.path.join(CONFIG_DIR, filename)
def initialize_config_files():
"""Copy default config files to CONFIG_DIR if they don't exist."""
defaults = ['prompts.json', 'user_prompts.json', 'gallery_favorites.json']
source_dir = os.path.dirname(__file__)
for filename in defaults:
dest_path = get_config_path(filename)
if not os.path.exists(dest_path):
source_path = os.path.join(source_dir, filename)
if os.path.exists(source_path):
print(f"Initializing {filename} in {CONFIG_DIR}...", flush=True)
try:
import shutil
shutil.copy2(source_path, dest_path)
except Exception as e:
print(f"Error initializing {filename}: {e}", flush=True)
# Run initialization on startup
initialize_config_files()
PREVIEW_MAX_DIMENSION = 1024
PREVIEW_JPEG_QUALITY = 85
@ -196,33 +224,7 @@ UPLOADS_DIR = os.path.join(app.static_folder, 'uploads')
os.makedirs(UPLOADS_DIR, exist_ok=True)
ALLOWED_GALLERY_EXTS = ('.png', '.jpg', '.jpeg', '.webp')
# Configuration Directory Setup
CONFIG_DIR = os.environ.get('CONFIG_DIR', os.path.dirname(__file__))
if not os.path.exists(CONFIG_DIR):
os.makedirs(CONFIG_DIR, exist_ok=True)
def get_config_path(filename):
return os.path.join(CONFIG_DIR, filename)
def initialize_config_files():
"""Copy default config files to CONFIG_DIR if they don't exist."""
defaults = ['prompts.json', 'user_prompts.json', 'gallery_favorites.json']
source_dir = os.path.dirname(__file__)
for filename in defaults:
dest_path = get_config_path(filename)
if not os.path.exists(dest_path):
source_path = os.path.join(source_dir, filename)
if os.path.exists(source_path):
print(f"Initializing {filename} in {CONFIG_DIR}...", flush=True)
try:
import shutil
shutil.copy2(source_path, dest_path)
except Exception as e:
print(f"Error initializing {filename}: {e}", flush=True)
# Run initialization on startup
initialize_config_files()
def normalize_gallery_path(path):