Нет описания

RNFetchBlobNetwork.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // RNFetchBlobNetwork.m
  3. // RNFetchBlob
  4. //
  5. // Created by wkh237 on 2016/6/6.
  6. // Copyright © 2016 wkh237. All rights reserved.
  7. //
  8. #import "RCTConvert.h"
  9. #import "RCTLog.h"
  10. #import <Foundation/Foundation.h>
  11. #import "RCTBridge.h"
  12. #import "RCTEventDispatcher.h"
  13. #import "RNFetchBlobFS.h"
  14. #import "RNFetchBlobNetwork.h"
  15. #import "RNFetchBlobConst.h"
  16. ////////////////////////////////////////
  17. //
  18. // HTTP request handler
  19. //
  20. ////////////////////////////////////////
  21. @implementation RNFetchBlobNetwork
  22. @synthesize taskId;
  23. @synthesize expectedBytes;
  24. @synthesize receivedBytes;
  25. @synthesize respData;
  26. @synthesize callback;
  27. @synthesize bridge;
  28. @synthesize options;
  29. // constructor
  30. - (id)init {
  31. self = [super init];
  32. return self;
  33. }
  34. // removing case from headers
  35. + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
  36. NSMutableDictionary * mheaders = [[NSMutableDictionary alloc]init];
  37. for(NSString * key in headers) {
  38. [mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
  39. }
  40. return mheaders;
  41. }
  42. // send HTTP request
  43. - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req withData:( NSData * _Nullable )data callback:(RCTResponseSenderBlock) callback {
  44. self.taskId = taskId;
  45. self.respData = [[NSMutableData alloc] initWithLength:0];
  46. self.callback = callback;
  47. self.bridge = bridgeRef;
  48. self.expectedBytes = 0;
  49. self.receivedBytes = 0;
  50. self.options = options;
  51. NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
  52. NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
  53. NSURLSession * session = [NSURLSession sharedSession];
  54. // file will be stored at a specific path
  55. if( path != nil) {
  56. NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  57. if(error != nil) {
  58. callback(@[[error localizedDescription]]);
  59. return;
  60. }
  61. NSError * taskErr;
  62. NSFileManager * fm = [NSFileManager defaultManager];
  63. // move temp file to desination
  64. [fm moveItemAtURL:location toURL:[NSURL fileURLWithPath:path] error:&taskErr];
  65. if(taskErr != nil) {
  66. callback(@[[taskErr localizedDescription]]);
  67. return;
  68. }
  69. callback(@[[NSNull null], path]);
  70. }];
  71. [task resume];
  72. }
  73. // file will be stored at tmp path
  74. else if ( [self.options valueForKey:CONFIG_USE_TEMP]!= nil ) {
  75. NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  76. if(error != nil) {
  77. callback(@[[error localizedDescription]]);
  78. return;
  79. }
  80. NSError * taskErr;
  81. NSFileManager * fm = [NSFileManager defaultManager];
  82. NSString * tmpPath = [RNFetchBlobFS getTempPath:self.taskId withExtension:[self.options valueForKey:CONFIG_FILE_EXT]];
  83. // move temp file to desination
  84. [fm moveItemAtURL:location toURL:[NSURL fileURLWithPath:tmpPath] error:&taskErr];
  85. if(taskErr != nil) {
  86. callback(@[[taskErr localizedDescription]]);
  87. return;
  88. }
  89. callback(@[[NSNull null], tmpPath]);
  90. }];
  91. [task resume];
  92. }
  93. // base64 response
  94. else {
  95. NSURLSessionUploadTask * task =
  96. [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  97. if(error != nil) {
  98. callback(@[[error localizedDescription]]);
  99. return;
  100. }
  101. else {
  102. callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
  103. }
  104. }];
  105. [task resume];
  106. }
  107. }
  108. ////////////////////////////////////////
  109. //
  110. // NSURLSession delegates
  111. //
  112. ////////////////////////////////////////
  113. #pragma mark NSURLSession delegate methods
  114. // set expected content length on response received
  115. - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
  116. {
  117. expectedBytes = [response expectedContentLength];
  118. }
  119. // download progress handler
  120. - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
  121. {
  122. receivedBytes += [data length];
  123. Boolean fileCache = [self.options valueForKey:CONFIG_USE_TEMP];
  124. NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
  125. // cache data in memory
  126. if(path == nil && fileCache == nil) {
  127. [respData appendData:data];
  128. }
  129. [self.bridge.eventDispatcher
  130. sendDeviceEventWithName:@"RNFetchBlobProgress"
  131. body:@{
  132. @"taskId": taskId,
  133. @"written": [NSString stringWithFormat:@"%d", receivedBytes],
  134. @"total": [NSString stringWithFormat:@"%d", expectedBytes]
  135. }
  136. ];
  137. }
  138. - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
  139. NSLog([error localizedDescription]);
  140. }
  141. // upload progress handler
  142. - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
  143. {
  144. expectedBytes = totalBytesExpectedToWrite;
  145. receivedBytes += totalBytesWritten;
  146. [self.bridge.eventDispatcher
  147. sendDeviceEventWithName:@"RNFetchBlobProgress"
  148. body:@{
  149. @"taskId": taskId,
  150. @"written": [NSString stringWithFormat:@"%d", receivedBytes],
  151. @"total": [NSString stringWithFormat:@"%d", expectedBytes]
  152. }
  153. ];
  154. }
  155. @end