Storage.swift 7.4 KB

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