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