|
@@ -64,7 +64,7 @@ NSMutableDictionary *fileStreams = nil;
|
64
|
64
|
|
65
|
65
|
// static member getter
|
66
|
66
|
+ (NSArray *) getFileStreams {
|
67
|
|
-
|
|
67
|
+
|
68
|
68
|
if(fileStreams == nil)
|
69
|
69
|
fileStreams = [[NSMutableDictionary alloc] init];
|
70
|
70
|
return fileStreams;
|
|
@@ -115,12 +115,12 @@ NSMutableDictionary *fileStreams = nil;
|
115
|
115
|
}
|
116
|
116
|
|
117
|
117
|
+ (NSString *) getTempPath {
|
118
|
|
-
|
|
118
|
+
|
119
|
119
|
return NSTemporaryDirectory();
|
120
|
120
|
}
|
121
|
121
|
|
122
|
122
|
+ (NSString *) getTempPath:(NSString*)taskId withExtension:(NSString *)ext {
|
123
|
|
-
|
|
123
|
+
|
124
|
124
|
NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
|
125
|
125
|
NSString * filename = [NSString stringWithFormat:@"/RNFetchBlob_tmp/RNFetchBlobTmp_%@", taskId];
|
126
|
126
|
if(ext != nil)
|
|
@@ -129,7 +129,7 @@ NSMutableDictionary *fileStreams = nil;
|
129
|
129
|
return tempPath;
|
130
|
130
|
}
|
131
|
131
|
|
132
|
|
-#pragma margk - readStream
|
|
132
|
+#pragma margk - readStream
|
133
|
133
|
|
134
|
134
|
+ (void) readStream:(NSString *)uri
|
135
|
135
|
encoding:(NSString * )encoding
|
|
@@ -139,7 +139,7 @@ NSMutableDictionary *fileStreams = nil;
|
139
|
139
|
bridgeRef:(RCTBridge *)bridgeRef
|
140
|
140
|
{
|
141
|
141
|
[[self class] getPathFromUri:uri completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
|
142
|
|
-
|
|
142
|
+
|
143
|
143
|
__block RCTEventDispatcher * event = bridgeRef.eventDispatcher;
|
144
|
144
|
__block int read = 0;
|
145
|
145
|
__block int backoff = tick *1000;
|
|
@@ -193,7 +193,7 @@ NSMutableDictionary *fileStreams = nil;
|
193
|
193
|
// release buffer
|
194
|
194
|
if(buffer != nil)
|
195
|
195
|
free(buffer);
|
196
|
|
-
|
|
196
|
+
|
197
|
197
|
}
|
198
|
198
|
@catch (NSError * err)
|
199
|
199
|
{
|
|
@@ -205,10 +205,10 @@ NSMutableDictionary *fileStreams = nil;
|
205
|
205
|
NSDictionary * payload = @{ @"event": FS_EVENT_END, @"detail": @"" };
|
206
|
206
|
[event sendDeviceEventWithName:streamId body:payload];
|
207
|
207
|
}
|
208
|
|
-
|
|
208
|
+
|
209
|
209
|
}];
|
210
|
|
-
|
211
|
|
-
|
|
210
|
+
|
|
211
|
+
|
212
|
212
|
}
|
213
|
213
|
|
214
|
214
|
// send read stream chunks via native event emitter
|
|
@@ -243,8 +243,8 @@ NSMutableDictionary *fileStreams = nil;
|
243
|
243
|
NSDictionary * payload = @{ @"event": FS_EVENT_DATA, @"detail" : asciiArray };
|
244
|
244
|
[event sendDeviceEventWithName:streamId body:payload];
|
245
|
245
|
}
|
246
|
|
-
|
247
|
|
-
|
|
246
|
+
|
|
247
|
+
|
248
|
248
|
}
|
249
|
249
|
|
250
|
250
|
# pragma write file from file
|
|
@@ -274,7 +274,7 @@ NSMutableDictionary *fileStreams = nil;
|
274
|
274
|
}
|
275
|
275
|
else if(asset != nil)
|
276
|
276
|
{
|
277
|
|
-
|
|
277
|
+
|
278
|
278
|
__block NSOutputStream * os = [[NSOutputStream alloc] initToFileAtPath:dest append:append];
|
279
|
279
|
int read = 0;
|
280
|
280
|
int cursor = 0;
|
|
@@ -293,7 +293,7 @@ NSMutableDictionary *fileStreams = nil;
|
293
|
293
|
else
|
294
|
294
|
callback(@"failed to resolve path", nil);
|
295
|
295
|
}];
|
296
|
|
-
|
|
296
|
+
|
297
|
297
|
return 0;
|
298
|
298
|
}
|
299
|
299
|
|
|
@@ -342,7 +342,7 @@ NSMutableDictionary *fileStreams = nil;
|
342
|
342
|
[content writeToFile:path atomically:YES];
|
343
|
343
|
}
|
344
|
344
|
fm = nil;
|
345
|
|
-
|
|
345
|
+
|
346
|
346
|
resolve([NSNumber numberWithInteger:[content length]]);
|
347
|
347
|
}
|
348
|
348
|
@catch (NSException * e)
|
|
@@ -423,16 +423,16 @@ NSMutableDictionary *fileStreams = nil;
|
423
|
423
|
else
|
424
|
424
|
{
|
425
|
425
|
if(![[NSFileManager defaultManager] fileExistsAtPath:path]) {
|
426
|
|
-
|
|
426
|
+
|
427
|
427
|
reject(@"RNFetchBlobFS readFile error", @"file not exists", nil);
|
428
|
428
|
return;
|
429
|
429
|
}
|
430
|
430
|
fileContent = [NSData dataWithContentsOfFile:path];
|
431
|
|
-
|
|
431
|
+
|
432
|
432
|
}
|
433
|
433
|
if(onComplete != nil)
|
434
|
434
|
onComplete(fileContent);
|
435
|
|
-
|
|
435
|
+
|
436
|
436
|
if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
|
437
|
437
|
if(resolve != nil) {
|
438
|
438
|
NSString * utf8 = [[NSString alloc] initWithData:fileContent encoding:NSUTF8StringEncoding];
|
|
@@ -480,7 +480,7 @@ NSMutableDictionary *fileStreams = nil;
|
480
|
480
|
|
481
|
481
|
+ (NSDictionary *) stat:(NSString *) path error:(NSError **) error {
|
482
|
482
|
|
483
|
|
-
|
|
483
|
+
|
484
|
484
|
BOOL isDir = NO;
|
485
|
485
|
NSFileManager * fm = [NSFileManager defaultManager];
|
486
|
486
|
if([fm fileExistsAtPath:path isDirectory:&isDir] == NO) {
|
|
@@ -498,7 +498,7 @@ NSMutableDictionary *fileStreams = nil;
|
498
|
498
|
@"lastModified" : [NSNumber numberWithLong:(time_t) [lastModified timeIntervalSince1970]*1000],
|
499
|
499
|
@"type" : isDir ? @"directory" : @"file"
|
500
|
500
|
};
|
501
|
|
-
|
|
501
|
+
|
502
|
502
|
}
|
503
|
503
|
|
504
|
504
|
# pragma mark - exists
|
|
@@ -584,7 +584,7 @@ NSMutableDictionary *fileStreams = nil;
|
584
|
584
|
[self.outStream close];
|
585
|
585
|
self.outStream = nil;
|
586
|
586
|
}
|
587
|
|
-
|
|
587
|
+
|
588
|
588
|
}
|
589
|
589
|
|
590
|
590
|
// Slice a file into another file, generally for support Blob implementation.
|
|
@@ -614,14 +614,14 @@ NSMutableDictionary *fileStreams = nil;
|
614
|
614
|
}
|
615
|
615
|
long size = [fm attributesOfItemAtPath:path error:nil].fileSize;
|
616
|
616
|
long max = MIN(size, [end longValue]);
|
617
|
|
-
|
|
617
|
+
|
618
|
618
|
if(![fm fileExistsAtPath:dest]) {
|
619
|
619
|
[fm createFileAtPath:dest contents:@"" attributes:nil];
|
620
|
620
|
}
|
621
|
621
|
[handle seekToFileOffset:[start longValue]];
|
622
|
622
|
while(read < expected)
|
623
|
623
|
{
|
624
|
|
-
|
|
624
|
+
|
625
|
625
|
NSData * chunk;
|
626
|
626
|
long chunkSize = 0;
|
627
|
627
|
if([start longValue] + read + 10240 > max)
|
|
@@ -639,7 +639,7 @@ NSMutableDictionary *fileStreams = nil;
|
639
|
639
|
if([chunk length] <= 0)
|
640
|
640
|
break;
|
641
|
641
|
long remain = expected - read;
|
642
|
|
-
|
|
642
|
+
|
643
|
643
|
[os write:[chunk bytes] maxLength:chunkSize];
|
644
|
644
|
read += [chunk length];
|
645
|
645
|
}
|
|
@@ -656,10 +656,10 @@ NSMutableDictionary *fileStreams = nil;
|
656
|
656
|
[os open];
|
657
|
657
|
long size = asset.size;
|
658
|
658
|
long max = MIN(size, [end longValue]);
|
659
|
|
-
|
|
659
|
+
|
660
|
660
|
while(read < expected)
|
661
|
661
|
{
|
662
|
|
-
|
|
662
|
+
|
663
|
663
|
uint8_t * chunk[10240];
|
664
|
664
|
long chunkSize = 0;
|
665
|
665
|
if([start longValue] + read + 10240 > max)
|
|
@@ -677,7 +677,7 @@ NSMutableDictionary *fileStreams = nil;
|
677
|
677
|
if( chunkRead <= 0)
|
678
|
678
|
break;
|
679
|
679
|
long remain = expected - read;
|
680
|
|
-
|
|
680
|
+
|
681
|
681
|
[os write:chunk maxLength:chunkSize];
|
682
|
682
|
read += chunkRead;
|
683
|
683
|
}
|
|
@@ -688,7 +688,7 @@ NSMutableDictionary *fileStreams = nil;
|
688
|
688
|
{
|
689
|
689
|
reject(@"slice error", [NSString stringWithFormat: @"could not resolve URI %@", path ], nil);
|
690
|
690
|
}
|
691
|
|
-
|
|
691
|
+
|
692
|
692
|
}];
|
693
|
693
|
}
|
694
|
694
|
|
|
@@ -701,7 +701,7 @@ NSMutableDictionary *fileStreams = nil;
|
701
|
701
|
[[RNFetchBlobFS getFileStreams] setValue:nil forKey:self.streamId];
|
702
|
702
|
self.streamId = nil;
|
703
|
703
|
}
|
704
|
|
-
|
|
704
|
+
|
705
|
705
|
}
|
706
|
706
|
|
707
|
707
|
|
|
@@ -737,7 +737,7 @@ NSMutableDictionary *fileStreams = nil;
|
737
|
737
|
NSError *error = nil;
|
738
|
738
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
739
|
739
|
NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error: &error];
|
740
|
|
-
|
|
740
|
+
|
741
|
741
|
if (dictionary) {
|
742
|
742
|
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemSize];
|
743
|
743
|
NSNumber *freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
|
|
@@ -750,7 +750,7 @@ NSMutableDictionary *fileStreams = nil;
|
750
|
750
|
} else {
|
751
|
751
|
callback(@[@"failed to get storage usage."]);
|
752
|
752
|
}
|
753
|
|
-
|
|
753
|
+
|
754
|
754
|
}
|
755
|
755
|
|
756
|
756
|
+ (void) writeAssetToPath:(ALAssetRepresentation * )rep dest:(NSString *)dest
|