@@ -159,14 +159,14 @@ - (NSDictionary *)constantsToExport
159
159
}
160
160
161
161
if ([fm fileExistsAtPath: path]) {
162
- reject (@" EEXIST" , @[[ NSString stringWithFormat: @" File '%@ ' already exists" , path] ], nil );
162
+ reject (@" EEXIST" , [ NSString stringWithFormat: @" File '%@ ' already exists" , path], nil );
163
163
}
164
164
else {
165
165
BOOL success = [fm createFileAtPath: path contents: fileContent attributes: NULL ];
166
166
if (success == YES )
167
167
resolve (@[[NSNull null ]]);
168
168
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 );
170
170
}
171
171
}
172
172
@@ -189,14 +189,14 @@ - (NSDictionary *)constantsToExport
189
189
[fileContent appendBytes: bytes length: dataArray.count];
190
190
191
191
if ([fm fileExistsAtPath: path]) {
192
- reject (@" EEXIST" , @[[ NSString stringWithFormat: @" File '%@ ' already exists" , path] ], nil );
192
+ reject (@" EEXIST" , [ NSString stringWithFormat: @" File '%@ ' already exists" , path], nil );
193
193
}
194
194
else {
195
195
BOOL success = [fm createFileAtPath: path contents: fileContent attributes: NULL ];
196
196
if (success == YES )
197
197
resolve (@[[NSNull null ]]);
198
198
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 );
200
200
}
201
201
202
202
free (bytes);
@@ -238,16 +238,24 @@ - (NSDictionary *)constantsToExport
238
238
{
239
239
RNFetchBlobFS * fileStream = [[RNFetchBlobFS alloc ] initWithBridgeRef: self .bridge];
240
240
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
+ }
246
254
}
247
- if (isDir == YES ) {
255
+ else if (isDir) {
248
256
callback (@[@" EISDIR" , [NSString stringWithFormat: @" Expecting a file but '%@ ' is a directory" , path]]);
249
- return ;
250
257
}
258
+
251
259
NSString * streamId = [fileStream openWithPath: path encode: encoding appendData: append];
252
260
callback (@[[NSNull null ], @[NSNull null ], streamId]);
253
261
}
@@ -323,7 +331,7 @@ - (NSDictionary *)constantsToExport
323
331
return reject (@" ENOENT" , [NSString stringWithFormat: @" No such file '%@ '" , path], nil );
324
332
}
325
333
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 );
327
335
}
328
336
NSError * error = nil ;
329
337
NSArray * result = [[NSFileManager defaultManager ] contentsOfDirectoryAtPath: path error: &error];
0 commit comments