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 "FirebaseStorage/Sources/FIRStorageGetDownloadURLTask.h"
  15. #import "FirebaseStorage/Sources/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. strongSelf->_fetcherCompletion = ^(NSData *data, NSError *error) {
  73. NSURL *downloadURL;
  74. if (error) {
  75. if (!self.error) {
  76. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  77. }
  78. } else {
  79. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  80. if (responseDictionary != nil) {
  81. downloadURL =
  82. [FIRStorageGetDownloadURLTask downloadURLFromMetadataDictionary:responseDictionary];
  83. if (!downloadURL) {
  84. self.error =
  85. [FIRStorageErrors errorWithCustomMessage:@"Failed to retrieve a download URL."];
  86. }
  87. } else {
  88. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  89. }
  90. }
  91. if (callback) {
  92. callback(downloadURL, self.error);
  93. }
  94. self->_fetcherCompletion = nil;
  95. };
  96. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  97. FIRStorageGetDownloadURLTask *strongSelf = weakSelf;
  98. if (strongSelf.fetcherCompletion) {
  99. strongSelf.fetcherCompletion(data, error);
  100. }
  101. }];
  102. }];
  103. };
  104. @end