StorageListResult.swift 2.7 KB

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