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 "FirebaseStorageInternal/Sources/Public/FirebaseStorageInternal/FIRStorageListResult.h"
  15. #import "FirebaseStorageInternal/Sources/Public/FirebaseStorageInternal/FIRStorageReference.h"
  16. #import "FirebaseStorageInternal/Sources/FIRStorageConstants_Private.h"
  17. @implementation FIRIMPLStorageListResult
  18. + (nullable FIRIMPLStorageListResult *)fromDictionary:(NSDictionary<NSString *, id> *)dictionary
  19. atReference:(FIRIMPLStorageReference *)reference {
  20. NSMutableArray<FIRIMPLStorageReference *> *prefixes = [NSMutableArray new];
  21. NSMutableArray<FIRIMPLStorageReference *> *items = [NSMutableArray new];
  22. FIRIMPLStorageReference *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. FIRIMPLStorageReference *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. FIRIMPLStorageReference *itemReference =
  35. [rootReference child:itemEntry[kFIRStorageListItemName]];
  36. [items addObject:itemReference];
  37. }
  38. NSString *pageToken = dictionary[kFIRStorageListPageToken];
  39. return [[FIRIMPLStorageListResult alloc] initWithPrefixes:prefixes
  40. items:items
  41. pageToken:pageToken];
  42. }
  43. - (nullable instancetype)initWithPrefixes:(NSArray<FIRIMPLStorageReference *> *)prefixes
  44. items:(NSArray<FIRIMPLStorageReference *> *)items
  45. pageToken:(nullable NSString *)pageToken {
  46. self = [super init];
  47. if (self) {
  48. _prefixes = [prefixes copy];
  49. _items = [items copy];
  50. _pageToken = [pageToken copy];
  51. }
  52. return self;
  53. }
  54. - (instancetype)copyWithZone:(NSZone *)zone {
  55. FIRIMPLStorageListResult *clone = [[[self class] allocWithZone:zone] initWithPrefixes:_prefixes
  56. items:_items
  57. pageToken:_pageToken];
  58. return clone;
  59. }
  60. @end