StorageListResult.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2022 Google LLC
  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 Foundation
  15. /** Contains the prefixes and items returned by a `StorageReference.list()` call. */
  16. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  17. @objc(FIRStorageListResult) open class StorageListResult: NSObject {
  18. /**
  19. * The prefixes (folders) returned by a `list()` operation.
  20. */
  21. @objc public let prefixes: [StorageReference]
  22. /**
  23. * The objects (files) returned by a `list()` operation.
  24. */
  25. @objc public let items: [StorageReference]
  26. /**
  27. * A token that can be used to resume a previous `list()` operation. `nil`
  28. * indicates that there are no more results.
  29. */
  30. @objc public let pageToken: String?
  31. // MARK: - NSObject overrides
  32. @objc override open func copy() -> Any {
  33. return StorageListResult(withPrefixes: prefixes,
  34. items: items,
  35. pageToken: pageToken)
  36. }
  37. // MARK: - Internal APIs
  38. convenience init(with dictionary: [String: Any], reference: StorageReference) {
  39. var prefixes = [StorageReference]()
  40. var items = [StorageReference]()
  41. let rootReference = reference.root()
  42. if let prefixEntries = dictionary["prefixes"] as? [String] {
  43. for prefixEntry in prefixEntries {
  44. var pathWithoutTrailingSlash = prefixEntry
  45. if prefixEntry.hasSuffix("/") {
  46. pathWithoutTrailingSlash = String(prefixEntry.dropLast())
  47. }
  48. prefixes.append(rootReference.child(pathWithoutTrailingSlash))
  49. }
  50. }
  51. if let itemEntries = dictionary["items"] as? [[String: String]] {
  52. for itemEntry in itemEntries {
  53. if let item = itemEntry["name"] {
  54. items.append(rootReference.child(item))
  55. }
  56. }
  57. }
  58. let pageToken = dictionary["nextPageToken"] as? String
  59. self.init(withPrefixes: prefixes, items: items, pageToken: pageToken)
  60. }
  61. init(withPrefixes prefixes: [StorageReference],
  62. items: [StorageReference],
  63. pageToken: String?) {
  64. self.prefixes = prefixes
  65. self.items = items
  66. self.pageToken = pageToken
  67. }
  68. }