digital-art-api / lib /cloudinary_service.py
Ruzzel Mendoza
Clean Deployment for Hugging Face
887791e
Raw History Blame
1.02 kB
import cloudinary
import cloudinary.uploader
import os
from dotenv import load_dotenv
from fastapi import HTTPException, UploadFile, File
load_dotenv()
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",
)
return response
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
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))