async def export_ad_image_file(
ad_id: str,
meta_access_token: Optional[str] = None,
output_dir: str = "ad_images",
) -> str:
if not ad_id:
return _json({"error": "No ad ID provided"})
image = await read_ad_image(ad_id=ad_id, meta_access_token=meta_access_token)
if not isinstance(image, Image):
return _json({"error": "Unexpected image response type"})
os.makedirs(output_dir, exist_ok=True)
file_path = os.path.join(output_dir, f"{ad_id}.jpg")
with open(file_path, "wb") as handle:
handle.write(image.data)
return _json({"filepath": file_path})