Spaces:
Sleeping
Sleeping
Download backend/app/services/cloudinary_storage.py from tech-ties/filezkit-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/spaces/tech-ties/filezkit-backend/resolve/main/backend/app/services/cloudinary_storage.py
- Command line
-
hf download hf://spaces/tech-ties/filezkit-backend/backend/app/services/cloudinary_storage.py
-
curl -L -o cloudinary_storage.py https://huggingface.co/spaces/tech-ties/filezkit-backend/resolve/main/backend/app/services/cloudinary_storage.py
1.5 kB
| import re | |
| import cloudinary | |
| import cloudinary.uploader | |
| import cloudinary.utils | |
| from app.core.config import settings | |
| cloudinary.config( | |
| cloud_name=(settings.CLOUDINARY_CLOUD_NAME or "").strip(), | |
| api_key=(settings.CLOUDINARY_API_KEY or "").strip(), | |
| api_secret=(settings.CLOUDINARY_API_SECRET or "").strip(), | |
| secure=True | |
| ) | |
| def upload_file(file_path: str) -> str: | |
| result = cloudinary.uploader.upload( | |
| file_path, | |
| resource_type="raw", | |
| ) | |
| return result["secure_url"] | |
| def signed_download_url(cloudinary_url: str) -> str: | |
| """Return a private_download_url that bypasses Cloudinary ACL restrictions. | |
| Uses api.cloudinary.com/v1_1/.../download with api_key + HMAC signature, | |
| which works regardless of account-level access-control settings. | |
| """ | |
| import time | |
| resource_type = "raw" | |
| if "/image/" in cloudinary_url: | |
| resource_type = "image" | |
| elif "/video/" in cloudinary_url: | |
| resource_type = "video" | |
| # Extract public_id: everything after /upload/(v{version}/) | |
| m = re.search(r"/upload/(?:v\d+/)?(.+)$", cloudinary_url) | |
| public_id = m.group(1) if m else cloudinary_url | |
| # Derive format from extension (Cloudinary needs it for raw downloads) | |
| fmt = public_id.rsplit(".", 1)[-1] if "." in public_id else "pdf" | |
| return cloudinary.utils.private_download_url( | |
| public_id, | |
| fmt, | |
| resource_type=resource_type, | |
| type="upload", | |
| expires_at=int(time.time()) + 3600, | |
| ) | |