FIRStorageUploadTask.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <FirebaseStorage/FIRStorageUploadTask.h>
  15. #import "FirebaseStorage/Sources/FIRStorageConstants_Private.h"
  16. #import "FirebaseStorage/Sources/FIRStorageMetadata_Private.h"
  17. #import "FirebaseStorage/Sources/FIRStorageObservableTask_Private.h"
  18. #import "FirebaseStorage/Sources/FIRStorageTask_Private.h"
  19. #import "FirebaseStorage/Sources/FIRStorageUploadTask_Private.h"
  20. #import <GTMSessionFetcher/GTMSessionUploadFetcher.h>
  21. @implementation FIRStorageUploadTask
  22. @synthesize progress = _progress;
  23. @synthesize fetcherCompletion = _fetcherCompletion;
  24. - (instancetype)initWithReference:(FIRStorageReference *)reference
  25. fetcherService:(GTMSessionFetcherService *)service
  26. dispatchQueue:(dispatch_queue_t)queue
  27. data:(NSData *)uploadData
  28. metadata:(FIRStorageMetadata *)metadata {
  29. self = [super initWithReference:reference fetcherService:service dispatchQueue:queue];
  30. if (self) {
  31. _uploadMetadata = [metadata copy];
  32. _uploadData = [uploadData copy];
  33. _progress = [NSProgress progressWithTotalUnitCount:[_uploadData length]];
  34. if (!_uploadMetadata.contentType) {
  35. _uploadMetadata.contentType = @"application/octet-stream";
  36. }
  37. }
  38. return self;
  39. }
  40. - (instancetype)initWithReference:(FIRStorageReference *)reference
  41. fetcherService:(GTMSessionFetcherService *)service
  42. dispatchQueue:(dispatch_queue_t)queue
  43. file:(NSURL *)fileURL
  44. metadata:(FIRStorageMetadata *)metadata {
  45. self = [super initWithReference:reference fetcherService:service dispatchQueue:queue];
  46. if (self) {
  47. _uploadMetadata = [metadata copy];
  48. _fileURL = [fileURL copy];
  49. _progress = [NSProgress progressWithTotalUnitCount:0];
  50. NSString *mimeType = [FIRStorageUtils MIMETypeForExtension:[_fileURL pathExtension]];
  51. if (!_uploadMetadata.contentType) {
  52. _uploadMetadata.contentType = mimeType ?: @"application/octet-stream";
  53. }
  54. }
  55. return self;
  56. }
  57. - (void)dealloc {
  58. [_uploadFetcher stopFetching];
  59. }
  60. - (void)enqueue {
  61. __weak FIRStorageUploadTask *weakSelf = self;
  62. [self dispatchAsync:^() {
  63. FIRStorageUploadTask *strongSelf = weakSelf;
  64. if (!strongSelf) {
  65. return;
  66. }
  67. NSError *contentValidationError;
  68. if (![strongSelf isContentToUploadValid:&contentValidationError]) {
  69. strongSelf.error = contentValidationError;
  70. [strongSelf finishTaskWithStatus:FIRStorageTaskStatusFailure snapshot:strongSelf.snapshot];
  71. return;
  72. }
  73. strongSelf.state = FIRStorageTaskStateQueueing;
  74. NSMutableURLRequest *request = [strongSelf.baseRequest mutableCopy];
  75. request.HTTPMethod = @"POST";
  76. request.timeoutInterval = strongSelf.reference.storage.maxUploadRetryTime;
  77. NSData *bodyData =
  78. [NSData frs_dataFromJSONDictionary:[strongSelf->_uploadMetadata dictionaryRepresentation]];
  79. request.HTTPBody = bodyData;
  80. [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
  81. NSString *contentLengthString =
  82. [NSString stringWithFormat:@"%zu", (unsigned long)[bodyData length]];
  83. [request setValue:contentLengthString forHTTPHeaderField:@"Content-Length"];
  84. NSURLComponents *components = [NSURLComponents componentsWithURL:request.URL
  85. resolvingAgainstBaseURL:NO];
  86. if ([components.host isEqual:kGCSHost]) {
  87. [components setPercentEncodedPath:[@"/upload" stringByAppendingString:components.path]];
  88. }
  89. NSDictionary *queryParams = @{@"uploadType" : @"resumable", @"name" : self.uploadMetadata.path};
  90. [components setPercentEncodedQuery:[FIRStorageUtils queryStringForDictionary:queryParams]];
  91. request.URL = components.URL;
  92. GTMSessionUploadFetcher *uploadFetcher =
  93. [GTMSessionUploadFetcher uploadFetcherWithRequest:request
  94. uploadMIMEType:strongSelf->_uploadMetadata.contentType
  95. chunkSize:kGTMSessionUploadFetcherStandardChunkSize
  96. fetcherService:self.fetcherService];
  97. if (strongSelf->_uploadData) {
  98. [uploadFetcher setUploadData:strongSelf->_uploadData];
  99. uploadFetcher.comment = @"Data UploadTask";
  100. } else if (strongSelf->_fileURL) {
  101. [uploadFetcher setUploadFileURL:strongSelf->_fileURL];
  102. uploadFetcher.comment = @"File UploadTask";
  103. }
  104. uploadFetcher.maxRetryInterval = self.reference.storage.maxUploadRetryTime;
  105. [uploadFetcher setSendProgressBlock:^(int64_t bytesSent, int64_t totalBytesSent,
  106. int64_t totalBytesExpectedToSend) {
  107. weakSelf.state = FIRStorageTaskStateProgress;
  108. weakSelf.progress.completedUnitCount = totalBytesSent;
  109. weakSelf.progress.totalUnitCount = totalBytesExpectedToSend;
  110. weakSelf.metadata = self->_uploadMetadata;
  111. [weakSelf fireHandlersForStatus:FIRStorageTaskStatusProgress snapshot:weakSelf.snapshot];
  112. weakSelf.state = FIRStorageTaskStateRunning;
  113. }];
  114. strongSelf->_uploadFetcher = uploadFetcher;
  115. // Process fetches
  116. strongSelf.state = FIRStorageTaskStateRunning;
  117. strongSelf->_fetcherCompletion = ^(NSData *_Nullable data, NSError *_Nullable error) {
  118. // Fire last progress updates
  119. [self fireHandlersForStatus:FIRStorageTaskStatusProgress snapshot:self.snapshot];
  120. // Handle potential issues with upload
  121. if (error) {
  122. self.state = FIRStorageTaskStateFailed;
  123. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  124. self.metadata = self->_uploadMetadata;
  125. [self finishTaskWithStatus:FIRStorageTaskStatusFailure snapshot:self.snapshot];
  126. return;
  127. }
  128. // Upload completed successfully, fire completion callbacks
  129. self.state = FIRStorageTaskStateSuccess;
  130. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  131. if (responseDictionary) {
  132. FIRStorageMetadata *metadata =
  133. [[FIRStorageMetadata alloc] initWithDictionary:responseDictionary];
  134. [metadata setType:FIRStorageMetadataTypeFile];
  135. self.metadata = metadata;
  136. } else {
  137. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  138. }
  139. [self finishTaskWithStatus:FIRStorageTaskStatusSuccess snapshot:self.snapshot];
  140. };
  141. [strongSelf->_uploadFetcher
  142. beginFetchWithCompletionHandler:^(NSData *_Nullable data, NSError *_Nullable error) {
  143. if (weakSelf.fetcherCompletion != nil) {
  144. weakSelf.fetcherCompletion(data, error);
  145. }
  146. }];
  147. }];
  148. }
  149. - (void)finishTaskWithStatus:(FIRStorageTaskStatus)status
  150. snapshot:(FIRStorageTaskSnapshot *)snapshot {
  151. [self fireHandlersForStatus:status snapshot:self.snapshot];
  152. [self removeAllObservers];
  153. self->_fetcherCompletion = nil;
  154. }
  155. - (BOOL)isContentToUploadValid:(NSError **)outError {
  156. if (_uploadData != nil) {
  157. return YES;
  158. }
  159. NSError *fileReachabilityError;
  160. if (![_fileURL checkResourceIsReachableAndReturnError:&fileReachabilityError] ||
  161. ![self fileURLisFile:_fileURL]) {
  162. if (outError != NULL) {
  163. NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:2];
  164. userInfo[NSLocalizedDescriptionKey] = [NSString
  165. stringWithFormat:@"File at URL: %@ is not reachable. "
  166. @"Ensure file URL is not a directory, symbolic link, or invalid url.",
  167. _fileURL.absoluteString];
  168. if (fileReachabilityError) {
  169. userInfo[NSUnderlyingErrorKey] = fileReachabilityError;
  170. }
  171. *outError = [NSError errorWithDomain:FIRStorageErrorDomain
  172. code:FIRStorageErrorCodeUnknown
  173. userInfo:userInfo];
  174. }
  175. return NO;
  176. }
  177. return YES;
  178. }
  179. #pragma mark - Upload Management
  180. - (void)cancel {
  181. __weak FIRStorageUploadTask *weakSelf = self;
  182. [self dispatchAsync:^() {
  183. weakSelf.state = FIRStorageTaskStateCancelled;
  184. [weakSelf.uploadFetcher stopFetching];
  185. if (weakSelf.state != FIRStorageTaskStateSuccess) {
  186. weakSelf.metadata = weakSelf.uploadMetadata;
  187. }
  188. weakSelf.error = [FIRStorageErrors errorWithCode:FIRStorageErrorCodeCancelled];
  189. [weakSelf fireHandlersForStatus:FIRStorageTaskStatusFailure snapshot:weakSelf.snapshot];
  190. }];
  191. }
  192. - (void)pause {
  193. __weak FIRStorageUploadTask *weakSelf = self;
  194. [self dispatchAsync:^() {
  195. weakSelf.state = FIRStorageTaskStatePaused;
  196. [weakSelf.uploadFetcher pauseFetching];
  197. if (weakSelf.state != FIRStorageTaskStateSuccess) {
  198. weakSelf.metadata = weakSelf.uploadMetadata;
  199. }
  200. [weakSelf fireHandlersForStatus:FIRStorageTaskStatusPause snapshot:weakSelf.snapshot];
  201. }];
  202. }
  203. - (void)resume {
  204. __weak FIRStorageUploadTask *weakSelf = self;
  205. [self dispatchAsync:^() {
  206. weakSelf.state = FIRStorageTaskStateResuming;
  207. [weakSelf.uploadFetcher resumeFetching];
  208. if (weakSelf.state != FIRStorageTaskStateSuccess) {
  209. weakSelf.metadata = weakSelf.uploadMetadata;
  210. }
  211. [weakSelf fireHandlersForStatus:FIRStorageTaskStatusResume snapshot:weakSelf.snapshot];
  212. weakSelf.state = FIRStorageTaskStateRunning;
  213. }];
  214. }
  215. #pragma mark - Private Helpers
  216. - (BOOL)fileURLisFile:(NSURL *)fileURL {
  217. NSNumber *isFile = [NSNumber numberWithBool:NO];
  218. [fileURL getResourceValue:&isFile forKey:NSURLIsRegularFileKey error:nil];
  219. return [isFile boolValue];
  220. }
  221. @end