Storage.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. import FirebaseAppCheckInterop
  16. import FirebaseAuthInterop
  17. import FirebaseCore
  18. // Avoids exposing internal FirebaseCore APIs to Swift users.
  19. @_implementationOnly import FirebaseCoreExtension
  20. /// Firebase Storage is a service that supports uploading and downloading binary objects,
  21. /// such as images, videos, and other files to Google Cloud Storage. Instances of `Storage`
  22. /// are not thread-safe, but can be accessed from any thread.
  23. ///
  24. /// If you call `Storage.storage()`, the instance will initialize with the default `FirebaseApp`,
  25. /// `FirebaseApp.app()`, and the storage location will come from the provided
  26. /// `GoogleService-Info.plist`.
  27. ///
  28. /// If you provide a custom instance of `FirebaseApp`,
  29. /// the storage location will be specified via the `FirebaseOptions.storageBucket` property.
  30. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  31. @objc(FIRStorage) open class Storage: NSObject {
  32. // MARK: - Public APIs
  33. /// The default `Storage` instance.
  34. /// - Returns: An instance of `Storage`, configured with the default `FirebaseApp`.
  35. @objc(storage) open class func storage() -> Storage {
  36. return storage(app: FirebaseApp.app()!)
  37. }
  38. /// A method used to create `Storage` instances initialized with a custom storage bucket URL.
  39. ///
  40. /// Any `StorageReferences` generated from this instance of `Storage` will reference files
  41. /// and directories within the specified bucket.
  42. /// - Parameter url: The `gs://` URL to your Firebase Storage bucket.
  43. /// - Returns: A `Storage` instance, configured with the custom storage bucket.
  44. @objc(storageWithURL:) open class func storage(url: String) -> Storage {
  45. return storage(app: FirebaseApp.app()!, url: url)
  46. }
  47. /// Creates an instance of `Storage`, configured with a custom `FirebaseApp`. `StorageReference`s
  48. /// generated from a resulting instance will reference files in the Firebase project
  49. /// associated with custom `FirebaseApp`.
  50. /// - Parameter app: The custom `FirebaseApp` used for initialization.
  51. /// - Returns: A `Storage` instance, configured with the custom `FirebaseApp`.
  52. @objc(storageForApp:) open class func storage(app: FirebaseApp) -> Storage {
  53. return storage(app: app, bucket: Storage.bucket(for: app))
  54. }
  55. /// Creates an instance of `Storage`, configured with a custom `FirebaseApp` and a custom storage
  56. /// bucket URL.
  57. /// - Parameters:
  58. /// - app: The custom `FirebaseApp` used for initialization.
  59. /// - url: The `gs://` url to your Firebase Storage bucket.
  60. /// - Returns: The `Storage` instance, configured with the custom `FirebaseApp` and storage bucket
  61. /// URL.
  62. @objc(storageForApp:URL:)
  63. open class func storage(app: FirebaseApp, url: String) -> Storage {
  64. return storage(app: app, bucket: Storage.bucket(for: app, urlString: url))
  65. }
  66. private class func storage(app: FirebaseApp, bucket: String) -> Storage {
  67. return InstanceCache.shared.storage(app: app, bucket: bucket)
  68. }
  69. /// The `FirebaseApp` associated with this Storage instance.
  70. @objc public let app: FirebaseApp
  71. /// The maximum time in seconds to retry an upload if a failure occurs.
  72. /// Defaults to 10 minutes (600 seconds).
  73. @objc public var maxUploadRetryTime: TimeInterval {
  74. didSet {
  75. maxUploadRetryInterval = Storage.computeRetryInterval(fromRetryTime: maxUploadRetryTime)
  76. }
  77. }
  78. /// The maximum time in seconds to retry a download if a failure occurs.
  79. /// Defaults to 10 minutes (600 seconds).
  80. @objc public var maxDownloadRetryTime: TimeInterval {
  81. didSet {
  82. maxDownloadRetryInterval = Storage.computeRetryInterval(fromRetryTime: maxDownloadRetryTime)
  83. }
  84. }
  85. /// The maximum time in seconds to retry operations other than upload and download if a failure
  86. /// occurs.
  87. /// Defaults to 2 minutes (120 seconds).
  88. @objc public var maxOperationRetryTime: TimeInterval {
  89. didSet {
  90. maxOperationRetryInterval = Storage.computeRetryInterval(fromRetryTime: maxOperationRetryTime)
  91. }
  92. }
  93. /// Specify the maximum upload chunk size. Values less than 256K (262144) will be rounded up to
  94. /// 256K. Values
  95. /// above 256K will be rounded down to the nearest 256K multiple. The default is no maximum.
  96. @objc public var uploadChunkSizeBytes: Int64 = .max
  97. /// A `DispatchQueue` that all developer callbacks are fired on. Defaults to the main queue.
  98. @objc public var callbackQueue: DispatchQueue = .main
  99. /// Creates a `StorageReference` initialized at the root Firebase Storage location.
  100. /// - Returns: An instance of `StorageReference` referencing the root of the storage bucket.
  101. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  102. @objc open func reference() -> StorageReference {
  103. configured = true
  104. let path = StoragePath(with: storageBucket)
  105. return StorageReference(storage: self, path: path)
  106. }
  107. /// Creates a StorageReference given a `gs://`, `http://`, or `https://` URL pointing to a
  108. /// Firebase Storage location.
  109. ///
  110. /// For example, you can pass in an `https://` download URL retrieved from
  111. /// `StorageReference.downloadURL(completion:)` or the `gs://` URL from
  112. /// `StorageReference.description`.
  113. /// - Parameter url: A gs:// or https:// URL to initialize the reference with.
  114. /// - Returns: An instance of StorageReference at the given child path.
  115. /// - Throws: Throws a fatal error if `url` is not associated with the `FirebaseApp` used to
  116. /// initialize this Storage instance.
  117. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  118. @objc open func reference(forURL url: String) -> StorageReference {
  119. configured = true
  120. do {
  121. let path = try StoragePath.path(string: url)
  122. // If no default bucket exists (empty string), accept anything.
  123. if storageBucket == "" {
  124. return StorageReference(storage: self, path: path)
  125. }
  126. // If there exists a default bucket, throw if provided a different bucket.
  127. if path.bucket != storageBucket {
  128. fatalError("Provided bucket: `\(path.bucket)` does not match the Storage bucket of the current " +
  129. "instance: `\(storageBucket)`")
  130. }
  131. return StorageReference(storage: self, path: path)
  132. } catch let StoragePathError.storagePathError(message) {
  133. fatalError(message)
  134. } catch {
  135. fatalError("Internal error finding StoragePath: \(error)")
  136. }
  137. }
  138. /// Creates a StorageReference given a `gs://`, `http://`, or `https://` URL pointing to a
  139. /// Firebase Storage location.
  140. ///
  141. /// For example, you can pass in an `https://` download URL retrieved from
  142. /// `StorageReference.downloadURL(completion:)` or the `gs://` URL from
  143. /// `StorageReference.description`.
  144. /// - Parameter url: A gs:// or https:// URL to initialize the reference with.
  145. /// - Returns: An instance of StorageReference at the given child path.
  146. /// - Throws: Throws an Error if `url` is not associated with the `FirebaseApp` used to initialize
  147. /// this Storage instance.
  148. open func reference(for url: URL) throws -> StorageReference {
  149. configured = true
  150. var path: StoragePath
  151. do {
  152. path = try StoragePath.path(string: url.absoluteString)
  153. } catch let StoragePathError.storagePathError(message) {
  154. throw StorageError.pathError(message: message)
  155. } catch {
  156. throw StorageError.pathError(message: "Internal error finding StoragePath: \(error)")
  157. }
  158. // If no default bucket exists (empty string), accept anything.
  159. if storageBucket == "" {
  160. return StorageReference(storage: self, path: path)
  161. }
  162. // If there exists a default bucket, throw if provided a different bucket.
  163. if path.bucket != storageBucket {
  164. throw StorageError
  165. .bucketMismatch(message: "Provided bucket: `\(path.bucket)` does not match the Storage " +
  166. "bucket of the current instance: `\(storageBucket)`")
  167. }
  168. return StorageReference(storage: self, path: path)
  169. }
  170. /// Creates a `StorageReference` initialized at a location specified by the `path` parameter.
  171. /// - Parameter path: A relative path from the root of the storage bucket,
  172. /// for instance @"path/to/object".
  173. /// - Returns: An instance of `StorageReference` pointing to the given path.
  174. @objc(referenceWithPath:) open func reference(withPath path: String) -> StorageReference {
  175. return reference().child(path)
  176. }
  177. /// Configures the Storage SDK to use an emulated backend instead of the default remote backend.
  178. ///
  179. /// This method should be called before invoking any other methods on a new instance of `Storage`.
  180. /// - Parameter host: A string specifying the host.
  181. /// - Parameter port: The port specified as an `Int`.
  182. @objc open func useEmulator(withHost host: String, port: Int) {
  183. guard host.count > 0 else {
  184. fatalError("Invalid host argument: Cannot connect to empty host.")
  185. }
  186. guard port >= 0 else {
  187. fatalError("Invalid port argument: Port must be greater or equal to zero.")
  188. }
  189. guard configured == false else {
  190. fatalError("Cannot connect to emulator after Storage SDK initialization. " +
  191. "Call useEmulator(host:port:) before creating a Storage " +
  192. "reference or trying to load data.")
  193. }
  194. usesEmulator = true
  195. scheme = "http"
  196. self.host = host
  197. self.port = port
  198. }
  199. // MARK: - NSObject overrides
  200. @objc override open func copy() -> Any {
  201. let storage = Storage(app: app, bucket: storageBucket)
  202. storage.callbackQueue = callbackQueue
  203. return storage
  204. }
  205. @objc override open func isEqual(_ object: Any?) -> Bool {
  206. guard let ref = object as? Storage else {
  207. return false
  208. }
  209. return app == ref.app && storageBucket == ref.storageBucket
  210. }
  211. @objc override public var hash: Int {
  212. return app.hash ^ callbackQueue.hashValue
  213. }
  214. // MARK: - Internal and Private APIs
  215. private final class InstanceCache: @unchecked Sendable {
  216. static let shared = InstanceCache()
  217. /// A map of active instances, grouped by app. Keys are FirebaseApp names and values are
  218. /// instances of Storage associated with the given app.
  219. private var instances: [String: Storage] = [:]
  220. /// Lock to manage access to the instances array to avoid race conditions.
  221. private var instancesLock: os_unfair_lock = .init()
  222. private init() {}
  223. func storage(app: FirebaseApp, bucket: String) -> Storage {
  224. os_unfair_lock_lock(&instancesLock)
  225. defer { os_unfair_lock_unlock(&instancesLock) }
  226. if let instance = instances[bucket] {
  227. return instance
  228. }
  229. let newInstance = FirebaseStorage.Storage(app: app, bucket: bucket)
  230. instances[bucket] = newInstance
  231. return newInstance
  232. }
  233. }
  234. let dispatchQueue: DispatchQueue
  235. init(app: FirebaseApp, bucket: String) {
  236. self.app = app
  237. auth = ComponentType<AuthInterop>.instance(for: AuthInterop.self,
  238. in: app.container)
  239. appCheck = ComponentType<AppCheckInterop>.instance(for: AppCheckInterop.self,
  240. in: app.container)
  241. storageBucket = bucket
  242. host = "firebasestorage.googleapis.com"
  243. scheme = "https"
  244. port = 443
  245. // Must be a serial queue.
  246. dispatchQueue = DispatchQueue(label: "com.google.firebase.storage")
  247. maxDownloadRetryTime = 600.0
  248. maxDownloadRetryInterval = Storage.computeRetryInterval(fromRetryTime: maxDownloadRetryTime)
  249. maxOperationRetryTime = 120.0
  250. maxOperationRetryInterval = Storage.computeRetryInterval(fromRetryTime: maxOperationRetryTime)
  251. maxUploadRetryTime = 600.0
  252. maxUploadRetryInterval = Storage.computeRetryInterval(fromRetryTime: maxUploadRetryTime)
  253. }
  254. let auth: AuthInterop?
  255. let appCheck: AppCheckInterop?
  256. let storageBucket: String
  257. var usesEmulator = false
  258. /// Once `configured` is true, the emulator can no longer be enabled.
  259. var configured = false
  260. var host: String
  261. var scheme: String
  262. var port: Int
  263. var maxDownloadRetryInterval: TimeInterval
  264. var maxOperationRetryInterval: TimeInterval
  265. var maxUploadRetryInterval: TimeInterval
  266. /// Performs a crude translation of the user provided timeouts to the retry intervals that
  267. /// GTMSessionFetcher accepts. GTMSessionFetcher times out operations if the time between
  268. /// individual retry attempts exceed a certain threshold, while our API contract looks at the
  269. /// total
  270. /// observed time of the operation (i.e. the sum of all retries).
  271. /// @param retryTime A timeout that caps the sum of all retry attempts
  272. /// @return A timeout that caps the timeout of the last retry attempt
  273. static func computeRetryInterval(fromRetryTime retryTime: TimeInterval) -> TimeInterval {
  274. // GTMSessionFetcher's retry starts at 1 second and then doubles every time. We use this
  275. // information to compute a best-effort estimate of what to translate the user provided retry
  276. // time into.
  277. // Note that this is the same as 2 << (log2(retryTime) - 1), but deemed more readable.
  278. var lastInterval = 1.0
  279. var sumOfAllIntervals = 1.0
  280. while sumOfAllIntervals < retryTime {
  281. lastInterval *= 2
  282. sumOfAllIntervals += lastInterval
  283. }
  284. return lastInterval
  285. }
  286. private static func bucket(for app: FirebaseApp) -> String {
  287. guard let bucket = app.options.storageBucket else {
  288. fatalError("No default Storage bucket found. Did you configure Firebase Storage properly?")
  289. }
  290. if bucket == "" {
  291. return Storage.bucket(for: app, urlString: "")
  292. } else {
  293. return Storage.bucket(for: app, urlString: "gs://\(bucket)/")
  294. }
  295. }
  296. private static func bucket(for app: FirebaseApp, urlString: String) -> String {
  297. if urlString == "" {
  298. return ""
  299. } else {
  300. guard let path = try? StoragePath.path(GSURI: urlString),
  301. path.object == nil || path.object == "" else {
  302. fatalError("Internal Error: Storage bucket cannot be initialized with a path")
  303. }
  304. return path.bucket
  305. }
  306. }
  307. }