Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix promise return from cloudinary #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions android/src/main/java/com/rncloudinary/RNCloudinaryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.util.Log;

import com.cloudinary.ProgressCallback;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand All @@ -19,6 +21,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class RNCloudinaryModule extends ReactContextBaseJavaModule {
Expand All @@ -35,6 +38,32 @@ public RNCloudinaryModule(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
}

public static WritableMap toWritableMap(Map<String, Object> map) {
WritableMap writableMap = Arguments.createMap();
Iterator iterator = map.entrySet().iterator();

while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
Object value = pair.getValue();

if (value == null) {
writableMap.putNull((String) pair.getKey());
} else if (value instanceof Boolean) {
writableMap.putBoolean((String) pair.getKey(), (Boolean) value);
} else if (value instanceof Double) {
writableMap.putDouble((String) pair.getKey(), (Double) value);
} else if (value instanceof Integer) {
writableMap.putInt((String) pair.getKey(), (Integer) value);
} else if (value instanceof String) {
writableMap.putString((String) pair.getKey(), (String) value);
}

iterator.remove();
}

return writableMap;
}

@Override
public String getName() {
return "RNCloudinary";
Expand Down Expand Up @@ -62,18 +91,10 @@ public void uploadImage(String path, Promise promise) {

Uri myFileUri = Uri.parse(path);
InputStream inputStream = this.reactContext.getContentResolver().openInputStream(myFileUri);
this.mCloudinary.uploader().unsignedUpload(inputStream, this.mPresetName, this.mConfig, new ProgressCallback() {
public void onProgress(long bytesUploaded, long totalBytes) {
if (bytesUploaded >= totalBytes) {
if (_this.isResolved == false) {
_promise.resolve("success");
_this.isResolved = true;
}
} else {

}
}
}) ;
Map uploadResult = this.mCloudinary.uploader().unsignedUpload(inputStream, this.mPresetName, this.mConfig);
WritableMap res = RNCloudinaryModule.toWritableMap(uploadResult);
_promise.resolve(res);
_this.isResolved = true;
} catch (RuntimeException e) {
String code = "Error";
String message = "Error";
Expand All @@ -89,4 +110,4 @@ public void onProgress(long bytesUploaded, long totalBytes) {

}

}
}
24 changes: 13 additions & 11 deletions ios/RNCloudinary.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ - (dispatch_queue_t)methodQueue {
reject(code, message, error);
}

if (self.cloudinary) {
[[self.cloudinary createUploader] uploadWithData:data uploadPreset:self.presetName params:NULL progress:^(NSProgress * progress) {

} completionHandler:^(CLDUploadResult * result, NSError * error) {
NSArray *keys = [NSArray arrayWithObjects:@"result", @"error", nil];
NSArray *objects = [NSArray arrayWithObjects:@"success", @"", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
resolve(dictionary);
}];
} else {
if (self.cloudinary) {
[[self.cloudinary createUploader] uploadWithData:data uploadPreset:self.presetName params:NULL progress:^(NSProgress * progress) {

} completionHandler:^(CLDUploadResult * result, NSError * error) {
if (error) {
NSString *code = @"Cloudinary error";
NSString *message = @"Cloudinary service returned an error.";
reject(code, message , error);
} else {
resolve([result resultJson]);
}
}];
} else {
NSString *code = @"not configured";
NSString *message = @"Cloudinary service is not configured correctly.";
NSError *error = [NSError errorWithDomain:@"RNCloudinary" code:0 userInfo:nil];
Expand Down