StorageListTask.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  21. * A Task that lists the entries under a {@link StorageReference}
  22. */
  23. internal class StorageListTask: StorageTask, StorageTaskManagement {
  24. private var fetcher: GTMSessionFetcher?
  25. private var fetcherCompletion: ((Data?, NSError?) -> Void)?
  26. private var taskCompletion: ((_: StorageListResult?, _: NSError?) -> Void)?
  27. private let pageSize: Int64?
  28. private let previousPageToken: String?
  29. /**
  30. * Initializes a new List Task.
  31. *
  32. * To schedule the task, invoke `[FIRStorageListTask enqueue]`.
  33. *
  34. * @param reference The location to invoke List on.
  35. * @param service GTMSessionFetcherService to use for the RPC.
  36. * @param queue The queue to schedule the List operation on.
  37. * @param pageSize An optional pageSize, denoting the maximum size of the result set. If
  38. * set to `nil`, the backend will use the default page size.
  39. * @param previousPageToken An optional pageToken, used to resume a previous invocation.
  40. * @param completion The completion handler to be called with the FIRIMPLStorageListResult.
  41. */
  42. internal init(reference: StorageReference,
  43. fetcherService: GTMSessionFetcherService,
  44. queue: DispatchQueue,
  45. pageSize: Int64?,
  46. previousPageToken: String?,
  47. completion: ((_ listResult: StorageListResult?, _ error: NSError?) -> Void)?) {
  48. self.pageSize = pageSize
  49. self.previousPageToken = previousPageToken
  50. super.init(reference: reference, service: fetcherService, queue: queue)
  51. taskCompletion = completion
  52. }
  53. deinit {
  54. self.fetcher?.stopFetching()
  55. }
  56. /**
  57. * Prepares a task and begins execution.
  58. */
  59. internal func enqueue() {
  60. weak var weakSelf = self
  61. dispatchQueue.async {
  62. guard let strongSelf = weakSelf else { return }
  63. var queryParams = [String: String]()
  64. let prefix = strongSelf.reference.fullPath
  65. if prefix.count > 0 {
  66. queryParams["prefix"] = "\(prefix)/"
  67. }
  68. // Firebase Storage uses file system semantics and treats slashes as separators. GCS's List API
  69. // does not prescribe a separator, and hence we need to provide a slash as the delimiter.
  70. queryParams["delimiter"] = "/"
  71. // listAll() doesn't set a pageSize as this allows Firebase Storage to determine how many items
  72. // to return per page. This removes the need to backfill results if Firebase Storage filters
  73. // objects that are considered invalid (such as items with two consecutive slashes).
  74. if let pageSize = strongSelf.pageSize {
  75. queryParams["maxResults"] = "\(pageSize)"
  76. }
  77. if let previousPageToken = strongSelf.previousPageToken {
  78. queryParams["pageToken"] = previousPageToken
  79. }
  80. let root = strongSelf.reference.root()
  81. var request = StorageUtils.defaultRequestForReference(
  82. reference: root,
  83. queryParams: queryParams
  84. )
  85. request.httpMethod = "GET"
  86. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime
  87. let callback = strongSelf.taskCompletion
  88. strongSelf.taskCompletion = nil
  89. let fetcher = strongSelf.fetcherService.fetcher(with: request)
  90. fetcher.comment = "ListTask"
  91. strongSelf.fetcher = fetcher
  92. strongSelf.fetcherCompletion = { (data: Data?, error: NSError?) in
  93. var listResult: StorageListResult?
  94. if let error = error, self.error == nil {
  95. self.error = StorageErrorCode.error(withServerError: error, ref: strongSelf.reference)
  96. } else {
  97. if let data = data,
  98. let responseDictionary = try? JSONSerialization
  99. .jsonObject(with: data) as? [String: Any] {
  100. listResult = StorageListResult(with: responseDictionary, reference: self.reference)
  101. } else {
  102. self.error = StorageErrorCode.error(withInvalidRequest: data)
  103. }
  104. }
  105. callback?(listResult, self.error)
  106. self.fetcherCompletion = nil
  107. }
  108. strongSelf.fetcher?.beginFetch { data, error in
  109. let strongSelf = weakSelf
  110. if let fetcherCompletion = strongSelf?.fetcherCompletion {
  111. fetcherCompletion(data, error as? NSError)
  112. }
  113. }
  114. }
  115. }
  116. }