FIRStorageListResult.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <FirebaseStorage/FIRStorageListResult.h>
  15. #import <FirebaseStorage/FIRStorageReference.h>
  16. #import "FirebaseStorage/Sources/FIRStorageConstants_Private.h"
  17. @implementation FIRStorageListResult
  18. + (nullable FIRStorageListResult *)fromDictionary:(NSDictionary<NSString *, id> *)dictionary
  19. atReference:(FIRStorageReference *)reference {
  20. NSMutableArray<FIRStorageReference *> *prefixes = [NSMutableArray new];
  21. NSMutableArray<FIRStorageReference *> *items = [NSMutableArray new];
  22. FIRStorageReference *rootReference = reference.root;
  23. NSArray<NSString *> *prefixEntries = dictionary[kFIRStorageListPrefixes];
  24. for (NSString *prefixEntry in prefixEntries) {
  25. NSString *pathWithoutTrailingSlash = prefixEntry;
  26. if ([prefixEntry hasSuffix:@"/"]) {
  27. pathWithoutTrailingSlash = [pathWithoutTrailingSlash substringToIndex:prefixEntry.length - 1];
  28. }
  29. FIRStorageReference *prefixReference = [rootReference child:pathWithoutTrailingSlash];
  30. NSAssert([prefixReference.fullPath hasPrefix:reference.fullPath],
  31. @"Expected %@ to be a child element of %@", reference, prefixReference);
  32. [prefixes addObject:prefixReference];
  33. }
  34. NSArray<NSDictionary<NSString *, NSString *> *> *itemEntries = dictionary[kFIRStorageListItems];
  35. for (NSDictionary<NSString *, NSString *> *itemEntry in itemEntries) {
  36. FIRStorageReference *itemReference = [rootReference child:itemEntry[kFIRStorageListItemName]];
  37. NSAssert([itemReference.fullPath hasPrefix:reference.fullPath],
  38. @"Expected %@ to be a child element of %@", reference, itemReference);
  39. [items addObject:itemReference];
  40. }
  41. NSString *pageToken = dictionary[kFIRStorageListPageToken];
  42. return [[FIRStorageListResult alloc] initWithPrefixes:prefixes items:items pageToken:pageToken];
  43. }
  44. - (nullable instancetype)initWithPrefixes:(NSArray<FIRStorageReference *> *)prefixes
  45. items:(NSArray<FIRStorageReference *> *)items
  46. pageToken:(nullable NSString *)pageToken {
  47. self = [super init];
  48. if (self) {
  49. _prefixes = [prefixes copy];
  50. _items = [items copy];
  51. _pageToken = [pageToken copy];
  52. }
  53. return self;
  54. }
  55. - (instancetype)copyWithZone:(NSZone *)zone {
  56. FIRStorageListResult *clone = [[[self class] allocWithZone:zone] initWithPrefixes:_prefixes
  57. items:_items
  58. pageToken:_pageToken];
  59. return clone;
  60. }
  61. @end