David
David

Reputation: 11

Implement Cloudinary Signed Upload in Kotlin

I am having a tough time in implementing signed upload to Cloudinary using Kotlin. I have implemented my backend to provide me a signture and timestamp. This is what I have done to build the config:

var config = HashMap<String, Any> ()
        config.put("cloud_name", "my_cloud_name");
        //config.put("apiKey", my_api_key);
        config.put("use_filename", true);

Now, I am unable to do the MediaManager.init using the signature. Can anyone please help? The Java code says to do the below, but I am unable to reproduce the same in Kotlin:

MediaManager.init(this, new SignatureProvider() {
    @Override
    public Signature provideSignature(Map options) {
        // call server signature endpoint
    }
}, null);

Upvotes: 1

Views: 424

Answers (2)

Kevin Kamau
Kevin Kamau

Reputation: 264

Get the timestamp and signature from your backend then add those as options.

val options = mapOf(
  "timestamp" to // timestamp from backend,
  "signature" to // signature from backend,
  // other options from backend
)
MediaManager.get()
            .upload(uri)
            .options(options)
            .callback(uploadCallback)
            .dispatch()

So this means not creating a SignatureProvider in the MediaManager.init().

I came to using this because I could not get this to work with the SignatureProvider in the MediaManager.init().

Upvotes: 0

nitzanj
nitzanj

Reputation: 1699

This is how you intialize MediaManager with a signature provider in Kotlin:

MediaManager.init(thiscontext!!, object: SignatureProvider {
        override fun provideSignature(options: MutableMap<Any?, Any?>?): Signature {
            return myBackendConnector.signRequest(options)
        }

        override fun getName(): String {
            return "myCustomSignatureProvider"
        }

    }, config)

This will work assuming your backend already has the api key (it should), and that the return type from your connector is Signature. Otherwise you'll need to adapt your backend's result to Signature (populate the POJO with the result your server provided).

Upvotes: 2

Related Questions