StorageUtils.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. private import UniformTypeIdentifiers
  16. #if os(iOS) || os(tvOS) || os(visionOS)
  17. import MobileCoreServices
  18. #elseif os(macOS) || os(watchOS)
  19. import CoreServices
  20. #endif // os(iOS) || os(tvOS)
  21. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  22. class StorageUtils {
  23. class func defaultRequestForReference(reference: StorageReference,
  24. queryParams: [String: String]? = nil)
  25. -> URLRequest {
  26. var components = URLComponents()
  27. components.scheme = reference.storage.scheme
  28. components.host = reference.storage.host
  29. components.port = reference.storage.port
  30. if let queryParams {
  31. var queryItems = [URLQueryItem]()
  32. for (key, value) in queryParams {
  33. queryItems.append(URLQueryItem(name: key, value: value))
  34. }
  35. components.queryItems = queryItems
  36. // NSURLComponents does not encode "+" as "%2B". This is however required by our backend, as
  37. // it treats "+" as a shorthand encoding for spaces. See also
  38. // https://stackoverflow.com/questions/31577188/how-to-encode-into-2b-with-nsurlcomponents
  39. components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(
  40. of: "+",
  41. with: "%2B"
  42. )
  43. }
  44. let encodedPath = encodedURL(for: reference.path)
  45. components.percentEncodedPath = encodedPath
  46. guard let url = components.url else {
  47. fatalError("FirebaseStorage Internal Error: Failed to create url for \(reference.bucket)")
  48. }
  49. return URLRequest(url: url)
  50. }
  51. class func encodedURL(for path: StoragePath) -> String {
  52. let bucketString = "/b/\(GCSEscapedString(path.bucket))"
  53. var objectString: String
  54. if let objectName = path.object {
  55. objectString = "/o/\(GCSEscapedString(objectName))"
  56. } else {
  57. objectString = "/o"
  58. }
  59. return "/v0\(bucketString)\(objectString)"
  60. }
  61. class func GCSEscapedString(_ string: String) -> String {
  62. // This is the list at https://cloud.google.com/storage/docs/json_api/ without &, ; and +.
  63. let allowedSet =
  64. CharacterSet(
  65. charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~!$'()*,=:@"
  66. )
  67. return string.addingPercentEncoding(withAllowedCharacters: allowedSet)!
  68. }
  69. static func MIMETypeForExtension(_ fileExtension: String?) -> String {
  70. guard let fileExtension else {
  71. return "application/octet-stream"
  72. }
  73. // TODO: Remove `else` when min. supported macOS is 11.0+.
  74. if #available(macOS 11.0, iOS 14.0, tvOS 14.0, *) {
  75. guard let mimeType = UTType(filenameExtension: fileExtension)?.preferredMIMEType else {
  76. return "application/octet-stream"
  77. }
  78. return mimeType
  79. } else {
  80. if let type = UTTypeCreatePreferredIdentifierForTag(
  81. kUTTagClassFilenameExtension,
  82. fileExtension as NSString,
  83. nil
  84. )?.takeRetainedValue() {
  85. if let mimeType = UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType)?
  86. .takeRetainedValue() {
  87. return mimeType as String
  88. }
  89. }
  90. return "application/octet-stream"
  91. }
  92. }
  93. }