StorageListResult.swift 2.5 KB

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