digital-art-api / lib /cloudinary_service.py
Ruzzel Mendoza
fix
28a415d
Raw History Blame Contribute Delete
3.83 kB
import cloudinary
import cloudinary.uploader
import logging
import os
import time
import requests
from dotenv import load_dotenv
from fastapi import HTTPException, UploadFile, File
load_dotenv()
logger = logging.getLogger(__name__)
cloudinary.config(
cloud_name=os.getenv("CLOUDINARY_NAME"),
api_key=os.getenv("CLOUDINARY_KEY"),
api_secret=os.getenv("CLOUDINARY_SECRET"),
)
def cloudinary_uploader(image_file: UploadFile = File(...)):
try:
image_file.file.seek(0)
response = cloudinary.uploader.upload(
image_file.file,
resource_type="image",
folder="temp_plag_scans",
# Serper's crawler must fetch this URL without credentials, so the
# temp upload must be explicitly public with a unique name.
access_mode="public",
use_filename=True,
unique_filename=True,
overwrite=False,
)
return response
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
def wait_for_url_ready(url, timeout=15.0, initial_delay=1.0, max_delay=4.0):
"""Poll a Cloudinary delivery URL until it is publicly fetchable.
Serper fetches ``secure_url`` immediately after upload; if the CDN has
not propagated yet it sees a 404/403 and returns HTTP 200 with an empty
``organic`` list. This gate distinguishes that case from a genuine
"no online match" so callers can degrade explicitly instead of
reporting a silent clean negative.
Returns (ready, info) where info carries attempts/last_status/elapsed_ms
for ``web_diagnostics``. Never raises.
"""
start = time.monotonic()
attempts = 0
last_status = None
delay = initial_delay
deadline = start + max(timeout, 0.1)
try:
while True:
attempts += 1
try:
# HEAD is cheap; some CDNs block it, so fall back to a
# 1-byte ranged GET on 403/405.
resp = requests.head(url, timeout=8, allow_redirects=True)
last_status = resp.status_code
content_type = (resp.headers.get("Content-Type") or "").lower()
if resp.status_code == 200 and (
not content_type or "image" in content_type
):
break
if resp.status_code in (403, 405):
g = requests.get(
url,
timeout=8,
headers={"Range": "bytes=0-0"},
stream=True,
)
last_status = g.status_code
g.close()
if g.status_code in (200, 206):
break
except Exception as e:
logger.debug("readiness poll attempt %d failed: %s", attempts, e)
now = time.monotonic()
if now >= deadline:
break
sleep_for = min(delay, max(0.0, deadline - now))
if sleep_for > 0:
time.sleep(sleep_for)
delay = min(delay * 1.5, max_delay)
except Exception as e: # never let readiness break the pipeline
logger.debug("readiness poll error: %s", e)
elapsed_ms = int((time.monotonic() - start) * 1000)
ready = last_status in (200, 206)
return ready, {
"attempts": attempts,
"last_status": last_status,
"elapsed_ms": elapsed_ms,
}
def delete_image(public_id):
try:
result = cloudinary.uploader.destroy(
public_id,
invalidate=True,
)
if result.get("result") == "ok":
return True
else:
return False
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))