@@ -166,7 +166,7 @@ + (void) readStream:(NSString *)uri
166
166
if ([[NSFileManager defaultManager ] fileExistsAtPath: path] == NO )
167
167
{
168
168
NSString * message = [NSString stringWithFormat: @" File does not exist at path %@ " , path];
169
- NSDictionary * payload = @{ @" event" : FS_EVENT_ERROR, @" detail" : message };
169
+ NSDictionary * payload = @{ @" event" : FS_EVENT_ERROR, @" code " : @" ENOENT " , @" detail" : message };
170
170
[event sendDeviceEventWithName: streamId body: payload];
171
171
free (buffer);
172
172
return ;
@@ -199,7 +199,7 @@ + (void) readStream:(NSString *)uri
199
199
}
200
200
else
201
201
{
202
- NSDictionary * payload = @{ @" event" : FS_EVENT_ERROR, @" detail " : @" RNFetchBlob.readStream unable to resolve URI" };
202
+ NSDictionary * payload = @{ @" event" : FS_EVENT_ERROR, @" code " : @" ENOENT " , @" detail " : @" Unable to resolve URI" };
203
203
[event sendDeviceEventWithName: streamId body: payload];
204
204
}
205
205
// release buffer
@@ -209,7 +209,7 @@ + (void) readStream:(NSString *)uri
209
209
}
210
210
@catch (NSError * err)
211
211
{
212
- NSDictionary * payload = @{ @" event" : FS_EVENT_ERROR, @" detail " : [ NSString stringWithFormat: @" RNFetchBlob.readStream error %@ " , [err description ] ] };
212
+ NSDictionary * payload = @{ @" event" : FS_EVENT_ERROR, @" code " : @" EUNSPECIFIED " , @" detail " : [err description ] };
213
213
[event sendDeviceEventWithName: streamId body: payload];
214
214
}
215
215
@finally
@@ -347,15 +347,21 @@ + (void) writeFile:(NSString *)path
347
347
NSString * folder = [path stringByDeletingLastPathComponent ];
348
348
encoding = [encoding lowercaseString ];
349
349
350
- if (![fm fileExistsAtPath: folder]) {
350
+ BOOL isDir = NO ;
351
+ BOOL exists = NO ;
352
+ exists = [[NSFileManager defaultManager ] fileExistsAtPath: path isDirectory: &isDir];
353
+
354
+ if (isDir) {
355
+ return reject (@" EISDIR" , [NSString stringWithFormat: @" Expecting a file but '%@ ' is a directory" , path], nil );
356
+ }
357
+
358
+ if (!exists) {
351
359
[fm createDirectoryAtPath: folder withIntermediateDirectories: YES attributes: NULL error: &err];
352
360
if (err != nil ) {
353
- reject (@" EUNSPECIFIED" , [err description ], nil );
354
- return ;
361
+ return reject (@" EUNSPECIFIED" , @[[NSString stringWithFormat: @" Failed to create parent directory '%@ ', error: %@ " , path, [err description ]]]], nil );
355
362
}
356
363
if (![fm createFileAtPath: path contents: nil attributes: nil ]) {
357
- reject (@" ENOENT" , [NSString stringWithFormat: @" File '%@ ' does not exist and could not be created, or it is a directory" , path], nil );
358
- return ;
364
+ return reject (@" ENOENT" , [NSString stringWithFormat: @" File '%@ ' does not exist and could not be created" , path], nil );
359
365
}
360
366
}
361
367
@@ -408,11 +414,19 @@ + (void) writeFileArray:(NSString *)path
408
414
// check if the folder exists, if not exists, create folders recursively
409
415
// after the folders created, write data into the file
410
416
NSString * folder = [path stringByDeletingLastPathComponent ];
411
- if (![fm fileExistsAtPath: folder]) {
417
+
418
+ BOOL isDir = NO ;
419
+ BOOL exists = NO ;
420
+ exists = [[NSFileManager defaultManager ] fileExistsAtPath: path isDirectory: &isDir];
421
+
422
+ if (isDir) {
423
+ return reject (@" EISDIR" , [NSString stringWithFormat: @" Expecting a file but '%@ ' is a directory" , path], nil );
424
+ }
425
+
426
+ if (!exists) {
412
427
[fm createDirectoryAtPath: folder withIntermediateDirectories: YES attributes: NULL error: &err];
413
428
if (err != nil ) {
414
- reject (@" EUNSPECIFIED" , [err description ], nil );
415
- return ;
429
+ return reject (@" EUNSPECIFIED" , @[[NSString stringWithFormat: @" Failed to create parent directory '%@ ', error: %@ " , path, [err description ]]]], nil );
416
430
}
417
431
}
418
432
@@ -423,8 +437,11 @@ + (void) writeFileArray:(NSString *)path
423
437
bytes[i] = [[data objectAtIndex: i] charValue ];
424
438
}
425
439
[fileContent appendBytes: bytes length: data.count];
426
- if (![fm fileExistsAtPath: path]) {
427
- [fm createFileAtPath: path contents: fileContent attributes: NULL ];
440
+
441
+ if (!exists) {
442
+ if (![fm createFileAtPath: path contents: fileContent attributes: NULL ]) {
443
+ return reject (@" ENOENT" , [NSString stringWithFormat: @" File '%@ ' does not exist and could not be created" , path], nil );
444
+ }
428
445
}
429
446
// if file exists, write file
430
447
else {
@@ -513,73 +530,78 @@ + (void) readFile:(NSString *)path
513
530
514
531
# pragma mark - hash
515
532
516
- RCT_EXPORT_METHOD (hash:(NSString *)filepath
533
+ RCT_EXPORT_METHOD (hash:(NSString *)path
517
534
algorithm:(NSString *)algorithm
518
535
resolver:(RCTPromiseResolveBlock)resolve
519
536
rejecter:(RCTPromiseRejectBlock)reject)
520
537
{
521
- BOOL fileExists = [[NSFileManager defaultManager ] fileExistsAtPath: filepath];
522
-
523
- if (!fileExists) {
524
- return reject (@" ENOENT" , [NSString stringWithFormat: @" No such file '%@ '" , filepath], nil );
525
- }
538
+ BOOL isDir = NO ;
539
+ BOOL exists = NO ;
540
+ exists = [[NSFileManager defaultManager ] fileExistsAtPath: path isDirectory: &isDir];
526
541
527
- NSError *error = nil ;
542
+ if (isDir) {
543
+ return reject (@" EISDIR" , [NSString stringWithFormat: @" Expecting a file but '%@ ' is a directory" , path], nil );
544
+ }
545
+ if (!fileExists) {
546
+ return reject (@" ENOENT" , [NSString stringWithFormat: @" No such file '%@ '" , path], nil );
547
+ }
528
548
529
- NSDictionary *attributes = [[ NSFileManager defaultManager ] attributesOfItemAtPath: filepath error: &error] ;
549
+ NSError *error = nil ;
530
550
531
- if (error) {
532
- return [self reject: reject withError: error];
533
- }
551
+ NSDictionary *attributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: path error: &error];
534
552
535
- if ([attributes objectForKey: NSFileType ] == NSFileTypeDirectory ) {
536
- return reject ( @" EISDIR " , [ NSString stringWithFormat: @" Expecting a file but ' %@ ' is a directory " , filepath], nil ) ;
537
- }
553
+ if (error ) {
554
+ return [ self reject: reject withError: error] ;
555
+ }
538
556
539
- NSData *content = [[NSFileManager defaultManager ] contentsAtPath: filepath];
557
+ if ([attributes objectForKey: NSFileType ] == NSFileTypeDirectory ) {
558
+ return reject (@" EISDIR" , [NSString stringWithFormat: @" Expecting a file but '%@ ' is a directory" , path], nil );
559
+ }
540
560
541
- NSArray *keys = [NSArray arrayWithObjects: @" md5 " , @" sha1 " , @" sha224 " , @" sha256 " , @" sha384 " , @" sha512 " , nil ];
561
+ NSData *content = [[ NSFileManager defaultManager ] contentsAtPath: path ];
542
562
543
- NSArray *digestLengths = [NSArray arrayWithObjects:
544
- @CC_MD5_DIGEST_LENGTH,
545
- @CC_SHA1_DIGEST_LENGTH,
546
- @CC_SHA224_DIGEST_LENGTH,
547
- @CC_SHA256_DIGEST_LENGTH,
548
- @CC_SHA384_DIGEST_LENGTH,
549
- @CC_SHA512_DIGEST_LENGTH,
550
- nil ];
563
+ NSArray *keys = [NSArray arrayWithObjects: @" md5" , @" sha1" , @" sha224" , @" sha256" , @" sha384" , @" sha512" , nil ];
551
564
552
- NSDictionary *keysToDigestLengths = [NSDictionary dictionaryWithObjects: digestLengths forKeys: keys];
565
+ NSArray *digestLengths = [NSArray arrayWithObjects:
566
+ @CC_MD5_DIGEST_LENGTH,
567
+ @CC_SHA1_DIGEST_LENGTH,
568
+ @CC_SHA224_DIGEST_LENGTH,
569
+ @CC_SHA256_DIGEST_LENGTH,
570
+ @CC_SHA384_DIGEST_LENGTH,
571
+ @CC_SHA512_DIGEST_LENGTH,
572
+ nil ];
553
573
554
- int digestLength = [[keysToDigestLengths objectForKey: algorithm] intValue ];
574
+ NSDictionary *keysToDigestLengths = [NSDictionary dictionaryWithObjects: digestLengths forKeys: keys ];
555
575
556
- if (!digestLength) {
557
- return reject (@" EINVAL" , [NSString stringWithFormat: @" Invalid algorithm '%@ ', must be one of md5, sha1, sha224, sha256, sha384, sha512" , algorithm], nil );
558
- }
576
+ int digestLength = [[keysToDigestLengths objectForKey: algorithm] intValue ];
559
577
560
- unsigned char buffer[digestLength];
578
+ if (!digestLength) {
579
+ return reject (@" EINVAL" , [NSString stringWithFormat: @" Invalid algorithm '%@ ', must be one of md5, sha1, sha224, sha256, sha384, sha512" , algorithm], nil );
580
+ }
561
581
562
- if ([algorithm isEqualToString: @" md5" ]) {
563
- CC_MD5 (content.bytes , (CC_LONG)content.length , buffer);
564
- } else if ([algorithm isEqualToString: @" sha1" ]) {
565
- CC_SHA1 (content.bytes , (CC_LONG)content.length , buffer);
566
- } else if ([algorithm isEqualToString: @" sha224" ]) {
567
- CC_SHA224 (content.bytes , (CC_LONG)content.length , buffer);
568
- } else if ([algorithm isEqualToString: @" sha256" ]) {
569
- CC_SHA256 (content.bytes , (CC_LONG)content.length , buffer);
570
- } else if ([algorithm isEqualToString: @" sha384" ]) {
571
- CC_SHA384 (content.bytes , (CC_LONG)content.length , buffer);
572
- } else if ([algorithm isEqualToString: @" sha512" ]) {
573
- CC_SHA512 (content.bytes , (CC_LONG)content.length , buffer);
574
- } else {
575
- return reject (@" EINVAL" , [NSString stringWithFormat: @" Invalid algorithm '%@ ', must be one of md5, sha1, sha224, sha256, sha384, sha512" , algorithm], nil );
576
- }
582
+ unsigned char buffer[digestLength];
583
+
584
+ if ([algorithm isEqualToString: @" md5" ]) {
585
+ CC_MD5 (content.bytes , (CC_LONG)content.length , buffer);
586
+ } else if ([algorithm isEqualToString: @" sha1" ]) {
587
+ CC_SHA1 (content.bytes , (CC_LONG)content.length , buffer);
588
+ } else if ([algorithm isEqualToString: @" sha224" ]) {
589
+ CC_SHA224 (content.bytes , (CC_LONG)content.length , buffer);
590
+ } else if ([algorithm isEqualToString: @" sha256" ]) {
591
+ CC_SHA256 (content.bytes , (CC_LONG)content.length , buffer);
592
+ } else if ([algorithm isEqualToString: @" sha384" ]) {
593
+ CC_SHA384 (content.bytes , (CC_LONG)content.length , buffer);
594
+ } else if ([algorithm isEqualToString: @" sha512" ]) {
595
+ CC_SHA512 (content.bytes , (CC_LONG)content.length , buffer);
596
+ } else {
597
+ return reject (@" EINVAL" , [NSString stringWithFormat: @" Invalid algorithm '%@ ', must be one of md5, sha1, sha224, sha256, sha384, sha512" , algorithm], nil );
598
+ }
577
599
578
- NSMutableString *output = [NSMutableString stringWithCapacity: digestLength * 2 ];
579
- for (int i = 0 ; i < digestLength; i++)
580
- [output appendFormat: @" %02x " ,buffer[i]];
600
+ NSMutableString *output = [NSMutableString stringWithCapacity: digestLength * 2 ];
601
+ for (int i = 0 ; i < digestLength; i++)
602
+ [output appendFormat: @" %02x " ,buffer[i]];
581
603
582
- resolve (output);
604
+ resolve (output);
583
605
}
584
606
585
607
# pragma mark - mkdir
@@ -740,7 +762,9 @@ + (void)slice:(NSString *)path
740
762
long max = MIN (size, [end longValue ]);
741
763
742
764
if (![fm fileExistsAtPath: dest]) {
743
- [fm createFileAtPath: dest contents: @" " attributes: nil ];
765
+ if (![fm createFileAtPath: dest contents: @" " attributes: nil ]) {
766
+ return reject (@" ENOENT" , [NSString stringWithFormat: @" File '%@ ' does not exist and could not be created" , path], nil );
767
+ }
744
768
}
745
769
[handle seekToFileOffset: [start longValue ]];
746
770
while (read < expected) {
0 commit comments