Skip to content

Commit 3292b38

Browse files
committed
1) wkh237#491 "writeStream() does not create file if it doesn't exist?"
2) I had gone overboard with the "@[..]" in the ios code, making some error strings arrays 3) fix typos: rename all ENODIR => ENOTDIR
1 parent ef9745d commit 3292b38

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static void writeFile(String path, String encoding, String data, final boolean a
5959
if(!f.exists()) {
6060
if(!dir.exists()) {
6161
if (!dir.mkdirs()) {
62-
promise.reject("EUNSPECIFIED", "Failed to create parent directory '" + path + "'");
62+
promise.reject("EUNSPECIFIED", "Failed to create parent directory of '" + path + "'");
6363
return;
6464
}
6565
}
@@ -118,7 +118,7 @@ static void writeFile(String path, ReadableArray data, final boolean append, fin
118118
if(!f.exists()) {
119119
if(!dir.exists()) {
120120
if (!dir.mkdirs()) {
121-
promise.reject("EUNSPECIFIED", "Failed to create parent directory '" + path + "'");
121+
promise.reject("ENOTDIR", "Failed to create parent directory of '" + path + "'");
122122
return;
123123
}
124124
}
@@ -366,16 +366,26 @@ else if(resolved == null) {
366366
* @param callback Callback
367367
*/
368368
void writeStream(String path, String encoding, boolean append, Callback callback) {
369-
File dest = new File(path);
370-
if(!dest.exists()) {
371-
callback.invoke("ENOENT", "No such file `" + path + "'");
372-
return;
373-
}
374-
if(dest.isDirectory()) {
375-
callback.invoke("EISDIR", "Expecting a file but '" + path + "' is a directory");
376-
return;
377-
}
378369
try {
370+
File dest = new File(path);
371+
File dir = dest.getParentFile();
372+
373+
if(!dest.exists()) {
374+
if(!dir.exists()) {
375+
if (!dir.mkdirs()) {
376+
callback.invoke("ENOTDIR", "Failed to create parent directory of '" + path + "'");
377+
return;
378+
}
379+
}
380+
if(!dest.createNewFile()) {
381+
callback.invoke("ENOENT", "File '" + path + "' does not exist and could not be created");
382+
return;
383+
}
384+
} else if(dest.isDirectory()) {
385+
callback.invoke("EISDIR", "Expecting a file but '" + path + "' is a directory");
386+
return;
387+
}
388+
379389
OutputStream fs = new FileOutputStream(path, append);
380390
this.encoding = encoding;
381391
String streamId = UUID.randomUUID().toString();
@@ -609,7 +619,7 @@ static void ls(String path, Promise promise) {
609619
return;
610620
}
611621
if (!src.isDirectory()) {
612-
promise.reject("ENODIR", "Not a directory '" + path + "'");
622+
promise.reject("ENOTDIR", "Not a directory '" + path + "'");
613623
return;
614624
}
615625
String[] files = new File(path).list();

ios/RNFetchBlob/RNFetchBlob.m

+20-12
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ - (NSDictionary *)constantsToExport
159159
}
160160

161161
if ([fm fileExistsAtPath:path]) {
162-
reject(@"EEXIST", @[[NSString stringWithFormat:@"File '%@' already exists", path]], nil);
162+
reject(@"EEXIST", [NSString stringWithFormat:@"File '%@' already exists", path], nil);
163163
}
164164
else {
165165
BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
166166
if(success == YES)
167167
resolve(@[[NSNull null]]);
168168
else
169-
reject(@"EUNSPECIFIED", @[[NSString stringWithFormat:@"Failed to create new file at path '%@', please ensure the folder exists", path]], nil);
169+
reject(@"EUNSPECIFIED", [NSString stringWithFormat:@"Failed to create new file at path '%@', please ensure the folder exists", path], nil);
170170
}
171171
}
172172

@@ -189,14 +189,14 @@ - (NSDictionary *)constantsToExport
189189
[fileContent appendBytes:bytes length:dataArray.count];
190190

191191
if ([fm fileExistsAtPath:path]) {
192-
reject(@"EEXIST", @[[NSString stringWithFormat:@"File '%@' already exists", path]], nil);
192+
reject(@"EEXIST", [NSString stringWithFormat:@"File '%@' already exists", path], nil);
193193
}
194194
else {
195195
BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
196196
if(success == YES)
197197
resolve(@[[NSNull null]]);
198198
else
199-
reject(@"EUNSPECIFIED", @[[NSString stringWithFormat:@"failed to create new file at path '%@', please ensure the folder exists", path]], nil);
199+
reject(@"EUNSPECIFIED", [NSString stringWithFormat:@"failed to create new file at path '%@', please ensure the folder exists", path], nil);
200200
}
201201

202202
free(bytes);
@@ -238,16 +238,24 @@ - (NSDictionary *)constantsToExport
238238
{
239239
RNFetchBlobFS * fileStream = [[RNFetchBlobFS alloc] initWithBridgeRef:self.bridge];
240240
NSFileManager * fm = [NSFileManager defaultManager];
241-
BOOL isDir = nil;
242-
BOOL exist = [fm fileExistsAtPath:path isDirectory:&isDir];
243-
if(exist == NO) {
244-
callback(@[@"ENOENT", [NSString stringWithFormat:@"No such file `%@`", path]]);
245-
return;
241+
NSString * folder = [path stringByDeletingLastPathComponent];
242+
243+
BOOL isDir = NO;
244+
BOOL exists = [fm fileExistsAtPath:path isDirectory: &isDir];
245+
246+
if(!exists) {
247+
[fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
248+
if(err != nil) {
249+
callback(@[@"ENOTDIR", [NSString stringWithFormat:@"Failed to create parent directory of '%@'; error: %@", path, [err description]]]);
250+
}
251+
if(![fm createFileAtPath:path contents:nil attributes:nil]) {
252+
callback(@[@"ENOENT", [NSString stringWithFormat:@"File '%@' does not exist and could not be created", path]]);
253+
}
246254
}
247-
if(isDir == YES) {
255+
else if(isDir) {
248256
callback(@[@"EISDIR", [NSString stringWithFormat:@"Expecting a file but '%@' is a directory", path]]);
249-
return;
250257
}
258+
251259
NSString * streamId = [fileStream openWithPath:path encode:encoding appendData:append];
252260
callback(@[[NSNull null], @[NSNull null], streamId]);
253261
}
@@ -323,7 +331,7 @@ - (NSDictionary *)constantsToExport
323331
return reject(@"ENOENT", [NSString stringWithFormat:@"No such file '%@'", path], nil);
324332
}
325333
if(isDir == NO) {
326-
return reject(@"ENODIR", [NSString stringWithFormat:@"Not a directory '%@'", path], nil);
334+
return reject(@"ENOTDIR", [NSString stringWithFormat:@"Not a directory '%@'", path], nil);
327335
}
328336
NSError * error = nil;
329337
NSArray * result = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

ios/RNFetchBlobFS.m

+5-5
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ + (void) writeFile:(NSString *)path
349349

350350
BOOL isDir = NO;
351351
BOOL exists = NO;
352-
exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory: &isDir];
352+
exists = [fm fileExistsAtPath:path isDirectory: &isDir];
353353

354354
if (isDir) {
355355
return reject(@"EISDIR", [NSString stringWithFormat:@"Expecting a file but '%@' is a directory", path], nil);
@@ -358,7 +358,7 @@ + (void) writeFile:(NSString *)path
358358
if(!exists) {
359359
[fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
360360
if(err != nil) {
361-
return reject(@"EUNSPECIFIED", @[[NSString stringWithFormat:@"Failed to create parent directory '%@', error: %@", path, [err description]]]], nil);
361+
return reject(@"ENOTDIR", [NSString stringWithFormat:@"Failed to create parent directory of '%@'; error: %@", path, [err description]], nil);
362362
}
363363
if(![fm createFileAtPath:path contents:nil attributes:nil]) {
364364
return reject(@"ENOENT", [NSString stringWithFormat:@"File '%@' does not exist and could not be created", path], nil);
@@ -426,7 +426,7 @@ + (void) writeFileArray:(NSString *)path
426426
if(!exists) {
427427
[fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
428428
if(err != nil) {
429-
return reject(@"EUNSPECIFIED", @[[NSString stringWithFormat:@"Failed to create parent directory '%@', error: %@", path, [err description]]]], nil);
429+
return reject(@"EUNSPECIFIED", [NSString stringWithFormat:@"Failed to create parent directory of '%@'; error: %@", path, [err description]], nil);
430430
}
431431
}
432432

@@ -616,7 +616,7 @@ + (void) mkdir:(NSString *) path resolver:(RCTPromiseResolveBlock)resolve reject
616616
BOOL isDir = NO;
617617
NSError * err = nil;
618618
if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) {
619-
reject(@"EEXIST", @[[NSString stringWithFormat:@"%@ '%@' already exists", isDir ? @"Directory" : @"File", path]], nil);
619+
reject(@"EEXIST", [NSString stringWithFormat:@"%@ '%@' already exists", isDir ? @"Directory" : @"File", path], nil);
620620
}
621621
else {
622622
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&err];
@@ -625,7 +625,7 @@ + (void) mkdir:(NSString *) path resolver:(RCTPromiseResolveBlock)resolve reject
625625
resolve(@YES);
626626
}
627627
else {
628-
reject(@"EUNSPECIFIED", @[[NSString stringWithFormat:@"Error creating folder '%@', error: %@", path, [err description]]]], nil);
628+
reject(@"EUNSPECIFIED", [NSString stringWithFormat:@"Error creating folder '%@', error: %@", path, [err description]], nil);
629629
}
630630
}
631631

0 commit comments

Comments
 (0)