def cloudinary_url(source, **options): original_source = source type = options.pop("type", "upload") if type == 'fetch': options["fetch_format"] = options.get("fetch_format", options.pop("format", None)) transformation, options = generate_transformation_string(**options) resource_type = options.pop("resource_type", "image") version = options.pop("version", None) format = options.pop("format", None) cdn_subdomain = options.pop("cdn_subdomain", cloudinary.config().cdn_subdomain) secure_cdn_subdomain = options.pop("secure_cdn_subdomain", cloudinary.config().secure_cdn_subdomain) cname = options.pop("cname", cloudinary.config().cname) shorten = options.pop("shorten", cloudinary.config().shorten) cloud_name = options.pop("cloud_name", cloudinary.config().cloud_name or None) if cloud_name is None: raise ValueError("Must supply cloud_name in tag or in configuration") secure = options.pop("secure", cloudinary.config().secure) private_cdn = options.pop("private_cdn", cloudinary.config().private_cdn) secure_distribution = options.pop("secure_distribution", cloudinary.config().secure_distribution) sign_url = options.pop("sign_url", cloudinary.config().sign_url) api_secret = options.pop("api_secret", cloudinary.config().api_secret) url_suffix = options.pop("url_suffix", None) use_root_path = options.pop("use_root_path", cloudinary.config().use_root_path) auth_token = options.pop("auth_token", None) if auth_token is not False: auth_token = merge(cloudinary.config().auth_token, auth_token) if (not source) or type == "upload" and re.match(r'^https?:', source): return original_source, options resource_type, type = finalize_resource_type(resource_type, type, url_suffix, use_root_path, shorten) source, source_to_sign = finalize_source(source, format, url_suffix) if source_to_sign.find("/") >= 0 \ and not re.match(r'^https?:/', source_to_sign) \ and not re.match(r'^v[0-9]+', source_to_sign) \ and not version: version = "1" if version: version = "v" + str(version) transformation = re.sub(r'([^:])/+', r'\1/', transformation) signature = None if sign_url and not auth_token: to_sign = "/".join(__compact([transformation, source_to_sign])) signature = "s--" + to_string( base64.urlsafe_b64encode(hashlib.sha1(to_bytes(to_sign + api_secret)).digest())[0:8]) + "--" prefix = unsigned_download_url_prefix(source, cloud_name, private_cdn, cdn_subdomain, secure_cdn_subdomain, cname, secure, secure_distribution) source = "/".join(__compact([prefix, resource_type, type, signature, transformation, version, source])) if sign_url and auth_token: path = urlparse(source).path token = cloudinary.auth_token.generate( **merge(auth_token, {"url": path})) source = "%s?%s" % (source, token) return source, options
def _parse_cloudinary_url(self, cloudinary_url): uri = urlparse(cloudinary_url.replace("cloudinary://", "http://")) for k, v in parse_qs(uri.query).items(): if self._is_nested_key(k): self._put_nested_key(k, v) else: self.__dict__[k] = v[0] self.update(cloud_name=uri.hostname, api_key=uri.username, api_secret=uri.password, private_cdn=uri.path != '') if uri.path != '': self.update(secure_distribution=uri.path[1:])
def _parse_cloudinary_url(self, cloudinary_url): uri = urlparse(cloudinary_url) if not self._is_url_scheme_valid(uri): raise ValueError("Invalid CLOUDINARY_URL scheme. Expecting to start with 'cloudinary://'") for k, v in parse_qs(uri.query).items(): if self._is_nested_key(k): self._put_nested_key(k, v) else: self.__dict__[k] = v[0] self.update( cloud_name=uri.hostname, api_key=uri.username, api_secret=uri.password, private_cdn=uri.path != '' ) if uri.path != '': self.update(secure_distribution=uri.path[1:])
def __init__(self): django_settings = import_django_settings() if django_settings: self.update(**django_settings) elif os.environ.get("CLOUDINARY_CLOUD_NAME"): self.update( cloud_name=os.environ.get("CLOUDINARY_CLOUD_NAME"), api_key=os.environ.get("CLOUDINARY_API_KEY"), api_secret=os.environ.get("CLOUDINARY_API_SECRET"), secure_distribution=os.environ.get("CLOUDINARY_SECURE_DISTRIBUTION"), private_cdn=os.environ.get("CLOUDINARY_PRIVATE_CDN") == "true", ) elif os.environ.get("CLOUDINARY_URL"): uri = urlparse(os.environ.get("CLOUDINARY_URL").replace("cloudinary://", "http://")) for k, v in parse_qs(uri.query).items(): self.__dict__[k] = v[0] self.update( cloud_name=uri.hostname, api_key=uri.username, api_secret=uri.password, private_cdn=uri.path != "" ) if uri.path != "": self.update(secure_distribution=uri.path[1:])
def __init__(self): django_settings = import_django_settings() if django_settings: self.update(**django_settings) elif os.environ.get("CLOUDINARY_CLOUD_NAME"): self.update( cloud_name=os.environ.get("CLOUDINARY_CLOUD_NAME"), api_key=os.environ.get("CLOUDINARY_API_KEY"), api_secret=os.environ.get("CLOUDINARY_API_SECRET"), secure_distribution=os.environ.get( "CLOUDINARY_SECURE_DISTRIBUTION"), private_cdn=os.environ.get("CLOUDINARY_PRIVATE_CDN") == 'true') elif os.environ.get("CLOUDINARY_URL"): uri = urlparse( os.environ.get("CLOUDINARY_URL").replace( "cloudinary://", "http://")) for k, v in parse_qs(uri.query).items(): self.__dict__[k] = v[0] self.update(cloud_name=uri.hostname, api_key=uri.username, api_secret=uri.password, private_cdn=uri.path != '') if uri.path != '': self.update(secure_distribution=uri.path[1:])
def _parse_cloudinary_url(cloudinary_url): return urlparse(cloudinary_url)
def cloudinary_url(source, **options): original_source = source patch_fetch_format(options) type = options.pop("type", "upload") transformation, options = generate_transformation_string(**options) resource_type = options.pop("resource_type", "image") force_version = options.pop("force_version", cloudinary.config().force_version) if force_version is None: force_version = True version = options.pop("version", None) format = options.pop("format", None) cdn_subdomain = options.pop("cdn_subdomain", cloudinary.config().cdn_subdomain) secure_cdn_subdomain = options.pop("secure_cdn_subdomain", cloudinary.config().secure_cdn_subdomain) cname = options.pop("cname", cloudinary.config().cname) shorten = options.pop("shorten", cloudinary.config().shorten) cloud_name = options.pop("cloud_name", cloudinary.config().cloud_name or None) if cloud_name is None: raise ValueError("Must supply cloud_name in tag or in configuration") secure = options.pop("secure", cloudinary.config().secure) private_cdn = options.pop("private_cdn", cloudinary.config().private_cdn) secure_distribution = options.pop("secure_distribution", cloudinary.config().secure_distribution) sign_url = options.pop("sign_url", cloudinary.config().sign_url) api_secret = options.pop("api_secret", cloudinary.config().api_secret) url_suffix = options.pop("url_suffix", None) use_root_path = options.pop("use_root_path", cloudinary.config().use_root_path) auth_token = options.pop("auth_token", None) long_url_signature = options.pop("long_url_signature", cloudinary.config().long_url_signature) signature_algorithm = options.pop("signature_algorithm", cloudinary.config().signature_algorithm) if auth_token is not False: auth_token = merge(cloudinary.config().auth_token, auth_token) if (not source) or type == "upload" and re.match(r'^https?:', source): return original_source, options resource_type, type = finalize_resource_type( resource_type, type, url_suffix, use_root_path, shorten) source, source_to_sign = finalize_source(source, format, url_suffix) if not version and force_version \ and source_to_sign.find("/") >= 0 \ and not re.match(r'^https?:/', source_to_sign) \ and not re.match(r'^v[0-9]+', source_to_sign): version = "1" if version: version = "v" + str(version) else: version = None transformation = re.sub(r'([^:])/+', r'\1/', transformation) signature = None if sign_url and not auth_token: to_sign = "/".join(__compact([transformation, source_to_sign])) if long_url_signature: # Long signature forces SHA256 signature_algorithm = SIGNATURE_SHA256 chars_length = LONG_URL_SIGNATURE_LENGTH else: chars_length = SHORT_URL_SIGNATURE_LENGTH if signature_algorithm not in signature_algorithms: raise ValueError("Unsupported signature algorithm '{}'".format(signature_algorithm)) hash_fn = signature_algorithms[signature_algorithm] signature = "s--" + to_string( base64.urlsafe_b64encode( hash_fn(to_bytes(to_sign + api_secret)).digest())[0:chars_length]) + "--" prefix = unsigned_download_url_prefix( source, cloud_name, private_cdn, cdn_subdomain, secure_cdn_subdomain, cname, secure, secure_distribution) source = "/".join(__compact( [prefix, resource_type, type, signature, transformation, version, source])) if sign_url and auth_token: path = urlparse(source).path token = cloudinary.auth_token.generate(**merge(auth_token, {"url": path})) source = "%s?%s" % (source, token) return source, options