FIRStorageListTask.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2019 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 "FIRStorageListTask.h"
  15. #import "FIRStorageListResult_Private.h"
  16. #import "FIRStorageReference_Private.h"
  17. #import "FIRStorageTask_Private.h"
  18. @implementation FIRStorageListTask {
  19. @private
  20. FIRStorageVoidListError _completion;
  21. NSNumber *_pageSize;
  22. NSString *_previousPageToken;
  23. }
  24. @synthesize fetcher = _fetcher;
  25. @synthesize fetcherCompletion = _fetcherCompletion;
  26. - (instancetype)initWithReference:(FIRStorageReference *)reference
  27. fetcherService:(GTMSessionFetcherService *)service
  28. dispatchQueue:(dispatch_queue_t)queue
  29. pageSize:(nullable NSNumber *)pageSize
  30. previousPageToken:(nullable NSString *)previousPageToken
  31. completion:(FIRStorageVoidListError)completion {
  32. self = [super initWithReference:reference fetcherService:service dispatchQueue:queue];
  33. if (self) {
  34. _completion = [completion copy];
  35. _pageSize = pageSize;
  36. _previousPageToken = [previousPageToken copy];
  37. }
  38. return self;
  39. }
  40. - (void)dealloc {
  41. [_fetcher stopFetching];
  42. }
  43. - (void)enqueue {
  44. __weak FIRStorageListTask *weakSelf = self;
  45. [self dispatchAsync:^() {
  46. FIRStorageListTask *strongSelf = weakSelf;
  47. if (!strongSelf) {
  48. return;
  49. }
  50. NSMutableDictionary<NSString *, NSString *> *queryParams = [NSMutableDictionary new];
  51. NSString *prefix = [self reference].fullPath;
  52. if (prefix.length != 0) {
  53. queryParams[@"prefix"] = [prefix stringByAppendingString:@"/"];
  54. }
  55. // Firebase Storage uses file system semantics and treats slashes as separators. GCS's List API
  56. // does not prescribe a separator, and hence we need to provide a slash as the delimiter.
  57. queryParams[@"delimiter"] = @"/";
  58. // listAll() doesn't set a pageSize as this allows Firebase Storage to determine how many items
  59. // to return per page. This removes the need to backfill results if Firebase Storage filters
  60. // objects that are considered invalid (such as items with two consecutive slashes).
  61. if (strongSelf->_pageSize != nil) {
  62. queryParams[@"maxResults"] = [strongSelf->_pageSize stringValue];
  63. }
  64. if (strongSelf->_previousPageToken) {
  65. queryParams[@"pageToken"] = strongSelf->_previousPageToken;
  66. }
  67. FIRStoragePath *basePath = [[FIRStoragePath alloc] initWithBucket:self.reference.bucket
  68. object:nil];
  69. NSMutableURLRequest *request =
  70. [[FIRStorageUtils defaultRequestForPath:basePath queryParams:queryParams] mutableCopy];
  71. request.HTTPMethod = @"GET";
  72. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime;
  73. FIRStorageVoidListError callback = strongSelf->_completion;
  74. strongSelf->_completion = nil;
  75. GTMSessionFetcher *fetcher = [strongSelf.fetcherService fetcherWithRequest:request];
  76. strongSelf->_fetcher = fetcher;
  77. fetcher.comment = @"ListTask";
  78. #pragma clang diagnostic push
  79. #pragma clang diagnostic ignored "-Warc-retain-cycles"
  80. strongSelf->_fetcherCompletion = ^(NSData *data, NSError *error) {
  81. FIRStorageListResult *listResult;
  82. if (error) {
  83. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  84. } else {
  85. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  86. if (responseDictionary != nil) {
  87. listResult = [FIRStorageListResult fromDictionary:responseDictionary
  88. atReference:self.reference];
  89. } else {
  90. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  91. }
  92. }
  93. if (callback) {
  94. callback(listResult, self.error);
  95. }
  96. // Remove retain cycle set up by `strongSelf->_fetcherCompletion`
  97. self->_fetcherCompletion = nil;
  98. };
  99. #pragma clang diagnostic pop
  100. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  101. weakSelf.fetcherCompletion(data, error);
  102. }];
  103. }];
  104. }
  105. @end