from fastapi import FastAPI, Request from fastapi.responses import JSONResponse import subprocess, tempfile, base64, os app = FastAPI() @app.post("/convert/heic-to-jpeg") async def convert(request: Request): body = await request.json() with tempfile.TemporaryDirectory() as tmpdir: heic_path = os.path.join(tmpdir, "input.heic") jpeg_path = os.path.join(tmpdir, "output.jpg") with open(heic_path, "wb") as f: f.write(base64.b64decode(body["base64"])) subprocess.run( ["sips", "-s", "format", "jpeg", heic_path, "--out", jpeg_path], capture_output=True, timeout=30, check=True ) with open(jpeg_path, "rb") as f: jpeg_b64 = base64.b64encode(f.read()).decode() return JSONResponse({"base64": jpeg_b64, "format": "jpeg"})