FIRStorageGetDownloadURLTask.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2018 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 "FIRStorageGetDownloadURLTask.h"
  15. #import "FIRStorageTask_Private.h"
  16. @implementation FIRStorageGetDownloadURLTask {
  17. @private
  18. FIRStorageVoidURLError _completion;
  19. }
  20. @synthesize fetcher = _fetcher;
  21. @synthesize fetcherCompletion = _fetcherCompletion;
  22. - (instancetype)initWithReference:(FIRStorageReference *)reference
  23. fetcherService:(GTMSessionFetcherService *)service
  24. dispatchQueue:(dispatch_queue_t)queue
  25. completion:(FIRStorageVoidURLError)completion {
  26. self = [super initWithReference:reference fetcherService:service dispatchQueue:queue];
  27. if (self) {
  28. _completion = [completion copy];
  29. }
  30. return self;
  31. }
  32. - (void)dealloc {
  33. [_fetcher stopFetching];
  34. }
  35. + (NSURL *)downloadURLFromMetadataDictionary:(NSDictionary *)dictionary {
  36. NSString *downloadTokens = dictionary[kFIRStorageMetadataDownloadTokens];
  37. if (downloadTokens && downloadTokens.length > 0) {
  38. NSArray<NSString *> *downloadTokenArray = [downloadTokens componentsSeparatedByString:@","];
  39. NSString *bucket = dictionary[kFIRStorageMetadataBucket];
  40. NSString *path = dictionary[kFIRStorageMetadataName];
  41. NSString *fullPath = [NSString stringWithFormat:kFIRStorageFullPathFormat, bucket,
  42. [FIRStorageUtils GCSEscapedString:path]];
  43. NSURLComponents *components = [[NSURLComponents alloc] init];
  44. components.scheme = kFIRStorageScheme;
  45. components.host = kFIRStorageHost;
  46. components.percentEncodedPath = fullPath;
  47. // The backend can return an arbitrary number of download tokens, but we only expose the first
  48. // token via the download URL.
  49. NSURLQueryItem *altItem = [[NSURLQueryItem alloc] initWithName:@"alt" value:@"media"];
  50. NSURLQueryItem *tokenItem = [[NSURLQueryItem alloc] initWithName:@"token"
  51. value:downloadTokenArray[0]];
  52. components.queryItems = @[ altItem, tokenItem ];
  53. return [components URL];
  54. }
  55. return nil;
  56. }
  57. - (void)enqueue {
  58. __weak FIRStorageGetDownloadURLTask *weakSelf = self;
  59. [self dispatchAsync:^() {
  60. FIRStorageGetDownloadURLTask *strongSelf = weakSelf;
  61. if (!strongSelf) {
  62. return;
  63. }
  64. NSMutableURLRequest *request = [strongSelf.baseRequest mutableCopy];
  65. request.HTTPMethod = @"GET";
  66. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime;
  67. FIRStorageVoidURLError callback = strongSelf->_completion;
  68. strongSelf->_completion = nil;
  69. GTMSessionFetcher *fetcher = [strongSelf.fetcherService fetcherWithRequest:request];
  70. strongSelf->_fetcher = fetcher;
  71. fetcher.comment = @"GetDownloadURLTask";
  72. #pragma clang diagnostic push
  73. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  74. strongSelf->_fetcherCompletion = ^(NSData *data, NSError *error) {
  75. NSURL *downloadURL;
  76. if (error) {
  77. if (!self.error) {
  78. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  79. }
  80. } else {
  81. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  82. if (responseDictionary != nil) {
  83. downloadURL =
  84. [FIRStorageGetDownloadURLTask downloadURLFromMetadataDictionary:responseDictionary];
  85. if (!downloadURL) {
  86. self.error =
  87. [FIRStorageErrors errorWithCustomMessage:@"Failed to retrieve a download URL."];
  88. }
  89. } else {
  90. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  91. }
  92. }
  93. if (callback) {
  94. callback(downloadURL, self.error);
  95. }
  96. self->_fetcherCompletion = nil;
  97. };
  98. #pragma clang diagnostic pop
  99. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  100. weakSelf.fetcherCompletion(data, error);
  101. }];
  102. }];
  103. };
  104. @end