SwiftAPIExtension.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2020 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 FirebaseStorage
  15. /// Generates a closure that returns a `Result` type from a closure that returns an optional type
  16. /// and `Error`.
  17. ///
  18. /// - Parameters:
  19. /// - completion: A completion block returning a `Result` enum with either a generic object or
  20. /// an `Error`.
  21. /// - Returns: A closure parameterized with an optional generic and optional `Error` to match
  22. /// Objective C APIs.
  23. private func getResultCallback<T>(completion: @escaping (Result<T, Error>) -> Void) -> (_: T?,
  24. _: Error?)
  25. -> Void {
  26. return { (value: T?, error: Error?) -> Void in
  27. if let value = value {
  28. completion(.success(value))
  29. } else if let error = error {
  30. completion(.failure(error))
  31. } else {
  32. completion(.failure(NSError(domain: "FirebaseStorageSwift",
  33. code: -1,
  34. userInfo: [NSLocalizedDescriptionKey:
  35. "InternalError - Return type and Error code both nil in " +
  36. "Storage Result generator"])))
  37. }
  38. }
  39. }
  40. public extension StorageReference {
  41. /// Asynchronously retrieves a long lived download URL with a revokable token.
  42. /// This can be used to share the file with others, but can be revoked by a developer
  43. /// in the Firebase Console if desired.
  44. ///
  45. /// - Parameters:
  46. /// - completion: A completion block returning a `Result` enum with either a URL or an `Error`.
  47. func downloadURL(completion: @escaping (Result<URL, Error>) -> Void) {
  48. downloadURL(completion: getResultCallback(completion: completion))
  49. }
  50. /// Asynchronously downloads the object at the `StorageReference` to a `Data` object.
  51. /// A `Data` of the provided max size will be allocated, so ensure that the device has enough
  52. /// memory to complete. For downloading large files, writeToFile may be a better option.
  53. /// - Parameters:
  54. /// - maxSize: The maximum size in bytes to download.
  55. /// - completion: A completion block returning a `Result` enum with either a `Data` object or
  56. /// an `Error`.
  57. ///
  58. /// - Returns: A StorageDownloadTask that can be used to monitor or manage the download.
  59. @discardableResult
  60. func getData(maxSize: Int64, completion: @escaping (Result<Data, Error>) -> Void)
  61. -> StorageDownloadTask {
  62. return getData(maxSize: maxSize, completion: getResultCallback(completion: completion))
  63. }
  64. /// Retrieves metadata associated with an object at the current path.
  65. ///
  66. /// - Parameters:
  67. /// - completion: A completion block which returns a `Result` enum with either the
  68. /// object metadata or an `Error`.
  69. func getMetadata(completion: @escaping (Result<StorageMetadata, Error>) -> Void) {
  70. getMetadata(completion: getResultCallback(completion: completion))
  71. }
  72. /// Resumes a previous `list` call, starting after a pagination token.
  73. /// Returns the next set of items (files) and prefixes (folders) under this StorageReference.
  74. ///
  75. /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  76. /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  77. /// filtered.
  78. ///
  79. /// Only available for projects using Firebase Rules Version 2.
  80. ///
  81. /// - Parameters:
  82. /// - maxResults The maximum number of results to return in a single page. Must be
  83. /// greater than 0 and at most 1000.
  84. /// - pageToken A page token from a previous call to list.
  85. /// - completion A completion handler that will be invoked with the next items and
  86. /// prefixes under the current StorageReference. It returns a `Result` enum
  87. /// with either the list or an `Error`.
  88. func list(maxResults: Int64,
  89. pageToken: String,
  90. completion: @escaping (Result<StorageListResult, Error>) -> Void) {
  91. list(maxResults: maxResults,
  92. pageToken: pageToken,
  93. completion: getResultCallback(completion: completion))
  94. }
  95. /// List up to `maxResults` items (files) and prefixes (folders) under this StorageReference.
  96. ///
  97. /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  98. /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  99. /// filtered.
  100. ///
  101. /// Only available for projects using Firebase Rules Version 2.
  102. ///
  103. /// - Parameters:
  104. /// - maxResults The maximum number of results to return in a single page. Must be
  105. /// greater than 0 and at most 1000.
  106. /// - completion A completion handler that will be invoked with the next items and
  107. /// prefixes under the current `StorageReference`. It returns a `Result` enum
  108. /// with either the list or an `Error`.
  109. func list(maxResults: Int64,
  110. completion: @escaping (Result<StorageListResult, Error>) -> Void) {
  111. list(maxResults: maxResults,
  112. completion: getResultCallback(completion: completion))
  113. }
  114. /// List all items (files) and prefixes (folders) under this StorageReference.
  115. ///
  116. /// This is a helper method for calling list() repeatedly until there are no more results.
  117. /// Consistency of the result is not guaranteed if objects are inserted or removed while this
  118. /// operation is executing. All results are buffered in memory.
  119. ///
  120. /// Only available for projects using Firebase Rules Version 2.
  121. ///
  122. /// - Parameters:
  123. /// - completion A completion handler that will be invoked with all items and prefixes
  124. /// under the current StorageReference. It returns a `Result` enum with either the
  125. /// list or an `Error`.
  126. func listAll(completion: @escaping (Result<StorageListResult, Error>) -> Void) {
  127. listAll(completion: getResultCallback(completion: completion))
  128. }
  129. /// Asynchronously uploads data to the currently specified `StorageReference`.
  130. /// This is not recommended for large files, and one should instead upload a file from disk.
  131. ///
  132. /// - Parameters:
  133. /// - uploadData The `Data` to upload.
  134. /// - metadata `StorageMetadata` containing additional information (MIME type, etc.)
  135. /// about the object being uploaded.
  136. /// - completion A completion block that returns a `Result` enum with either the
  137. /// object metadata or an `Error`.
  138. ///
  139. /// - Returns: An instance of `StorageUploadTask`, which can be used to monitor or manage
  140. /// the upload.
  141. @discardableResult
  142. func putData(_ uploadData: Data,
  143. metadata: StorageMetadata? = nil,
  144. completion: @escaping (Result<StorageMetadata, Error>) -> Void)
  145. -> StorageUploadTask {
  146. return putData(uploadData,
  147. metadata: metadata,
  148. completion: getResultCallback(completion: completion))
  149. }
  150. /// Asynchronously uploads a file to the currently specified `StorageReference`.
  151. ///
  152. /// - Parameters:
  153. /// - from A URL representing the system file path of the object to be uploaded.
  154. /// - metadata `StorageMetadata` containing additional information (MIME type, etc.)
  155. /// about the object being uploaded.
  156. /// - completion A completion block that returns a `Result` enum with either the
  157. /// object metadata or an `Error`.
  158. ///
  159. /// - Returns: An instance of `StorageUploadTask`, which can be used to monitor or manage
  160. /// the upload.
  161. @discardableResult
  162. func putFile(from: URL,
  163. metadata: StorageMetadata? = nil,
  164. completion: @escaping (Result<StorageMetadata, Error>) -> Void)
  165. -> StorageUploadTask {
  166. return putFile(from: from,
  167. metadata: metadata,
  168. completion: getResultCallback(completion: completion))
  169. }
  170. /// Updates the metadata associated with an object at the current path.
  171. ///
  172. /// - Parameters:
  173. /// - metadata A `StorageMetadata` object with the metadata to update.
  174. /// - completion A completion block which returns a `Result` enum with either the
  175. /// object metadata or an `Error`.
  176. func updateMetadata(_ metadata: StorageMetadata,
  177. completion: @escaping (Result<StorageMetadata, Error>) -> Void) {
  178. updateMetadata(metadata, completion: getResultCallback(completion: completion))
  179. }
  180. /// Asynchronously downloads the object at the current path to a specified system filepath.
  181. ///
  182. /// - Parameters:
  183. /// - toFile A file system URL representing the path the object should be downloaded to.
  184. /// - completion A completion block that fires when the file download completes. The
  185. /// block returns a `Result` enum with either an NSURL pointing to the file
  186. /// path of the downloaded file or an `Error`.
  187. ///
  188. /// - Returns: A `StorageDownloadTask` that can be used to monitor or manage the download.
  189. @discardableResult
  190. func write(toFile: URL, completion: @escaping (Result<URL, Error>)
  191. -> Void) -> StorageDownloadTask {
  192. return write(toFile: toFile, completion: getResultCallback(completion: completion))
  193. }
  194. }