AWSS3TransferUtilityTasks.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //
  2. // Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License").
  5. // You may not use this file except in compliance with the License.
  6. // A copy of the License is located at
  7. //
  8. // http://aws.amazon.com/apache2.0
  9. //
  10. // or in the "license" file accompanying this file. This file is distributed
  11. // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. // express or implied. See the License for the specific language governing
  13. // permissions and limitations under the License.
  14. //
  15. #import <Foundation/Foundation.h>
  16. #import "AWSS3TransferUtilityTasks.h"
  17. #import "AWSS3TransferUtilityDatabaseHelper.h"
  18. #import "AWSS3PreSignedURL.h"
  19. #import <AWSCore/AWSFMDB.h>
  20. @interface AWSS3TransferUtilityExpression()
  21. @property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *internalRequestHeaders;
  22. @property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *internalRequestParameters;
  23. - (void)assignRequestParameters:(AWSS3GetPreSignedURLRequest *)getPreSignedURLRequest;
  24. - (void)assignRequestHeaders:(AWSS3GetPreSignedURLRequest *)getPreSignedURLRequest;
  25. @end
  26. @interface AWSS3TransferUtilityUploadExpression()
  27. @property (copy, atomic) AWSS3TransferUtilityUploadCompletionHandlerBlock completionHandler;
  28. @end
  29. @interface AWSS3TransferUtilityMultiPartUploadExpression()
  30. @property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *internalRequestHeaders;
  31. @property (strong, nonatomic) NSMutableDictionary<NSString *, NSString *> *internalRequestParameters;
  32. - (void)assignRequestParameters:(AWSS3GetPreSignedURLRequest *)getPreSignedURLRequest;
  33. @property (copy, atomic) AWSS3TransferUtilityMultiPartUploadCompletionHandlerBlock completionHandler;
  34. @end
  35. @interface AWSS3TransferUtilityDownloadExpression()
  36. @property (copy, atomic) AWSS3TransferUtilityDownloadCompletionHandlerBlock completionHandler;
  37. @end
  38. @interface AWSS3TransferUtilityTask()
  39. @property (strong, nonatomic) NSURLSessionTask *sessionTask;
  40. @property (strong, nonatomic) NSString *transferID;
  41. @property (strong, nonatomic) NSString *bucket;
  42. @property (strong, nonatomic) NSString *key;
  43. @property (strong, nonatomic) NSData *data;
  44. @property (strong, nonatomic) NSURL *location;
  45. @property (strong, nonatomic) NSError *error;
  46. @property int retryCount;
  47. @property NSString *nsURLSessionID;
  48. @property NSString *file;
  49. @property NSString *transferType;
  50. @property AWSS3TransferUtilityTransferStatusType status;
  51. @property (strong) AWSFMDatabaseQueue *databaseQueue;
  52. @end
  53. @interface AWSS3TransferUtilityUploadTask()
  54. @property (strong, nonatomic) AWSS3TransferUtilityUploadExpression *expression;
  55. @property NSString *responseData;
  56. @property (atomic) BOOL cancelled;
  57. @property BOOL temporaryFileCreated;
  58. @end
  59. @interface AWSS3TransferUtilityMultiPartUploadTask()
  60. @property (strong, nonatomic) AWSS3TransferUtilityMultiPartUploadExpression *expression;
  61. @property NSString * uploadID;
  62. @property BOOL cancelled;
  63. @property BOOL temporaryFileCreated;
  64. @property (strong, atomic) NSMutableDictionary <NSNumber *, AWSS3TransferUtilityUploadSubTask *> *waitingPartsDictionary;
  65. @property (strong, atomic) NSMutableDictionary <NSNumber *, AWSS3TransferUtilityUploadSubTask *> *completedPartsDictionary;
  66. @property (strong, atomic) NSMutableDictionary <NSNumber *, AWSS3TransferUtilityUploadSubTask *> *inProgressPartsDictionary;
  67. @property int retryCount;
  68. @property int partNumber;
  69. @property NSString *file;
  70. @property NSString *transferType;
  71. @property NSString *nsURLSessionID;
  72. @property (strong) AWSFMDatabaseQueue *databaseQueue;
  73. @property (strong, nonatomic) NSError *error;
  74. @property (strong, nonatomic) NSString *bucket;
  75. @property (strong, nonatomic) NSString *key;
  76. @property (strong, nonatomic) NSString *transferID;
  77. @property AWSS3TransferUtilityTransferStatusType status;
  78. @property NSNumber *contentLength;
  79. @end
  80. @interface AWSS3TransferUtilityUploadSubTask()
  81. @property (strong, nonatomic) NSURLSessionTask *sessionTask;
  82. @property (strong, nonatomic) NSNumber *partNumber;
  83. @property (readwrite) NSUInteger taskIdentifier;
  84. @property (strong, nonatomic) NSString *eTag;
  85. @property int64_t totalBytesExpectedToSend;
  86. @property int64_t totalBytesSent;
  87. @property NSString *responseData;
  88. @property NSString *file;
  89. @property NSString *transferType;
  90. @property NSString *transferID;
  91. @property AWSS3TransferUtilityTransferStatusType status;
  92. @property NSString *uploadID;
  93. @end
  94. @interface AWSS3TransferUtilityDownloadTask()
  95. @property (strong, nonatomic) AWSS3TransferUtilityDownloadExpression *expression;
  96. @property BOOL cancelled;
  97. @property NSString *responseData;
  98. @end
  99. @interface AWSS3TransferUtilityDatabaseHelper()
  100. + (void) deleteTransferRequestFromDB:(NSString *) transferID
  101. databaseQueue: (AWSFMDatabaseQueue *) databaseQueue;
  102. + (void) updateTransferRequestInDB: (NSString *) transferID
  103. partNumber: (NSNumber *) partNumber
  104. taskIdentifier: (NSUInteger) taskIdentifier
  105. eTag: (NSString *) eTag
  106. status: (AWSS3TransferUtilityTransferStatusType) status
  107. retry_count: (NSUInteger) retryCount
  108. databaseQueue: (AWSFMDatabaseQueue *) databaseQueue;
  109. @end
  110. #pragma mark - AWSS3TransferUtilityTasks
  111. @implementation AWSS3TransferUtilityTask
  112. - (instancetype)init {
  113. if (self = [super init]) {
  114. _progress = [NSProgress new];
  115. _progress.completedUnitCount = 0;
  116. }
  117. return self;
  118. }
  119. - (NSUInteger)taskIdentifier {
  120. return self.sessionTask.taskIdentifier;
  121. }
  122. - (void)cancel {
  123. }
  124. - (void)resume {
  125. if (self.status != AWSS3TransferUtilityTransferStatusPaused) {
  126. //Resume called on a transfer that is not paused. No op
  127. return;
  128. }
  129. [self.sessionTask resume];
  130. self.status = AWSS3TransferUtilityTransferStatusInProgress;
  131. [AWSS3TransferUtilityDatabaseHelper updateTransferRequestInDB:self.transferID
  132. partNumber:@0
  133. taskIdentifier:self.taskIdentifier
  134. eTag:@""
  135. status:self.status
  136. retry_count:self.retryCount
  137. databaseQueue:self.databaseQueue];
  138. }
  139. - (void)suspend {
  140. if (self.status != AWSS3TransferUtilityTransferStatusInProgress ) {
  141. //Pause called on a transfer that is not in progress. No op.
  142. return;
  143. }
  144. [self.sessionTask suspend];
  145. self.status = AWSS3TransferUtilityTransferStatusPaused;
  146. [AWSS3TransferUtilityDatabaseHelper updateTransferRequestInDB:self.transferID
  147. partNumber:@0
  148. taskIdentifier:self.taskIdentifier
  149. eTag:@""
  150. status:self.status
  151. retry_count:self.retryCount
  152. databaseQueue:self.databaseQueue];
  153. }
  154. - (NSURLRequest *)request {
  155. return self.sessionTask.originalRequest;
  156. }
  157. - (NSHTTPURLResponse *)response {
  158. if ([self.sessionTask.response isKindOfClass:[NSHTTPURLResponse class]]) {
  159. return (NSHTTPURLResponse *)self.sessionTask.response;
  160. }
  161. return nil;
  162. }
  163. @end
  164. @implementation AWSS3TransferUtilityUploadTask
  165. - (AWSS3TransferUtilityUploadExpression *)expression {
  166. if (!_expression) {
  167. _expression = [AWSS3TransferUtilityUploadExpression new];
  168. }
  169. return _expression;
  170. }
  171. -(void) cancel {
  172. self.status = AWSS3TransferUtilityTransferStatusCancelled;
  173. self.cancelled = YES;
  174. [self.sessionTask cancel];
  175. [AWSS3TransferUtilityDatabaseHelper deleteTransferRequestFromDB:self.transferID databaseQueue:self.databaseQueue];
  176. }
  177. -(void) setCompletionHandler:(AWSS3TransferUtilityUploadCompletionHandlerBlock)completionHandler {
  178. self.expression.completionHandler = completionHandler;
  179. //If the task has already completed successfully, call the completion handler
  180. if (self.status == AWSS3TransferUtilityTransferStatusCompleted ) {
  181. _expression.completionHandler(self, nil);
  182. }
  183. //If the task has completed with error, call the completion handler
  184. else if (self.error) {
  185. _expression.completionHandler(self, self.error);
  186. }
  187. }
  188. -(void) setProgressBlock:(AWSS3TransferUtilityProgressBlock)progressBlock {
  189. self.expression.progressBlock = progressBlock;
  190. }
  191. @end
  192. @implementation AWSS3TransferUtilityMultiPartUploadTask
  193. - (instancetype)init {
  194. if (self = [super init]) {
  195. _progress = [NSProgress new];
  196. _waitingPartsDictionary = [NSMutableDictionary new];
  197. _inProgressPartsDictionary = [NSMutableDictionary new];
  198. _completedPartsDictionary = [NSMutableDictionary new];
  199. }
  200. return self;
  201. }
  202. - (AWSS3TransferUtilityMultiPartUploadExpression *)expression {
  203. if (!_expression) {
  204. _expression = [AWSS3TransferUtilityMultiPartUploadExpression new];
  205. }
  206. return _expression;
  207. }
  208. - (void)cancel {
  209. self.cancelled = YES;
  210. self.status = AWSS3TransferUtilityTransferStatusCancelled;
  211. for (NSNumber *key in [self.inProgressPartsDictionary allKeys]) {
  212. AWSS3TransferUtilityUploadSubTask *subTask = [self.inProgressPartsDictionary objectForKey:key];
  213. [subTask.sessionTask cancel];
  214. }
  215. for (NSNumber *key in [self.waitingPartsDictionary allKeys]) {
  216. AWSS3TransferUtilityUploadSubTask *subTask = [self.waitingPartsDictionary objectForKey:key];
  217. [subTask.sessionTask cancel];
  218. }
  219. [AWSS3TransferUtilityDatabaseHelper deleteTransferRequestFromDB:_transferID databaseQueue:self.databaseQueue];
  220. }
  221. - (void)resume {
  222. if (self.status != AWSS3TransferUtilityTransferStatusPaused ) {
  223. //Resume called on a transfer that hasn't been paused. No op.
  224. return;
  225. }
  226. for (NSNumber *key in [self.inProgressPartsDictionary allKeys]) {
  227. AWSS3TransferUtilityUploadSubTask *subTask = [self.inProgressPartsDictionary objectForKey:key];
  228. subTask.status = AWSS3TransferUtilityTransferStatusInProgress;
  229. [AWSS3TransferUtilityDatabaseHelper updateTransferRequestInDB:subTask.transferID
  230. partNumber:subTask.partNumber
  231. taskIdentifier:subTask.taskIdentifier
  232. eTag:subTask.eTag
  233. status:subTask.status
  234. retry_count:self.retryCount
  235. databaseQueue:self.databaseQueue];
  236. [subTask.sessionTask resume];
  237. }
  238. self.status = AWSS3TransferUtilityTransferStatusInProgress;
  239. //Update the Master Record
  240. [AWSS3TransferUtilityDatabaseHelper updateTransferRequestInDB:self.transferID
  241. partNumber:@0
  242. taskIdentifier:0
  243. eTag:@""
  244. status:self.status
  245. retry_count:self.retryCount
  246. databaseQueue:self.databaseQueue];
  247. }
  248. - (void)suspend {
  249. if (self.status != AWSS3TransferUtilityTransferStatusInProgress) {
  250. //Pause called on a transfer that is not in progresss. No op.
  251. return;
  252. }
  253. for (NSNumber *key in [self.inProgressPartsDictionary allKeys]) {
  254. AWSS3TransferUtilityUploadSubTask *subTask = [self.inProgressPartsDictionary objectForKey:key];
  255. [subTask.sessionTask suspend];
  256. subTask.status = AWSS3TransferUtilityTransferStatusPaused;
  257. [AWSS3TransferUtilityDatabaseHelper updateTransferRequestInDB:subTask.transferID
  258. partNumber:subTask.partNumber
  259. taskIdentifier:subTask.taskIdentifier
  260. eTag:subTask.eTag
  261. status:subTask.status
  262. retry_count:self.retryCount
  263. databaseQueue:self.databaseQueue];
  264. }
  265. self.status = AWSS3TransferUtilityTransferStatusPaused;
  266. //Update the Master Record
  267. [AWSS3TransferUtilityDatabaseHelper updateTransferRequestInDB:self.transferID
  268. partNumber:@0
  269. taskIdentifier:0
  270. eTag:@""
  271. status:self.status
  272. retry_count:self.retryCount
  273. databaseQueue:self.databaseQueue];
  274. }
  275. -(void) setCompletionHandler:(AWSS3TransferUtilityMultiPartUploadCompletionHandlerBlock)completionHandler {
  276. self.expression.completionHandler = completionHandler;
  277. //If the task has already completed successfully, call the completion handler
  278. if (self.status == AWSS3TransferUtilityTransferStatusCompleted) {
  279. _expression.completionHandler(self, nil);
  280. }
  281. //If the task has completed with error, call the completion handler
  282. else if (self.error ) {
  283. _expression.completionHandler(self, self.error);
  284. }
  285. }
  286. -(void) setProgressBlock:(AWSS3TransferUtilityMultiPartProgressBlock)progressBlock {
  287. self.expression.progressBlock = progressBlock;
  288. }
  289. @end
  290. @implementation AWSS3TransferUtilityDownloadTask
  291. - (AWSS3TransferUtilityDownloadExpression *)expression {
  292. if (!_expression) {
  293. _expression = [AWSS3TransferUtilityDownloadExpression new];
  294. }
  295. return _expression;
  296. }
  297. -(void) cancel {
  298. self.cancelled = YES;
  299. self.status = AWSS3TransferUtilityTransferStatusCancelled;
  300. [self.sessionTask cancel];
  301. [AWSS3TransferUtilityDatabaseHelper deleteTransferRequestFromDB:self.transferID databaseQueue:self.databaseQueue];
  302. }
  303. -(void) setCompletionHandler:(AWSS3TransferUtilityDownloadCompletionHandlerBlock)completionHandler {
  304. self.expression.completionHandler = completionHandler;
  305. //If the task has already completed successfully, call the completion handler
  306. if (self.status == AWSS3TransferUtilityTransferStatusCompleted) {
  307. _expression.completionHandler(self, self.location, self.data, nil);
  308. }
  309. //If the task has completed with error, call the completion handler
  310. else if (self.error ) {
  311. _expression.completionHandler(self, self.location, self.data, self.error);
  312. }
  313. }
  314. -(void) setProgressBlock:(AWSS3TransferUtilityProgressBlock)progressBlock {
  315. self.expression.progressBlock = progressBlock;
  316. }
  317. @end
  318. @implementation AWSS3TransferUtilityUploadSubTask
  319. @end
  320. #pragma mark - AWSS3TransferUtilityExpressions
  321. @implementation AWSS3TransferUtilityExpression
  322. - (instancetype)init {
  323. if (self = [super init]) {
  324. _internalRequestHeaders = [NSMutableDictionary new];
  325. _internalRequestParameters = [NSMutableDictionary new];
  326. }
  327. return self;
  328. }
  329. - (NSDictionary<NSString *, NSString *> *)requestHeaders {
  330. return [NSDictionary dictionaryWithDictionary:self.internalRequestHeaders];
  331. }
  332. - (NSDictionary<NSString *, NSString *> *)requestParameters {
  333. return [NSDictionary dictionaryWithDictionary:self.internalRequestParameters];
  334. }
  335. - (void)setValue:(NSString *)value forRequestHeader:(NSString *)requestHeader {
  336. [self.internalRequestHeaders setValue:value forKey:requestHeader];
  337. }
  338. - (void)setValue:(NSString *)value forRequestParameter:(NSString *)requestParameter {
  339. [self.internalRequestParameters setValue:value forKey:requestParameter];
  340. }
  341. - (void)assignRequestHeaders:(AWSS3GetPreSignedURLRequest *)getPreSignedURLRequest {
  342. for (NSString *key in self.internalRequestHeaders) {
  343. [getPreSignedURLRequest setValue:self.internalRequestHeaders[key]
  344. forRequestHeader:key];
  345. }
  346. }
  347. - (void)assignRequestParameters:(AWSS3GetPreSignedURLRequest *)getPreSignedURLRequest {
  348. for (NSString *key in self.internalRequestParameters) {
  349. [getPreSignedURLRequest setValue:self.internalRequestParameters[key]
  350. forRequestParameter:key];
  351. }
  352. }
  353. @end
  354. @implementation AWSS3TransferUtilityUploadExpression
  355. - (NSString *)contentMD5 {
  356. return [self.internalRequestHeaders valueForKey:@"Content-MD5"];
  357. }
  358. - (void)setContentMD5:(NSString *)contentMD5 {
  359. [self setValue:contentMD5 forRequestHeader:@"Content-MD5"];
  360. }
  361. @end
  362. @implementation AWSS3TransferUtilityMultiPartUploadExpression
  363. - (instancetype)init {
  364. if (self = [super init]) {
  365. _internalRequestHeaders = [NSMutableDictionary new];
  366. _internalRequestParameters = [NSMutableDictionary new];
  367. }
  368. return self;
  369. }
  370. - (NSDictionary<NSString *, NSString *> *)requestHeaders {
  371. return [NSDictionary dictionaryWithDictionary:self.internalRequestHeaders];
  372. }
  373. - (NSDictionary<NSString *, NSString *> *)requestParameters {
  374. return [NSDictionary dictionaryWithDictionary:self.internalRequestParameters];
  375. }
  376. - (void)setValue:(NSString *)value forRequestHeader:(NSString *)requestHeader {
  377. [self.internalRequestHeaders setValue:value forKey:requestHeader];
  378. }
  379. - (void)setValue:(NSString *)value forRequestParameter:(NSString *)requestParameter {
  380. [self.internalRequestParameters setValue:value forKey:requestParameter];
  381. }
  382. - (void)assignRequestParameters:(AWSS3GetPreSignedURLRequest *)getPreSignedURLRequest {
  383. for (NSString *key in self.internalRequestParameters) {
  384. [getPreSignedURLRequest setValue:self.internalRequestParameters[key]
  385. forRequestParameter:key];
  386. }
  387. }
  388. @end
  389. @implementation AWSS3TransferUtilityDownloadExpression
  390. @end