StorageGetMetadataTask.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * Task which provides the ability to delete an object in Firebase Storage.
  22. */
  23. internal class StorageGetMetadataTask: StorageTask, StorageTaskManagement {
  24. private var fetcher: GTMSessionFetcher?
  25. private var fetcherCompletion: ((Data?, NSError?) -> Void)?
  26. private var taskCompletion: ((_ metadata: StorageMetadata?, _: Error?) -> Void)?
  27. internal init(reference: StorageReference,
  28. fetcherService: GTMSessionFetcherService,
  29. queue: DispatchQueue,
  30. completion: ((_: StorageMetadata?, _: Error?) -> Void)?) {
  31. super.init(reference: reference, service: fetcherService, queue: queue)
  32. taskCompletion = completion
  33. }
  34. deinit {
  35. self.fetcher?.stopFetching()
  36. }
  37. /**
  38. * Prepares a task and begins execution.
  39. */
  40. internal func enqueue() {
  41. weak var weakSelf = self
  42. dispatchQueue.async {
  43. guard let strongSelf = weakSelf else { return }
  44. strongSelf.state = .queueing
  45. var request = strongSelf.baseRequest
  46. request.httpMethod = "GET"
  47. request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime
  48. let callback = strongSelf.taskCompletion
  49. strongSelf.taskCompletion = nil
  50. let fetcher = strongSelf.fetcherService.fetcher(with: request)
  51. fetcher.comment = "GetMetadataTask"
  52. strongSelf.fetcher = fetcher
  53. strongSelf.fetcherCompletion = { (data: Data?, error: NSError?) in
  54. var metadata: StorageMetadata?
  55. if let error = error {
  56. if self.error == nil {
  57. self.error = StorageErrorCode.error(withServerError: error, ref: self.reference)
  58. }
  59. } else {
  60. if let data = data,
  61. let responseDictionary = try? JSONSerialization
  62. .jsonObject(with: data) as? [String: AnyHashable] {
  63. metadata = StorageMetadata(dictionary: responseDictionary)
  64. metadata?.fileType = .file
  65. } else {
  66. self.error = StorageErrorCode.error(withInvalidRequest: data)
  67. }
  68. }
  69. callback?(metadata, self.error)
  70. self.fetcherCompletion = nil
  71. }
  72. strongSelf.fetcher?.beginFetch { data, error in
  73. let strongSelf = weakSelf
  74. if let fetcherCompletion = strongSelf?.fetcherCompletion {
  75. fetcherCompletion(data, error as? NSError)
  76. }
  77. }
  78. }
  79. }
  80. }