Storage.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 FirebaseStorageInternal
  16. import FirebaseCore
  17. import FirebaseAppCheckInterop
  18. import FirebaseAuthInterop
  19. // Avoids exposing internal FirebaseCore APIs to Swift users.
  20. @_implementationOnly import FirebaseCoreExtension
  21. /**
  22. * Firebase Storage is a service that supports uploading and downloading binary objects,
  23. * such as images, videos, and other files to Google Cloud Storage. Instances of `Storage`
  24. * are not thread-safe.
  25. *
  26. * If you call `Storage.storage()`, the instance will initialize with the default `FirebaseApp`,
  27. * `FirebaseApp.app()`, and the storage location will come from the provided
  28. * `GoogleService-Info.plist`.
  29. *
  30. * If you provide a custom instance of `FirebaseApp`,
  31. * the storage location will be specified via the `FirebaseOptions.storageBucket` property.
  32. */
  33. @objc(FIRStorage) open class Storage: NSObject {
  34. // MARK: - Public APIs
  35. /**
  36. * The default `Storage` instance.
  37. * - Returns: An instance of `Storage`, configured with the default `FirebaseApp`.
  38. */
  39. @objc(storage) open class func storage() -> Storage {
  40. return storage(app: FirebaseApp.app()!)
  41. }
  42. /**
  43. * A method used to create `Storage` instances initialized with a custom storage bucket URL.
  44. * Any `StorageReferences` generated from this instance of `Storage` will reference files
  45. * and directories within the specified bucket.
  46. * - Parameter url The `gs://` URL to your Firebase Storage bucket.
  47. * - Returns: A `Storage` instance, configured with the custom storage bucket.
  48. */
  49. @objc(storageWithURL:) open class func storage(url: String) -> Storage {
  50. return storage(app: FirebaseApp.app()!, url: url)
  51. }
  52. /**
  53. * Creates an instance of `Storage`, configured with a custom `FirebaseApp`. `StorageReference`s
  54. * generated from a resulting instance will reference files in the Firebase project
  55. * associated with custom `FirebaseApp`.
  56. * - Parameter app The custom `FirebaseApp` used for initialization.
  57. * - Returns: A `Storage` instance, configured with the custom `FirebaseApp`.
  58. */
  59. @objc(storageForApp:) open class func storage(app: FirebaseApp) -> Storage {
  60. let provider = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
  61. in: app.container)
  62. return provider.storage(for: FIRIMPLStorage.bucket(for: app))
  63. }
  64. /**
  65. * Creates an instance of `Storage`, configured with a custom `FirebaseApp` and a custom storage
  66. * bucket URL.
  67. * - Parameters:
  68. * - app: The custom `FirebaseApp` used for initialization.
  69. * - url: The `gs://` url to your Firebase Storage bucket.
  70. * - Returns: the `Storage` instance, configured with the custom `FirebaseApp` and storage bucket URL.
  71. */
  72. @objc(storageForApp:URL:)
  73. open class func storage(app: FirebaseApp, url: String) -> Storage {
  74. let provider = ComponentType<StorageProvider>.instance(for: StorageProvider.self,
  75. in: app.container)
  76. return provider.storage(for: FIRIMPLStorage.bucket(for: app, url: url))
  77. }
  78. /**
  79. * The `FirebaseApp` associated with this Storage instance.
  80. */
  81. @objc public let app: FirebaseApp
  82. /**
  83. * The maximum time in seconds to retry an upload if a failure occurs.
  84. * Defaults to 10 minutes (600 seconds).
  85. */
  86. @objc public var maxUploadRetryTime: TimeInterval {
  87. get {
  88. return impl.maxUploadRetryTime
  89. }
  90. set(newValue) {
  91. impl.maxUploadRetryTime = newValue
  92. }
  93. }
  94. /**
  95. * The maximum time in seconds to retry a download if a failure occurs.
  96. * Defaults to 10 minutes (600 seconds).
  97. */
  98. @objc public var maxDownloadRetryTime: TimeInterval {
  99. get {
  100. return impl.maxDownloadRetryTime
  101. }
  102. set(newValue) {
  103. impl.maxDownloadRetryTime = newValue
  104. }
  105. }
  106. /**
  107. * The maximum time in seconds to retry operations other than upload and download if a failure occurs.
  108. * Defaults to 2 minutes (120 seconds).
  109. */
  110. @objc public var maxOperationRetryTime: TimeInterval {
  111. get {
  112. return impl.maxOperationRetryTime
  113. }
  114. set(newValue) {
  115. impl.maxOperationRetryTime = newValue
  116. }
  117. }
  118. /**
  119. * A `DispatchQueue` that all developer callbacks are fired on. Defaults to the main queue.
  120. */
  121. @objc public var callbackQueue: DispatchQueue {
  122. get {
  123. return impl.callbackQueue
  124. }
  125. set(newValue) {
  126. impl.callbackQueue = newValue
  127. }
  128. }
  129. /**
  130. * Creates a `StorageReference` initialized at the root Firebase Storage location.
  131. * - Returns: An instance of `StorageReference` referencing the root of the storage bucket.
  132. */
  133. @objc open func reference() -> StorageReference {
  134. return StorageReference(impl: impl.reference(), storage: self)
  135. }
  136. /**
  137. * Creates a StorageReference given a `gs://` or `https://` URL pointing to a Firebase Storage
  138. * location. For example, you can pass in an `https://` download URL retrieved from
  139. * `StorageReference.downloadURL(completion:)` or the `gs://` URL from
  140. * `StorageReference.description`.
  141. * - Parameter url A gs:// or https:// URL to initialize the reference with.
  142. * - Returns: An instance of StorageReference at the given child path.
  143. * - Throws: Throws a fatal error if `url` is not associated with the `FirebaseApp` used to initialize
  144. * this Storage instance.
  145. */
  146. @objc open func reference(forURL url: String) -> StorageReference {
  147. return StorageReference(impl: impl.reference(forURL: url), storage: self)
  148. }
  149. /**
  150. * Creates a `StorageReference` initialized at a location specified by the `path` parameter.
  151. * - Parameter path A relative path from the root of the storage bucket,
  152. * for instance @"path/to/object".
  153. * - Returns: An instance of `StorageReference` pointing to the given path.
  154. */
  155. @objc(referenceWithPath:) open func reference(withPath path: String) -> StorageReference {
  156. return StorageReference(impl: impl.reference(withPath: path), storage: self)
  157. }
  158. /**
  159. * Configures the Storage SDK to use an emulated backend instead of the default remote backend.
  160. * This method should be called before invoking any other methods on a new instance of `Storage`.
  161. */
  162. @objc open func useEmulator(withHost host: String, port: Int) {
  163. impl.useEmulator(withHost: host, port: port)
  164. }
  165. // MARK: - NSObject overrides
  166. @objc override open func copy() -> Any {
  167. return Storage(copy: self)
  168. }
  169. @objc override open func isEqual(_ object: Any?) -> Bool {
  170. guard let ref = object as? Storage else {
  171. return false
  172. }
  173. return impl.isEqual(ref.impl)
  174. }
  175. @objc override public var hash: Int {
  176. return impl.hash
  177. }
  178. @objc override public var description: String {
  179. return impl.description
  180. }
  181. // MARK: - Internal APIs
  182. private let impl: FIRIMPLStorage
  183. internal init(app: FirebaseApp, bucket: String) {
  184. let auth = ComponentType<AuthInterop>.instance(for: AuthInterop.self,
  185. in: app.container)
  186. let appCheck = ComponentType<AppCheckInterop>.instance(for: AppCheckInterop.self,
  187. in: app.container)
  188. impl = FIRIMPLStorage(app: app, bucket: bucket, auth: auth, appCheck: appCheck)
  189. self.app = impl.app
  190. }
  191. internal init(copy: Storage) {
  192. impl = copy.impl.copy() as! FIRIMPLStorage
  193. app = impl.app
  194. }
  195. }