FIRStorageListResult.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/Sources/Public/FirebaseStorage/FIRStorageListResult.h"
  15. #import "FirebaseStorage/Sources/Public/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. [prefixes addObject:prefixReference];
  31. }
  32. NSArray<NSDictionary<NSString *, NSString *> *> *itemEntries = dictionary[kFIRStorageListItems];
  33. for (NSDictionary<NSString *, NSString *> *itemEntry in itemEntries) {
  34. FIRStorageReference *itemReference = [rootReference child:itemEntry[kFIRStorageListItemName]];
  35. [items addObject:itemReference];
  36. }
  37. NSString *pageToken = dictionary[kFIRStorageListPageToken];
  38. return [[FIRStorageListResult alloc] initWithPrefixes:prefixes items:items pageToken:pageToken];
  39. }
  40. - (nullable instancetype)initWithPrefixes:(NSArray<FIRStorageReference *> *)prefixes
  41. items:(NSArray<FIRStorageReference *> *)items
  42. pageToken:(nullable NSString *)pageToken {
  43. self = [super init];
  44. if (self) {
  45. _prefixes = [prefixes copy];
  46. _items = [items copy];
  47. _pageToken = [pageToken copy];
  48. }
  49. return self;
  50. }
  51. - (instancetype)copyWithZone:(NSZone *)zone {
  52. FIRStorageListResult *clone = [[[self class] allocWithZone:zone] initWithPrefixes:_prefixes
  53. items:_items
  54. pageToken:_pageToken];
  55. return clone;
  56. }
  57. @end