Upload an image and get editable DrawIO XML. Supported: PNG, JPG, BMP, TIFF, WebP.
(file: UploadFile = File(...))
| 35 | |
| 36 | @app.post("/convert") |
| 37 | async def convert(file: UploadFile = File(...)): |
| 38 | """Upload an image and get editable DrawIO XML. Supported: PNG, JPG, BMP, TIFF, WebP.""" |
| 39 | name = file.filename or "" |
| 40 | ext = Path(name).suffix.lower() |
| 41 | allowed = {".png", ".jpg", ".jpeg", ".bmp", ".tiff", ".webp"} |
| 42 | if ext not in allowed: |
| 43 | raise HTTPException(400, f"Unsupported format. Use one of: {', '.join(sorted(allowed))}.") |
| 44 | |
| 45 | config_path = os.path.join(PROJECT_ROOT, "config", "config.yaml") |
| 46 | if not os.path.exists(config_path): |
| 47 | raise HTTPException(503, "Server not configured (missing config/config.yaml)") |
| 48 | |
| 49 | try: |
| 50 | from main import load_config, Pipeline |
| 51 | import tempfile |
| 52 | import shutil |
| 53 | |
| 54 | config = load_config() |
| 55 | output_dir = config.get("paths", {}).get("output_dir", "./output") |
| 56 | os.makedirs(output_dir, exist_ok=True) |
| 57 | |
| 58 | with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp: |
| 59 | shutil.copyfileobj(file.file, tmp) |
| 60 | tmp_path = tmp.name |
| 61 | |
| 62 | try: |
| 63 | pipeline = Pipeline(config) |
| 64 | result_path = pipeline.process_image( |
| 65 | tmp_path, |
| 66 | output_dir=output_dir, |
| 67 | with_refinement=False, |
| 68 | with_text=True, |
| 69 | ) |
| 70 | if not result_path or not os.path.exists(result_path): |
| 71 | raise HTTPException(500, "Conversion failed") |
| 72 | return {"success": True, "output_path": result_path} |
| 73 | finally: |
| 74 | try: |
| 75 | os.unlink(tmp_path) |
| 76 | except Exception: |
| 77 | pass |
| 78 | except HTTPException: |
| 79 | raise |
| 80 | except Exception as e: |
| 81 | raise HTTPException(500, str(e)) |
| 82 | |
| 83 | |
| 84 | def main(): |
nothing calls this directly
no test coverage detected