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 +} diff --git a/ios/RNCloudinary.m b/ios/RNCloudinary.m index 46a8b13..c31bd04 100644 --- a/ios/RNCloudinary.m +++ b/ios/RNCloudinary.m @@ -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];