iceriver102

RNBLOB

Dec 29th, 2019
449
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Copyright (c) Facebook, Inc. and its affiliates.
  3.  *
  4.  * This source code is licensed under the MIT license found in the
  5.  * LICENSE file in the root directory of this source tree.
  6.  */
  7.  
  8.  
  9. #import "RCTFileReaderModule.h"
  10.  
  11. #import <React/RCTBridge.h>
  12. #import <React/RCTConvert.h>
  13.  
  14. #import "RCTBlobManager.h"
  15.  
  16.  
  17. @implementation RCTFileReaderModule
  18.  
  19. RCT_EXPORT_MODULE(FileReaderModule)
  20.  
  21. @synthesize bridge = _bridge;
  22.  
  23. RCT_EXPORT_METHOD(readAsText:(NSDictionary<NSString *, id> *)blob
  24.                   encoding:(NSString *)encoding
  25.                   resolve:(RCTPromiseResolveBlock)resolve
  26.                   reject:(RCTPromiseRejectBlock)reject)
  27. {
  28.   RCTBlobManager *blobManager = [[self bridge] moduleForClass:[RCTBlobManager class]];
  29.   NSData *data = [blobManager resolve:blob];
  30.  
  31.   if (data == nil) {
  32.     reject(RCTErrorUnspecified,
  33.            [NSString stringWithFormat:@"Unable to resolve data for blob: %@", [RCTConvert NSString:blob[@"blobId"]]], nil);
  34.   } else {
  35.     NSStringEncoding stringEncoding;
  36.  
  37.     if (encoding == nil) {
  38.       stringEncoding = NSUTF8StringEncoding;
  39.     } else {
  40.       stringEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef) encoding));
  41.     }
  42.  
  43.     NSString *text = [[NSString alloc] initWithData:data encoding:stringEncoding];
  44.  
  45.     resolve(text);
  46.   }
  47. }
  48.  
  49. RCT_EXPORT_METHOD(readAsDataURL:(NSDictionary<NSString *, id> *)blob
  50.                   resolve:(RCTPromiseResolveBlock)resolve
  51.                   reject:(RCTPromiseRejectBlock)reject)
  52. {
  53.   RCTBlobManager *blobManager = [[self bridge] moduleForClass:[RCTBlobManager class]];
  54.   NSData *data = [blobManager resolve:blob];
  55.  
  56.   if (data == nil) {
  57.     reject(RCTErrorUnspecified,
  58.            [NSString stringWithFormat:@"Unable to resolve data for blob: %@", [RCTConvert NSString:blob[@"blobId"]]], nil);
  59.   } else {
  60.     NSString *type = [RCTConvert NSString:blob[@"type"]];
  61.     NSString *text = [NSString stringWithFormat:@"data:%@;base64,%@",
  62.                       type != nil && [type length] > 0 ? type : @"application/octet-stream",
  63.                       [data base64EncodedStringWithOptions:0]];
  64.  
  65.     resolve(text);
  66.   }
  67. }
  68.  
  69. @end
RAW Paste Data