FIRStorageListTask.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "FirebaseStorageInternal/Sources/FIRStorageListTask.h"
  15. #import "FirebaseStorageInternal/Sources/FIRStorageListResult_Private.h"
  16. #import "FirebaseStorageInternal/Sources/FIRStorageReference_Private.h"
  17. #import "FirebaseStorageInternal/Sources/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:(FIRIMPLStorageReference *)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. FIRIMPLStorageReference *root = self.reference.root;
  68. NSMutableURLRequest *request =
  69. [[FIRStorageUtils defaultRequestForReference:root queryParams:queryParams] mutableCopy];
  70. request.HTTPMethod = @"GET";
  71. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime;
  72. FIRStorageVoidListError callback = strongSelf->_completion;
  73. strongSelf->_completion = nil;
  74. GTMSessionFetcher *fetcher = [strongSelf.fetcherService fetcherWithRequest:request];
  75. strongSelf->_fetcher = fetcher;
  76. fetcher.comment = @"ListTask";
  77. strongSelf->_fetcherCompletion = ^(NSData *data, NSError *error) {
  78. FIRIMPLStorageListResult *listResult;
  79. if (error) {
  80. self.error = [FIRStorageErrors errorWithServerError:error reference:self.reference];
  81. } else {
  82. NSDictionary *responseDictionary = [NSDictionary frs_dictionaryFromJSONData:data];
  83. if (responseDictionary != nil) {
  84. listResult = [FIRIMPLStorageListResult fromDictionary:responseDictionary
  85. atReference:self.reference];
  86. } else {
  87. self.error = [FIRStorageErrors errorWithInvalidRequest:data];
  88. }
  89. }
  90. if (callback) {
  91. callback(listResult, self.error);
  92. }
  93. // Remove retain cycle set up by `strongSelf->_fetcherCompletion`
  94. self->_fetcherCompletion = nil;
  95. };
  96. [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  97. FIRStorageListTask *strongSelf = weakSelf;
  98. if (strongSelf.fetcherCompletion) {
  99. strongSelf.fetcherCompletion(data, error);
  100. }
  101. }];
  102. }];
  103. }
  104. @end