StorageListTask.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #if COCOAPODS
  16. import GTMSessionFetcher
  17. #else
  18. import GTMSessionFetcherCore
  19. #endif
  20. /// A Task that lists the entries under a StorageReference
  21. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  22. enum StorageListTask {
  23. static func listTask(reference: StorageReference,
  24. fetcherService: GTMSessionFetcherService,
  25. queue: DispatchQueue,
  26. pageSize: Int64?,
  27. previousPageToken: String?,
  28. completion: ((_: StorageListResult?, _: Error?) -> Void)?) {
  29. var queryParams = [String: String]()
  30. let prefix = reference.fullPath
  31. if prefix.count > 0 {
  32. queryParams["prefix"] = "\(prefix)/"
  33. }
  34. // Firebase Storage uses file system semantics and treats slashes as separators. GCS's List
  35. // API
  36. // does not prescribe a separator, and hence we need to provide a slash as the delimiter.
  37. queryParams["delimiter"] = "/"
  38. // listAll() doesn't set a pageSize as this allows Firebase Storage to determine how many
  39. // items
  40. // to return per page. This removes the need to backfill results if Firebase Storage filters
  41. // objects that are considered invalid (such as items with two consecutive slashes).
  42. if let pageSize {
  43. queryParams["maxResults"] = "\(pageSize)"
  44. }
  45. if let previousPageToken {
  46. queryParams["pageToken"] = previousPageToken
  47. }
  48. let root = reference.root()
  49. let request = StorageUtils.defaultRequestForReference(
  50. reference: root,
  51. queryParams: queryParams
  52. )
  53. StorageInternalTask(reference: reference,
  54. fetcherService: fetcherService,
  55. queue: queue,
  56. request: request,
  57. httpMethod: "GET",
  58. fetcherComment: "ListTask") { (data: Data?, error: Error?) in
  59. if let error {
  60. completion?(nil, error)
  61. } else {
  62. if let data,
  63. let responseDictionary = try? JSONSerialization
  64. .jsonObject(with: data) as? [String: AnyHashable] {
  65. let listResult = StorageListResult(with: responseDictionary, reference: reference)
  66. completion?(listResult, nil)
  67. } else {
  68. completion?(nil, StorageErrorCode.error(withInvalidRequest: data))
  69. }
  70. }
  71. }
  72. }
  73. }