import os import mimetypes import cloudinary import cloudinary.uploader def _is_configured(): return bool(os.getenv("CLOUDINARY_URL")) def _resource_type_for(path): mime_type, _ = mimetypes.guess_type(path) if mime_type and mime_type.startswith("image/"): return "image" return "raw" def upload_file(path, folder="examcraft", public_id=None, tags=None): if not _is_configured(): return None resource_type = _resource_type_for(path) options = { "folder": folder, "resource_type": resource_type, } if public_id: options["public_id"] = public_id if tags: options["tags"] = tags result = cloudinary.uploader.upload(path, **options) return { "url": result.get("secure_url") or result.get("url"), "public_id": result.get("public_id"), "resource_type": result.get("resource_type"), }