StorageGetDownloadURLTask.swift 3.3 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. /// Task which provides the ability to get a download URL for an object in Firebase Storage.
  21. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  22. enum StorageGetDownloadURLTask {
  23. static func getDownloadURLTask(reference: StorageReference,
  24. fetcherService: GTMSessionFetcherService,
  25. queue: DispatchQueue,
  26. completion: ((_: URL?, _: Error?) -> Void)?) {
  27. StorageInternalTask(reference: reference,
  28. fetcherService: fetcherService,
  29. queue: queue,
  30. httpMethod: "GET",
  31. fetcherComment: "GetDownloadURLTask") { (data: Data?, error: Error?) in
  32. if let error {
  33. completion?(nil, error)
  34. } else {
  35. if let data,
  36. let responseDictionary = try? JSONSerialization
  37. .jsonObject(with: data) as? [String: Any] {
  38. guard let downloadURL = downloadURLFromMetadataDictionary(responseDictionary,
  39. reference) else {
  40. let error = StorageError.unknown(
  41. message: "Failed to retrieve a download URL.",
  42. serverError: [:]
  43. ) as NSError
  44. completion?(nil, error)
  45. return
  46. }
  47. completion?(downloadURL, nil)
  48. } else {
  49. completion?(nil, StorageErrorCode.error(withInvalidRequest: data))
  50. }
  51. }
  52. }
  53. }
  54. static func downloadURLFromMetadataDictionary(_ dictionary: [String: Any],
  55. _ reference: StorageReference) -> URL? {
  56. let downloadTokens = dictionary["downloadTokens"]
  57. guard let downloadTokens = downloadTokens as? String,
  58. downloadTokens.count > 0 else {
  59. return nil
  60. }
  61. let downloadTokenArray = downloadTokens.components(separatedBy: ",")
  62. let bucket = dictionary["bucket"] ?? "<error: missing bucket>"
  63. let path = dictionary["name"] as? String ?? "<error: missing path name>"
  64. let fullPath = "/v0/b/\(bucket)/o/\(StorageUtils.GCSEscapedString(path))"
  65. var components = URLComponents()
  66. components.scheme = reference.storage.scheme
  67. components.host = reference.storage.host
  68. components.port = reference.storage.port
  69. components.percentEncodedPath = fullPath
  70. // The backend can return an arbitrary number of download tokens, but we only expose the first
  71. // token via the download URL.
  72. let altItem = URLQueryItem(name: "alt", value: "media")
  73. let tokenItem = URLQueryItem(name: "token", value: downloadTokenArray[0])
  74. components.queryItems = [altItem, tokenItem]
  75. return components.url
  76. }
  77. }