StorageGetDownloadURLTask.swift 3.1 KB

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