All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cloudinary.config.CloudinaryConfig.kt Maven / Gradle / Ivy

Go to download

Cloudinary is a cloud service that offers a solution to a web application's entire image management pipeline. Upload images to the cloud. Automatically perform smart image resizing, cropping and conversion without installing any complex software. Integrate Facebook or Twitter profile image extraction in a snap, in any dimension and style to match your website’s graphics requirements. Images are seamlessly delivered through a fast CDN, and much much more. This Java library allows to easily integrate with Cloudinary in Kotlin applications.

There is a newer version: 1.10.0
Show newest version
package com.cloudinary.config

import java.net.URI

data class CloudinaryConfig(
    val cloudConfig: CloudConfig,
    val urlConfig: UrlConfig = UrlConfig(),
    val apiConfig: ApiConfig = ApiConfig()
) : IUrlConfig by urlConfig, ICloudConfig by cloudConfig, IApiConfig by apiConfig {

    companion object {
        fun fromUri(uri: String): CloudinaryConfig {

            val params = parseConfigUrl(uri)

            val cloudConfig = CloudConfig(params)
            val urlConfig = UrlConfig(params)
            val apiConfig = ApiConfig(params)

            return CloudinaryConfig(
                cloudConfig,
                urlConfig,
                apiConfig
            )
        }
    }
}

internal fun Map.getBoolean(key: String) =
    when (val obj = this[key]) {
        is Boolean -> obj
        is String -> obj.toBoolean()
        else -> null
    }

internal fun Map.getInt(key: String) =
    when (val obj = this[key]) {
        is Int -> obj
        is String -> obj.toInt()
        else -> null
    }

private fun parseConfigUrl(cloudinaryUrl: String): Map {
    require(cloudinaryUrl.isNotBlank()) { "Cloudinary url must not be blank" }
    require(cloudinaryUrl.startsWith("cloudinary://")) { "Cloudinary url must start with 'cloudinary://'" }

    val params = mutableMapOf()
    val uri = URI.create(cloudinaryUrl)
    params["cloud_name"] = uri.host

    uri.userInfo?.split(":")?.run {
        params["api_key"] = get(0)
        if (size > 1) params["api_secret"] = get(1)
    }

    params["private_cdn"] = !uri.path.isNullOrBlank()
    params["secure_distribution"] = uri.path
    uri.query?.let { updateMapFromQuery(params, it) }
    return params
}

private fun updateMapFromQuery(params: MutableMap, query: String) {
    query.split("&").forEach {
        val (key, value) = it.split("=")

        if (isNestedKey(key)) {
            putNestedValue(params, key, value)
        } else {
            params[key] = value
        }
    }
}

private fun putNestedValue(params: MutableMap, key: String, value: String) {
    val chain = key.split("[", "]").filter { it.isNotEmpty() }
    var outer = params
    var innerKey = chain[0]
    var i = 0
    while (i < chain.size - 1) {
        @Suppress("UNCHECKED_CAST") var inner: MutableMap? = outer[innerKey] as? MutableMap
        if (inner == null) {
            inner = mutableMapOf()
            outer[innerKey] = inner
        }
        outer = inner
        i++
        innerKey = chain[i]
    }

    outer[innerKey] = value
}

private fun isNestedKey(key: String): Boolean {
    return key.matches("\\w+\\[\\w+]".toRegex())
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy