webmazta commited on
Commit
00785d3
·
1 Parent(s): 22f0ecf

fix: ensure output directory exists before saving featured images

Browse files
Files changed (3) hide show
  1. app.py +10 -0
  2. article_rewriter.py +3 -0
  3. main.py +6 -2
app.py CHANGED
@@ -695,6 +695,16 @@ def _start_agent():
695
  print("[STARTUP] Initializing database...")
696
  init_db()
697
 
 
 
 
 
 
 
 
 
 
 
698
  # Recover any jobs stuck in 'running' from a previous crash/deploy
699
  _conn = get_db()
700
  _recovered = _conn.execute("UPDATE jobs SET status = 'pending', started_at = NULL WHERE status = 'running'").rowcount
 
695
  print("[STARTUP] Initializing database...")
696
  init_db()
697
 
698
+ # Ensure working directories exist (downloads, output, uploads)
699
+ from pathlib import Path
700
+ _DATA_DIR = Path("/data") if Path("/data").is_dir() or os.getenv("HUGGING_FACE_SPACES") else Path(__file__).parent
701
+ if _DATA_DIR == Path("/data"):
702
+ _DATA_DIR.mkdir(parents=True, exist_ok=True)
703
+ (_DATA_DIR / "downloads").mkdir(exist_ok=True)
704
+ (_DATA_DIR / "output").mkdir(exist_ok=True)
705
+ (_DATA_DIR / "uploads").mkdir(exist_ok=True)
706
+ print(f"[STARTUP] Working directories ensured: {_DATA_DIR}")
707
+
708
  # Recover any jobs stuck in 'running' from a previous crash/deploy
709
  _conn = get_db()
710
  _recovered = _conn.execute("UPDATE jobs SET status = 'pending', started_at = NULL WHERE status = 'running'").rowcount
article_rewriter.py CHANGED
@@ -1479,6 +1479,9 @@ def create_article_featured_image(
1479
 
1480
  canvas = _collage_3_to_1(padded, WIDTH, HEIGHT)
1481
 
 
 
 
1482
  output_path = OUTPUT_DIR / f"featured_article_{output_name}.jpg"
1483
  canvas.save(str(output_path), "JPEG", quality=92)
1484
  print(f" ✅ Featured image saved: {output_path}")
 
1479
 
1480
  canvas = _collage_3_to_1(padded, WIDTH, HEIGHT)
1481
 
1482
+ # Ensure output directory exists (defensive)
1483
+ OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
1484
+
1485
  output_path = OUTPUT_DIR / f"featured_article_{output_name}.jpg"
1486
  canvas.save(str(output_path), "JPEG", quality=92)
1487
  print(f" ✅ Featured image saved: {output_path}")
main.py CHANGED
@@ -1161,6 +1161,9 @@ def create_featured_image(title: str, frames: list[Image.Image], output_name: st
1161
  draw_top = ImageDraw.Draw(canvas)
1162
  draw_top.rectangle([(0, 0), (WIDTH, 5)], fill=(255, 69, 58))
1163
 
 
 
 
1164
  output_path = OUTPUT_DIR / f"featured_{output_name}.jpg"
1165
  canvas.save(str(output_path), "JPEG", quality=92)
1166
  print(f" [OK] Featured image saved: {output_path}")
@@ -1264,8 +1267,9 @@ def create_flyer(title: str, frames: list[Image.Image], output_name: str, catego
1264
  frame = frame.resize((frame_w, frame_h), Image.LANCZOS)
1265
  canvas.paste(frame, (i * frame_w, 0))
1266
 
1267
-
1268
-
 
1269
  output_path = OUTPUT_DIR / f"flyer_{output_name}.jpg"
1270
  canvas.save(str(output_path), "JPEG", quality=92)
1271
  print(f" [OK] Square flyer saved: {output_path}")
 
1161
  draw_top = ImageDraw.Draw(canvas)
1162
  draw_top.rectangle([(0, 0), (WIDTH, 5)], fill=(255, 69, 58))
1163
 
1164
+ # Ensure output directory exists (defensive)
1165
+ OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
1166
+
1167
  output_path = OUTPUT_DIR / f"featured_{output_name}.jpg"
1168
  canvas.save(str(output_path), "JPEG", quality=92)
1169
  print(f" [OK] Featured image saved: {output_path}")
 
1267
  frame = frame.resize((frame_w, frame_h), Image.LANCZOS)
1268
  canvas.paste(frame, (i * frame_w, 0))
1269
 
1270
+ # Ensure output directory exists (defensive)
1271
+ OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
1272
+
1273
  output_path = OUTPUT_DIR / f"flyer_{output_name}.jpg"
1274
  canvas.save(str(output_path), "JPEG", quality=92)
1275
  print(f" [OK] Square flyer saved: {output_path}")