# backend/workers/pdf_worker.py import os import uuid import shutil from celery_app import celery_app from routers.pdf import ( ghostscript_compress, PRESET_MAP, TEMP_DIR, safe_remove, ) @celery_app.task(bind=True) def pdf_compress_task(self, input_path: str, preset: str): output_path = None try: preset_value = PRESET_MAP.get(preset, "/screen") output_path = os.path.join( TEMP_DIR, f"compressed_{uuid.uuid4().hex}.pdf" ) # 🔥 HEAVY WORK (Ghostscript) ghostscript_compress( input_path=input_path, output_path=output_path, preset=preset_value ) if not os.path.exists(output_path): raise Exception("Compression failed: output file missing") return { "status": "done", "output_path": output_path, "size": os.path.getsize(output_path), } except Exception as e: return { "status": "failed", "error": str(e), } finally: # Cleanup input try: safe_remove(input_path) except: pass