FIRStorageUploadTask.m 10 KB

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