|
@@ -15,7 +15,7 @@
|
15
|
15
|
#import "RNFetchBlobFS.h"
|
16
|
16
|
#import "RNFetchBlobConst.h"
|
17
|
17
|
@import AssetsLibrary;
|
18
|
|
-@import Photos;
|
|
18
|
+
|
19
|
19
|
|
20
|
20
|
NSMutableDictionary *fileStreams = nil;
|
21
|
21
|
|
|
@@ -54,9 +54,9 @@ NSMutableDictionary *fileStreams = nil;
|
54
|
54
|
+(NSString *) getPathOfAsset:(NSString *)assetURI
|
55
|
55
|
{
|
56
|
56
|
// get file path of an app asset
|
57
|
|
- if([assetURI hasPrefix:@"bundle-assets://"])
|
|
57
|
+ if([assetURI hasPrefix:ASSET_PREFIX])
|
58
|
58
|
{
|
59
|
|
- assetURI = [assetURI stringByReplacingOccurrencesOfString:@"bundle-assets://" withString:@""];
|
|
59
|
+ assetURI = [assetURI stringByReplacingOccurrencesOfString:ASSET_PREFIX withString:@""];
|
60
|
60
|
assetURI = [[NSBundle mainBundle] pathForResource: [assetURI stringByDeletingPathExtension]
|
61
|
61
|
ofType: [assetURI pathExtension]];
|
62
|
62
|
}
|
|
@@ -98,7 +98,7 @@ NSMutableDictionary *fileStreams = nil;
|
98
|
98
|
return tempPath;
|
99
|
99
|
}
|
100
|
100
|
|
101
|
|
-- (void) startAssetReadStream:(NSData *)assetUrl
|
|
101
|
+- (void) startAssetReadStream:(NSString *)assetUrl
|
102
|
102
|
{
|
103
|
103
|
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
|
104
|
104
|
{
|
|
@@ -185,34 +185,6 @@ NSMutableDictionary *fileStreams = nil;
|
185
|
185
|
}
|
186
|
186
|
}
|
187
|
187
|
|
188
|
|
-// read system asset file
|
189
|
|
-+ (void) readAssetFile:(NSData *)assetUrl completionBlock:(void(^)(NSData * content))completionBlock failBlock:(void(^)(NSError * err))failBlock
|
190
|
|
-{
|
191
|
|
-
|
192
|
|
- ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
|
193
|
|
- {
|
194
|
|
- ALAssetRepresentation *rep = [myasset defaultRepresentation];
|
195
|
|
- Byte *buffer = (Byte*)malloc(rep.size);
|
196
|
|
- NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
|
197
|
|
- NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
|
198
|
|
- completionBlock(data);
|
199
|
|
- };
|
200
|
|
-
|
201
|
|
- ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error)
|
202
|
|
- {
|
203
|
|
- failBlock(error);
|
204
|
|
- };
|
205
|
|
-
|
206
|
|
- if(assetUrl && [assetUrl length])
|
207
|
|
- {
|
208
|
|
- NSURL *asseturl = [NSURL URLWithString:assetUrl];
|
209
|
|
- ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
|
210
|
|
- [assetslibrary assetForURL:asseturl
|
211
|
|
- resultBlock:resultblock
|
212
|
|
- failureBlock:failureblock];
|
213
|
|
- }
|
214
|
|
-}
|
215
|
|
-
|
216
|
188
|
+ (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
217
|
189
|
@try {
|
218
|
190
|
NSFileManager * fm = [NSFileManager defaultManager];
|
|
@@ -301,68 +273,62 @@ NSMutableDictionary *fileStreams = nil;
|
301
|
273
|
}
|
302
|
274
|
}
|
303
|
275
|
|
304
|
|
-+ (void) readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
|
276
|
++ (void) readFile:(NSString *)path encoding:(NSString *)encoding
|
|
277
|
+ resolver:(RCTPromiseResolveBlock)resolve
|
|
278
|
+ rejecter:(RCTPromiseRejectBlock)reject
|
|
279
|
+ onComplete:(void (^)(NSData * content))onComplete
|
|
280
|
+{
|
305
|
281
|
@try
|
306
|
282
|
{
|
307
|
|
- // before start reading file, we have to check if the `path` contains any special prefix
|
308
|
|
- // if the `path` begins with the following prefix then it will need special handling.
|
309
|
|
- // "assets-library://" this kind of path usually comes from camera roll, should use it's own readFile implementation
|
310
|
|
- // "bundle-assets://" this means an asset inside app bundle, usually we only have to convert it into normal file path
|
311
|
|
- if([path hasPrefix:@"assets-library://"])
|
312
|
|
- {
|
313
|
|
- [[self class] readAssetFile:path completionBlock:^(NSData * content)
|
|
283
|
+ [[self class] getPathFromUri:path completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
|
|
284
|
+ NSData * fileContent;
|
|
285
|
+ NSError * err;
|
|
286
|
+ if(asset != nil)
|
|
287
|
+ {
|
|
288
|
+ Byte * buffer = malloc(asset.size);
|
|
289
|
+ [asset getBytes:buffer fromOffset:0 length:asset.size error:&err];
|
|
290
|
+ if(err != nil)
|
|
291
|
+ {
|
|
292
|
+ reject(@"RNFetchBlobFS readFile error", @"failed to read asset", [err localizedDescription]);
|
|
293
|
+ return;
|
|
294
|
+ }
|
|
295
|
+ fileContent = [NSData dataWithBytes:buffer length:asset.size];
|
|
296
|
+ if(onComplete != nil)
|
|
297
|
+ onComplete(fileContent);
|
|
298
|
+ free(buffer);
|
|
299
|
+ }
|
|
300
|
+ else
|
314
|
301
|
{
|
315
|
|
- if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
|
316
|
|
- resolve([[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding]);
|
|
302
|
+ BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path];
|
|
303
|
+ if(!exists) {
|
|
304
|
+ reject(@"RNFetchBlobFS readFile error", @"file not exists", path);
|
|
305
|
+ return;
|
317
|
306
|
}
|
318
|
|
- else if ([[encoding lowercaseString] isEqualToString:@"base64"]) {
|
319
|
|
- resolve([content base64EncodedStringWithOptions:0]);
|
|
307
|
+ fileContent = [NSData dataWithContentsOfFile:path];
|
|
308
|
+ }
|
|
309
|
+
|
|
310
|
+ if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
|
|
311
|
+ if(resolve != nil)
|
|
312
|
+ resolve([[NSString alloc] initWithData:fileContent encoding:NSUTF8StringEncoding]);
|
|
313
|
+ }
|
|
314
|
+ else if ([[encoding lowercaseString] isEqualToString:@"base64"]) {
|
|
315
|
+ if(resolve != nil)
|
|
316
|
+ resolve([fileContent base64EncodedStringWithOptions:0]);
|
|
317
|
+ }
|
|
318
|
+ else if ([[encoding lowercaseString] isEqualToString:@"ascii"]) {
|
|
319
|
+ NSMutableArray * resultArray = [NSMutableArray array];
|
|
320
|
+ char * bytes = [fileContent bytes];
|
|
321
|
+ for(int i=0;i<[fileContent length];i++) {
|
|
322
|
+ [resultArray addObject:[NSNumber numberWithChar:bytes[i]]];
|
320
|
323
|
}
|
321
|
|
- else if ([[encoding lowercaseString] isEqualToString:@"ascii"]) {
|
322
|
|
- NSMutableArray * resultArray = [NSMutableArray array];
|
323
|
|
- char * bytes = [content bytes];
|
324
|
|
- for(int i=0;i<[content length];i++) {
|
325
|
|
- [resultArray addObject:[NSNumber numberWithChar:bytes[i]]];
|
326
|
|
- }
|
|
324
|
+ if(resolve != nil)
|
327
|
325
|
resolve(resultArray);
|
328
|
|
- }
|
329
|
|
- } failBlock:^(NSError *err) {
|
330
|
|
- @throw @"RNFetchBlobFS readFile error", @"failed to read asset", path;
|
331
|
|
- }];
|
332
|
|
- return ;
|
333
|
|
- }
|
334
|
|
-
|
335
|
|
- // normalize the file path
|
336
|
|
- path = [[self class]getPathOfAsset:path];
|
337
|
|
-
|
338
|
|
- NSFileManager * fm = [NSFileManager defaultManager];
|
339
|
|
- NSError *err = nil;
|
340
|
|
- BOOL exists = [fm fileExistsAtPath:path];
|
341
|
|
- if(!exists) {
|
342
|
|
- @throw @"RNFetchBlobFS readFile error", @"file not exists", path;
|
343
|
|
- return;
|
344
|
|
- }
|
345
|
|
- if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
|
346
|
|
- NSString * utf8Result = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
|
347
|
|
- resolve(utf8Result);
|
348
|
|
- }
|
349
|
|
- else if ([[encoding lowercaseString] isEqualToString:@"base64"]) {
|
350
|
|
- NSData * fileData = [NSData dataWithContentsOfFile:path];
|
351
|
|
- resolve([fileData base64EncodedStringWithOptions:0]);
|
352
|
|
- }
|
353
|
|
- else if ([[encoding lowercaseString] isEqualToString:@"ascii"]) {
|
354
|
|
- NSData * resultData = [NSData dataWithContentsOfFile:path];
|
355
|
|
- NSMutableArray * resultArray = [NSMutableArray array];
|
356
|
|
- char * bytes = [resultData bytes];
|
357
|
|
- for(int i=0;i<[resultData length];i++) {
|
358
|
|
- [resultArray addObject:[NSNumber numberWithChar:bytes[i]]];
|
359
|
326
|
}
|
360
|
|
- resolve(resultArray);
|
361
|
|
- }
|
362
|
|
-
|
|
327
|
+ }];
|
363
|
328
|
}
|
364
|
329
|
@catch(NSException * e)
|
365
|
330
|
{
|
|
331
|
+ if(reject != nil)
|
366
|
332
|
reject(@"RNFetchBlobFS readFile error", @"error", [e description]);
|
367
|
333
|
}
|
368
|
334
|
}
|
|
@@ -486,9 +452,9 @@ NSMutableDictionary *fileStreams = nil;
|
486
|
452
|
self.path = path;
|
487
|
453
|
self.bufferSize = bufferSize;
|
488
|
454
|
|
489
|
|
- if([path hasPrefix:@"assets-library://"])
|
|
455
|
+ if([path hasPrefix:AL_PREFIX])
|
490
|
456
|
{
|
491
|
|
-
|
|
457
|
+ [self startAssetReadStream:path];
|
492
|
458
|
return;
|
493
|
459
|
}
|
494
|
460
|
|
|
@@ -631,4 +597,24 @@ NSMutableDictionary *fileStreams = nil;
|
631
|
597
|
|
632
|
598
|
}
|
633
|
599
|
|
|
600
|
++ (void) getPathFromUri:(NSString *)uri completionHandler:(void(^)(NSString * path, ALAssetRepresentation *asset)) onComplete
|
|
601
|
+{
|
|
602
|
+ if([uri hasPrefix:AL_PREFIX])
|
|
603
|
+ {
|
|
604
|
+ NSURL *asseturl = [NSURL URLWithString:uri];
|
|
605
|
+ ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
|
|
606
|
+ [assetslibrary assetForURL:asseturl
|
|
607
|
+ resultBlock:^(ALAsset *asset) {
|
|
608
|
+ onComplete(nil, [asset defaultRepresentation]);
|
|
609
|
+ }
|
|
610
|
+ failureBlock:^(NSError *error) {
|
|
611
|
+ onComplete(nil, nil);
|
|
612
|
+ }];
|
|
613
|
+ }
|
|
614
|
+ else
|
|
615
|
+ {
|
|
616
|
+ onComplete([[self class] getPathOfAsset:uri], nil);
|
|
617
|
+ }
|
|
618
|
+}
|
|
619
|
+
|
634
|
620
|
@end
|