설명 없음

RNFetchBlob.m 22KB

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