Storage.swift 7.2 KB

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