Geen omschrijving

RNFetchBlob.m 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. //
  2. // RNFetchBlob.m
  3. //
  4. // Created by wkh237 on 2016/4/28.
  5. //
  6. #import "RNFetchBlob.h"
  7. #import "RNFetchBlobFS.h"
  8. #import "RNFetchBlobNetwork.h"
  9. #import "RNFetchBlobConst.h"
  10. #import "RNFetchBlobReqBuilder.h"
  11. #import "RNFetchBlobProgress.h"
  12. __strong RCTBridge * bridgeRef;
  13. dispatch_queue_t commonTaskQueue;
  14. dispatch_queue_t fsQueue;
  15. ////////////////////////////////////////
  16. //
  17. // Exported native methods
  18. //
  19. ////////////////////////////////////////
  20. #pragma mark RNFetchBlob exported methods
  21. @implementation RNFetchBlob
  22. @synthesize filePathPrefix;
  23. @synthesize documentController;
  24. @synthesize bridge = _bridge;
  25. - (dispatch_queue_t) methodQueue {
  26. if(commonTaskQueue == nil)
  27. commonTaskQueue = dispatch_queue_create("RNFetchBlob.queue", DISPATCH_QUEUE_SERIAL);
  28. return commonTaskQueue;
  29. }
  30. + (RCTBridge *)getRCTBridge
  31. {
  32. RCTRootView * rootView = (RCTRootView*) [[UIApplication sharedApplication] keyWindow].rootViewController.view;
  33. return rootView.bridge;
  34. }
  35. RCT_EXPORT_MODULE();
  36. - (id) init {
  37. self = [super init];
  38. self.filePathPrefix = FILE_PREFIX;
  39. if(commonTaskQueue == nil)
  40. commonTaskQueue = dispatch_queue_create("RNFetchBlob.queue", DISPATCH_QUEUE_SERIAL);
  41. if(fsQueue == nil)
  42. fsQueue = dispatch_queue_create("RNFetchBlob.fs.queue", DISPATCH_QUEUE_SERIAL);
  43. BOOL isDir;
  44. // if temp folder not exists, create one
  45. if(![[NSFileManager defaultManager] fileExistsAtPath: [RNFetchBlobFS getTempPath] isDirectory:&isDir]) {
  46. [[NSFileManager defaultManager] createDirectoryAtPath:[RNFetchBlobFS getTempPath] withIntermediateDirectories:YES attributes:nil error:NULL];
  47. }
  48. bridgeRef = _bridge;
  49. [RNFetchBlobNetwork emitExpiredTasks];
  50. return self;
  51. }
  52. - (NSDictionary *)constantsToExport
  53. {
  54. return @{
  55. @"MainBundleDir" : [RNFetchBlobFS getMainBundleDir],
  56. @"DocumentDir": [RNFetchBlobFS getDocumentDir],
  57. @"CacheDir" : [RNFetchBlobFS getCacheDir]
  58. };
  59. }
  60. // Fetch blob data request
  61. RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
  62. taskId:(NSString *)taskId
  63. method:(NSString *)method
  64. url:(NSString *)url
  65. headers:(NSDictionary *)headers
  66. form:(NSArray *)form
  67. callback:(RCTResponseSenderBlock)callback)
  68. {
  69. [RNFetchBlobReqBuilder buildMultipartRequest:options
  70. taskId:taskId
  71. method:method
  72. url:url
  73. headers:headers
  74. form:form
  75. onComplete:^(__weak NSURLRequest *req, long bodyLength)
  76. {
  77. // something went wrong when building the request body
  78. if(req == nil)
  79. {
  80. callback(@[@"RNFetchBlob.fetchBlobForm failed to create request body"]);
  81. }
  82. // send HTTP request
  83. else
  84. {
  85. [[RNFetchBlobNetwork sharedInstance] sendRequest:options
  86. contentLength:bodyLength
  87. bridge:self.bridge
  88. taskId:taskId
  89. withRequest:req
  90. callback:callback];
  91. }
  92. }];
  93. }
  94. // Fetch blob data request
  95. RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
  96. taskId:(NSString *)taskId
  97. method:(NSString *)method
  98. url:(NSString *)url
  99. headers:(NSDictionary *)headers
  100. body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
  101. {
  102. [RNFetchBlobReqBuilder buildOctetRequest:options
  103. taskId:taskId
  104. method:method
  105. url:url
  106. headers:headers
  107. body:body
  108. onComplete:^(NSURLRequest *req, long bodyLength)
  109. {
  110. // something went wrong when building the request body
  111. if(req == nil)
  112. {
  113. callback(@[@"RNFetchBlob.fetchBlob failed to create request body"]);
  114. }
  115. // send HTTP request
  116. else
  117. {
  118. [[RNFetchBlobNetwork sharedInstance] sendRequest:options
  119. contentLength:bodyLength
  120. bridge:self.bridge
  121. taskId:taskId
  122. withRequest:req
  123. callback:callback];
  124. }
  125. }];
  126. }
  127. #pragma mark - fs.createFile
  128. RCT_EXPORT_METHOD(createFile:(NSString *)path
  129. data:(NSString *)data
  130. encoding:(NSString *)encoding
  131. resolver:(RCTPromiseResolveBlock)resolve
  132. rejecter:(RCTPromiseRejectBlock)reject)
  133. {
  134. NSFileManager * fm = [NSFileManager defaultManager];
  135. NSData * fileContent = nil;
  136. if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
  137. fileContent = [[NSData alloc] initWithData:[data dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
  138. }
  139. else if([[encoding lowercaseString] isEqualToString:@"base64"]) {
  140. fileContent = [[NSData alloc] initWithBase64EncodedData:data options:0];
  141. }
  142. else if([[encoding lowercaseString] isEqualToString:@"uri"]) {
  143. NSString * orgPath = [data stringByReplacingOccurrencesOfString:FILE_PREFIX withString:@""];
  144. fileContent = [[NSData alloc] initWithContentsOfFile:orgPath];
  145. }
  146. else {
  147. fileContent = [[NSData alloc] initWithData:[data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
  148. }
  149. if ([fm fileExistsAtPath:path]) {
  150. reject(@"EEXIST", [NSString stringWithFormat:@"File '%@' already exists", path], nil);
  151. }
  152. else {
  153. BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
  154. if(success == YES)
  155. resolve(@[[NSNull null]]);
  156. else
  157. reject(@"EUNSPECIFIED", [NSString stringWithFormat:@"Failed to create new file at path '%@', please ensure the folder exists", path], nil);
  158. }
  159. }
  160. #pragma mark - fs.createFileASCII
  161. // method for create file with ASCII content
  162. RCT_EXPORT_METHOD(createFileASCII:(NSString *)path
  163. data:(NSArray *)dataArray
  164. resolver:(RCTPromiseResolveBlock)resolve
  165. rejecter:(RCTPromiseRejectBlock)reject)
  166. {
  167. NSFileManager * fm = [NSFileManager defaultManager];
  168. NSMutableData * fileContent = [NSMutableData alloc];
  169. // prevent stack overflow, alloc on heap
  170. char * bytes = (char*) malloc([dataArray count]);
  171. for(int i = 0; i < dataArray.count; i++) {
  172. bytes[i] = [[dataArray objectAtIndex:i] charValue];
  173. }
  174. [fileContent appendBytes:bytes length:dataArray.count];
  175. if ([fm fileExistsAtPath:path]) {
  176. reject(@"EEXIST", [NSString stringWithFormat:@"File '%@' already exists", path], nil);
  177. }
  178. else {
  179. BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
  180. if(success == YES)
  181. resolve(@[[NSNull null]]);
  182. else
  183. reject(@"EUNSPECIFIED", [NSString stringWithFormat:@"failed to create new file at path '%@', please ensure the folder exists", path], nil);
  184. }
  185. free(bytes);
  186. }
  187. #pragma mark - fs.pathForAppGroup
  188. RCT_EXPORT_METHOD(pathForAppGroup:(NSString *)groupName
  189. resolver:(RCTPromiseResolveBlock)resolve
  190. rejecter:(RCTPromiseRejectBlock)reject)
  191. {
  192. NSString * path = [RNFetchBlobFS getPathForAppGroup:groupName];
  193. if(path) {
  194. resolve(path);
  195. } else {
  196. reject(@"EUNSPECIFIED", @"could not find path for app group", nil);
  197. }
  198. }
  199. #pragma mark - fs.exists
  200. RCT_EXPORT_METHOD(exists:(NSString *)path callback:(RCTResponseSenderBlock)callback) {
  201. [RNFetchBlobFS exists:path callback:callback];
  202. }
  203. #pragma mark - fs.writeFile
  204. RCT_EXPORT_METHOD(writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  205. {
  206. [RNFetchBlobFS writeFile:path encoding:[NSString stringWithString:encoding] data:data append:append resolver:resolve rejecter:reject];
  207. }
  208. #pragma mark - fs.writeArray
  209. RCT_EXPORT_METHOD(writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  210. {
  211. [RNFetchBlobFS writeFileArray:path data:data append:append resolver:resolve rejecter:reject];
  212. }
  213. #pragma mark - fs.writeStream
  214. RCT_EXPORT_METHOD(writeStream:(NSString *)path withEncoding:(NSString *)encoding appendData:(BOOL)append callback:(RCTResponseSenderBlock)callback)
  215. {
  216. RNFetchBlobFS * fileStream = [[RNFetchBlobFS alloc] initWithBridgeRef:self.bridge];
  217. NSFileManager * fm = [NSFileManager defaultManager];
  218. NSString * folder = [path stringByDeletingLastPathComponent];
  219. NSError* err = nil;
  220. BOOL isDir = NO;
  221. BOOL exists = [fm fileExistsAtPath:path isDirectory: &isDir];
  222. if(!exists) {
  223. [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
  224. if(err != nil) {
  225. callback(@[@"ENOTDIR", [NSString stringWithFormat:@"Failed to create parent directory of '%@'; error: %@", path, [err description]]]);
  226. }
  227. if(![fm createFileAtPath:path contents:nil attributes:nil]) {
  228. callback(@[@"ENOENT", [NSString stringWithFormat:@"File '%@' does not exist and could not be created", path]]);
  229. }
  230. }
  231. else if(isDir) {
  232. callback(@[@"EISDIR", [NSString stringWithFormat:@"Expecting a file but '%@' is a directory", path]]);
  233. }
  234. NSString * streamId = [fileStream openWithPath:path encode:encoding appendData:append];
  235. callback(@[[NSNull null], [NSNull null], streamId]);
  236. }
  237. #pragma mark - fs.writeArrayChunk
  238. RCT_EXPORT_METHOD(writeArrayChunk:(NSString *)streamId withArray:(NSArray *)dataArray callback:(RCTResponseSenderBlock) callback)
  239. {
  240. RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
  241. char * bytes = (char *) malloc([dataArray count]);
  242. for(int i = 0; i < dataArray.count; i++) {
  243. bytes[i] = [[dataArray objectAtIndex:i] charValue];
  244. }
  245. NSMutableData * data = [NSMutableData alloc];
  246. [data appendBytes:bytes length:dataArray.count];
  247. [fs write:data];
  248. free(bytes);
  249. callback(@[[NSNull null]]);
  250. }
  251. #pragma mark - fs.writeChunk
  252. RCT_EXPORT_METHOD(writeChunk:(NSString *)streamId withData:(NSString *)data callback:(RCTResponseSenderBlock) callback)
  253. {
  254. RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
  255. [fs writeEncodeChunk:data];
  256. callback(@[[NSNull null]]);
  257. }
  258. #pragma mark - fs.closeStream
  259. RCT_EXPORT_METHOD(closeStream:(NSString *)streamId callback:(RCTResponseSenderBlock) callback)
  260. {
  261. RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
  262. [fs closeOutStream];
  263. callback(@[[NSNull null], @YES]);
  264. }
  265. #pragma mark - unlink
  266. RCT_EXPORT_METHOD(unlink:(NSString *)path callback:(RCTResponseSenderBlock) callback)
  267. {
  268. NSError * error = nil;
  269. NSString * tmpPath = nil;
  270. [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
  271. if(error == nil || [[NSFileManager defaultManager] fileExistsAtPath:path] == NO)
  272. callback(@[[NSNull null]]);
  273. else
  274. callback(@[[NSString stringWithFormat:@"failed to unlink file or path at %@", path]]);
  275. }
  276. #pragma mark - fs.removeSession
  277. RCT_EXPORT_METHOD(removeSession:(NSArray *)paths callback:(RCTResponseSenderBlock) callback)
  278. {
  279. NSError * error = nil;
  280. NSString * tmpPath = nil;
  281. for(NSString * path in paths) {
  282. [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
  283. if(error != nil) {
  284. callback(@[[NSString stringWithFormat:@"failed to remove session path at %@", path]]);
  285. return;
  286. }
  287. }
  288. callback(@[[NSNull null]]);
  289. }
  290. #pragma mark - fs.ls
  291. RCT_EXPORT_METHOD(ls:(NSString *)path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  292. {
  293. NSFileManager* fm = [NSFileManager defaultManager];
  294. BOOL exist = nil;
  295. BOOL isDir = nil;
  296. exist = [fm fileExistsAtPath:path isDirectory:&isDir];
  297. if(exist == NO) {
  298. return reject(@"ENOENT", [NSString stringWithFormat:@"No such file '%@'", path], nil);
  299. }
  300. if(isDir == NO) {
  301. return reject(@"ENOTDIR", [NSString stringWithFormat:@"Not a directory '%@'", path], nil);
  302. }
  303. NSError * error = nil;
  304. NSArray * result = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
  305. if(error == nil)
  306. resolve(result);
  307. else
  308. reject(@"EUNSPECIFIED", [error description], nil);
  309. }
  310. #pragma mark - fs.stat
  311. RCT_EXPORT_METHOD(stat:(NSString *)target callback:(RCTResponseSenderBlock) callback)
  312. {
  313. [RNFetchBlobFS getPathFromUri:target completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
  314. __block NSMutableArray * result;
  315. if(path != nil)
  316. {
  317. NSFileManager* fm = [NSFileManager defaultManager];
  318. BOOL exist = nil;
  319. BOOL isDir = nil;
  320. NSError * error = nil;
  321. exist = [fm fileExistsAtPath:path isDirectory:&isDir];
  322. if(exist == NO) {
  323. callback(@[[NSString stringWithFormat:@"failed to stat path `%@` because it does not exist or it is not a folder", path]]);
  324. return ;
  325. }
  326. result = [RNFetchBlobFS stat:path error:&error];
  327. if(error == nil)
  328. callback(@[[NSNull null], result]);
  329. else
  330. callback(@[[error localizedDescription], [NSNull null]]);
  331. }
  332. else if(asset != nil)
  333. {
  334. __block NSNumber * size = [NSNumber numberWithLong:[asset size]];
  335. result = [asset metadata];
  336. [result setValue:size forKey:@"size"];
  337. callback(@[[NSNull null], result]);
  338. }
  339. else
  340. {
  341. callback(@[@"failed to stat path, could not resolve URI", [NSNull null]]);
  342. }
  343. }];
  344. }
  345. #pragma mark - fs.lstat
  346. RCT_EXPORT_METHOD(lstat:(NSString *)path callback:(RCTResponseSenderBlock) callback)
  347. {
  348. NSFileManager* fm = [NSFileManager defaultManager];
  349. BOOL exist = nil;
  350. BOOL isDir = nil;
  351. path = [RNFetchBlobFS getPathOfAsset:path];
  352. exist = [fm fileExistsAtPath:path isDirectory:&isDir];
  353. if(exist == NO) {
  354. callback(@[[NSString stringWithFormat:@"failed to lstat path `%@` because it does not exist or it is not a folder", path]]);
  355. return ;
  356. }
  357. NSError * error = nil;
  358. NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
  359. NSMutableArray * res = [[NSMutableArray alloc] init];
  360. if(isDir == YES) {
  361. for(NSString * p in files) {
  362. NSString * filePath = [NSString stringWithFormat:@"%@/%@", path, p];
  363. [res addObject:[RNFetchBlobFS stat:filePath error:&error]];
  364. }
  365. }
  366. else {
  367. [res addObject:[RNFetchBlobFS stat:path error:&error]];
  368. }
  369. if(error == nil)
  370. callback(@[[NSNull null], res == nil ? [NSNull null] :res ]);
  371. else
  372. callback(@[[error localizedDescription], [NSNull null]]);
  373. }
  374. #pragma mark - fs.cp
  375. RCT_EXPORT_METHOD(cp:(NSString*)src toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback)
  376. {
  377. // path = [RNFetchBlobFS getPathOfAsset:path];
  378. [RNFetchBlobFS getPathFromUri:src completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
  379. NSError * error = nil;
  380. if(path == nil)
  381. {
  382. [RNFetchBlobFS writeAssetToPath:asset dest:dest];
  383. callback(@[[NSNull null], @YES]);
  384. }
  385. else
  386. {
  387. // If the destination exists there will be an error
  388. BOOL result = [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
  389. if(error == nil)
  390. callback(@[[NSNull null], @YES]);
  391. else
  392. callback(@[[error localizedDescription], @NO]);
  393. }
  394. }];
  395. }
  396. #pragma mark - fs.mv
  397. RCT_EXPORT_METHOD(mv:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback)
  398. {
  399. NSError * error = nil;
  400. BOOL result = [[NSFileManager defaultManager] moveItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
  401. if(error == nil)
  402. callback(@[[NSNull null], @YES]);
  403. else
  404. callback(@[[error localizedDescription], @NO]);
  405. }
  406. #pragma mark - fs.mkdir
  407. RCT_EXPORT_METHOD(mkdir:(NSString *)path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  408. {
  409. [RNFetchBlobFS mkdir:path resolver:resolve rejecter:reject];
  410. }
  411. #pragma mark - fs.readFile
  412. RCT_EXPORT_METHOD(readFile:(NSString *)path
  413. encoding:(NSString *)encoding
  414. resolver:(RCTPromiseResolveBlock)resolve
  415. rejecter:(RCTPromiseRejectBlock)reject)
  416. {
  417. [RNFetchBlobFS readFile:path encoding:encoding onComplete:^(NSData * content, NSString * code, NSString * err) {
  418. if(err != nil) {
  419. reject(code, err, nil);
  420. return;
  421. }
  422. if(encoding == @"ascii") {
  423. resolve((NSMutableArray *)content);
  424. }
  425. else {
  426. resolve((NSString *)content);
  427. }
  428. }];
  429. }
  430. #pragma mark - fs.hash
  431. RCT_EXPORT_METHOD(hash:(NSString *)path
  432. algorithm:(NSString *)algorithm
  433. resolver:(RCTPromiseResolveBlock)resolve
  434. rejecter:(RCTPromiseRejectBlock)reject)
  435. {
  436. [RNFetchBlobFS hash:path algorithm:[NSString stringWithString:algorithm] resolver:resolve rejecter:reject];
  437. }
  438. #pragma mark - fs.readStream
  439. RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize tick:(int)tick streamId:(NSString *)streamId)
  440. {
  441. if(bufferSize == nil) {
  442. if([[encoding lowercaseString] isEqualToString:@"base64"])
  443. bufferSize = 4095;
  444. else
  445. bufferSize = 4096;
  446. }
  447. dispatch_async(fsQueue, ^{
  448. [RNFetchBlobFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId bridgeRef:_bridge];
  449. });
  450. }
  451. #pragma mark - fs.getEnvionmentDirs
  452. RCT_EXPORT_METHOD(getEnvironmentDirs:(RCTResponseSenderBlock) callback)
  453. {
  454. callback(@[
  455. [RNFetchBlobFS getDocumentDir],
  456. [RNFetchBlobFS getCacheDir],
  457. ]);
  458. }
  459. #pragma mark - net.cancelRequest
  460. RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBlock)callback) {
  461. [[RNFetchBlobNetwork sharedInstance] cancelRequest:taskId];
  462. callback(@[[NSNull null], taskId]);
  463. }
  464. #pragma mark - net.enableProgressReport
  465. RCT_EXPORT_METHOD(enableProgressReport:(NSString *)taskId interval:(nonnull NSNumber*)interval count:(nonnull NSNumber*)count)
  466. {
  467. RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Download interval:interval count:count];
  468. [[RNFetchBlobNetwork sharedInstance] enableProgressReport:taskId config:cfg];
  469. }
  470. #pragma mark - net.enableUploadProgressReport
  471. RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId interval:(nonnull NSNumber*)interval count:(nonnull NSNumber*)count)
  472. {
  473. RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Upload interval:interval count:count];
  474. [[RNFetchBlobNetwork sharedInstance] enableUploadProgress:taskId config:cfg];
  475. }
  476. #pragma mark - fs.slice
  477. RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(nonnull NSNumber *)start end:(nonnull NSNumber *)end resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  478. {
  479. [RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
  480. }
  481. RCT_EXPORT_METHOD(previewDocument:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  482. {
  483. NSString * utf8uri = [uri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  484. NSURL * url = [[NSURL alloc] initWithString:utf8uri];
  485. // NSURL * url = [[NSURL alloc] initWithString:uri];
  486. documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
  487. UIViewController *rootCtrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
  488. documentController.delegate = self;
  489. if(scheme == nil || [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scheme]]) {
  490. CGRect rect = CGRectMake(0.0, 0.0, 0.0, 0.0);
  491. dispatch_sync(dispatch_get_main_queue(), ^{
  492. [documentController presentOptionsMenuFromRect:rect inView:rootCtrl.view animated:YES];
  493. });
  494. resolve(@[[NSNull null]]);
  495. } else {
  496. reject(@"EINVAL", @"scheme is not supported", nil);
  497. }
  498. }
  499. # pragma mark - open file with UIDocumentInteractionController and delegate
  500. RCT_EXPORT_METHOD(openDocument:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  501. {
  502. NSString * utf8uri = [uri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  503. NSURL * url = [[NSURL alloc] initWithString:utf8uri];
  504. // NSURL * url = [[NSURL alloc] initWithString:uri];
  505. documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
  506. documentController.delegate = self;
  507. if(scheme == nil || [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scheme]]) {
  508. dispatch_sync(dispatch_get_main_queue(), ^{
  509. [documentController presentPreviewAnimated:YES];
  510. });
  511. resolve(@[[NSNull null]]);
  512. } else {
  513. reject(@"EINVAL", @"scheme is not supported", nil);
  514. }
  515. }
  516. # pragma mark - exclude from backup key
  517. RCT_EXPORT_METHOD(excludeFromBackupKey:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  518. {
  519. NSError *error = nil;
  520. [ [NSURL URLWithString:url] setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
  521. if(!error)
  522. {
  523. resolve(@[[NSNull null]]);
  524. } else {
  525. reject(@"EUNSPECIFIED", [error description], nil);
  526. }
  527. }
  528. RCT_EXPORT_METHOD(df:(RCTResponseSenderBlock)callback)
  529. {
  530. [RNFetchBlobFS df:callback];
  531. }
  532. - (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
  533. {
  534. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  535. return window.rootViewController;
  536. }
  537. # pragma mark - check expired network events
  538. RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
  539. {
  540. [RNFetchBlobNetwork emitExpiredTasks];
  541. }
  542. @end