From 929e004027d8fe40a033951a97660b6b7d977456 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Sun, 30 Jul 2017 00:38:29 +0100 Subject: [PATCH 1/4] Fix promise return from cloudinary --- ios/RNCloudinary.m | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ios/RNCloudinary.m b/ios/RNCloudinary.m index 46a8b13..5d959dd 100644 --- a/ios/RNCloudinary.m +++ b/ios/RNCloudinary.m @@ -61,13 +61,14 @@ - (dispatch_queue_t)methodQueue { 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); + completionHandler:^(CLDUploadResult * result, NSError * error) { + if (error) { + NSString *code = @"cloudinary error"; + + reject(code, @"Cloudinary service returned an error" , error); + } else { + resolve([result resultJson]); + } }]; } else { NSString *code = @"not configured"; From b4f40bce8eb3bcec307bfb7d4da72fe5c2ad3408 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Sun, 30 Jul 2017 00:42:09 +0100 Subject: [PATCH 2/4] Keep codestyle inline --- ios/RNCloudinary.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ios/RNCloudinary.m b/ios/RNCloudinary.m index 5d959dd..5e18a69 100644 --- a/ios/RNCloudinary.m +++ b/ios/RNCloudinary.m @@ -64,8 +64,8 @@ - (dispatch_queue_t)methodQueue { completionHandler:^(CLDUploadResult * result, NSError * error) { if (error) { NSString *code = @"cloudinary error"; - - reject(code, @"Cloudinary service returned an error" , error); + NSString *message = @"Cloudinary service returned an error."; + reject(code, message , error); } else { resolve([result resultJson]); } From f79039eeb2455688d279da007ace539dd4b30803 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Sun, 30 Jul 2017 00:47:40 +0100 Subject: [PATCH 3/4] Fix typo --- ios/RNCloudinary.m | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/ios/RNCloudinary.m b/ios/RNCloudinary.m index 5e18a69..c31bd04 100644 --- a/ios/RNCloudinary.m +++ b/ios/RNCloudinary.m @@ -59,18 +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) { - if (error) { - NSString *code = @"cloudinary error"; - NSString *message = @"Cloudinary service returned an error."; - reject(code, message , error); - } else { - resolve([result resultJson]); - } - }]; - } 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]; From 12f94a6465ca3099c1f8b58e56789fe788c65cc9 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Sun, 6 Aug 2017 11:14:53 +0100 Subject: [PATCH 4/4] Fix android promise resolve --- .../com/rncloudinary/RNCloudinaryModule.java | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/android/src/main/java/com/rncloudinary/RNCloudinaryModule.java b/android/src/main/java/com/rncloudinary/RNCloudinaryModule.java index bb5d9e2..c3d95aa 100644 --- a/android/src/main/java/com/rncloudinary/RNCloudinaryModule.java +++ b/android/src/main/java/com/rncloudinary/RNCloudinaryModule.java @@ -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; @@ -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 { @@ -35,6 +38,32 @@ public RNCloudinaryModule(ReactApplicationContext reactContext) { this.reactContext = reactContext; } + public static WritableMap toWritableMap(Map 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"; @@ -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"; @@ -89,4 +110,4 @@ public void onProgress(long bytesUploaded, long totalBytes) { } -} \ No newline at end of file +}