Browse Source

Add written bytes count to write APIs

Ben Hsieh 8 years ago
parent
commit
7ce5fba126

+ 10
- 4
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -74,6 +74,7 @@ public class RNFetchBlobFS {
74 74
                     String path = args[0];
75 75
                     String encoding = args[1];
76 76
                     String data = args[2];
77
+                    int written = 0;
77 78
                     File f = new File(path);
78 79
                     File dir = f.getParentFile();
79 80
                     if(!dir.exists())
@@ -90,16 +91,21 @@ public class RNFetchBlobFS {
90 91
                         FileInputStream fin = new FileInputStream(src);
91 92
                         byte [] buffer = new byte [10240];
92 93
                         int read = fin.read(buffer);
94
+                        written = read;
93 95
                         while(read > 0) {
94 96
                             fout.write(buffer, 0, read);
95 97
                             read = fin.read(buffer);
98
+                            written += read;
96 99
                         }
97 100
                         fin.close();
98 101
                     }
99
-                    else
100
-                        fout.write(stringToBytes(data, encoding));
102
+                    else {
103
+                        byte[] bytes = stringToBytes(data, encoding);
104
+                        fout.write(bytes);
105
+                        written = bytes.length;
106
+                    }
101 107
                     fout.close();
102
-                    promise.resolve(Arguments.createArray());
108
+                    promise.resolve(written);
103 109
                 } catch (Exception e) {
104 110
                     promise.reject("RNFetchBlob writeFileError", e.getLocalizedMessage());
105 111
                 }
@@ -133,7 +139,7 @@ public class RNFetchBlobFS {
133 139
                     }
134 140
                     os.write(bytes);
135 141
                     os.close();
136
-                    promise.resolve(null);
142
+                    promise.resolve(data.size());
137 143
                 } catch (Exception e) {
138 144
                     promise.reject("RNFetchBlob writeFileError", e.getLocalizedMessage());
139 145
                 }

+ 10
- 5
src/ios/RNFetchBlobFS.m View File

@@ -185,20 +185,24 @@ NSMutableDictionary *fileStreams = nil;
185 185
     }
186 186
 }
187 187
 
188
-+ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append
188
++ (NSNumber *) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append
189 189
 {
190 190
     NSInputStream * is = [[NSInputStream alloc] initWithFileAtPath:src];
191 191
     NSOutputStream * os = [[NSOutputStream alloc] initToFileAtPath:dest append:append];
192 192
     [is open];
193 193
     [os open];
194 194
     uint8_t buffer[10240];
195
+    long written = 0;
195 196
     int read = [is read:buffer maxLength:10240];
197
+    written += read;
196 198
     while(read > 0) {
197 199
         [os write:buffer maxLength:read];
198 200
         read = [is read:buffer maxLength:10240];
201
+        written += read;
199 202
     }
200 203
     [os close];
201 204
     [is close];
205
+    return [NSNumber numberWithLong:written];
202 206
 }
203 207
 
204 208
 + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
@@ -225,8 +229,8 @@ NSMutableDictionary *fileStreams = nil;
225 229
             content = [[NSData alloc] initWithBase64EncodedString:data options:0];
226 230
         }
227 231
         else if([encoding isEqualToString:@"uri"]) {
228
-            [[self class] writeFileFromFile:data toFile:path append:append];
229
-            resolve([NSNull null]);
232
+            NSNumber* size = [[self class] writeFileFromFile:data toFile:path append:append];
233
+            resolve(size);
230 234
             return;
231 235
         }
232 236
         else {
@@ -241,7 +245,8 @@ NSMutableDictionary *fileStreams = nil;
241 245
             [content writeToFile:path atomically:YES];
242 246
         }
243 247
         fm = nil;
244
-        resolve([NSNull null]);
248
+        
249
+        resolve([NSNumber numberWithInteger:[content length]]);
245 250
     }
246 251
     @catch (NSException * e)
247 252
     {
@@ -283,7 +288,7 @@ NSMutableDictionary *fileStreams = nil;
283 288
         }
284 289
         free(bytes);
285 290
         fm = nil;
286
-        resolve([NSNull null]);
291
+        resolve([NSNumber numberWithInteger: data.count]);
287 292
     }
288 293
     @catch (NSException * e)
289 294
     {