Result.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Foundation
  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?) in
  27. if let value {
  28. completion(.success(value))
  29. } else if let error {
  30. completion(.failure(error))
  31. } else {
  32. completion(.failure(StorageError.internalError(
  33. message: "Internal failure in getResultCallback"
  34. )))
  35. }
  36. }
  37. }
  38. public extension StorageReference {
  39. /// Asynchronously retrieves a long lived download URL with a revokable token.
  40. ///
  41. /// This can be used to share the file with others, but can be revoked by a developer
  42. /// in the Firebase Console.
  43. ///
  44. /// - Parameters:
  45. /// - completion: A completion block returning a `Result` enum with either a URL or an `Error`.
  46. func downloadURL(completion: @escaping (Result<URL, Error>) -> Void) {
  47. downloadURL(completion: getResultCallback(completion: completion))
  48. }
  49. /// Asynchronously downloads the object at the `StorageReference` to a `Data` object.
  50. ///
  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, the `write` API 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. ///
  74. /// Returns the next set of items (files) and prefixes (folders) under this StorageReference.
  75. ///
  76. /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  77. /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  78. /// filtered.
  79. ///
  80. /// Only available for projects using Firebase Rules Version 2.
  81. ///
  82. /// - Parameters:
  83. /// - maxResults: The maximum number of results to return in a single page. Must be
  84. /// greater than 0 and at most 1000.
  85. /// - pageToken: A page token from a previous call to list.
  86. /// - completion: A completion handler that will be invoked with the next items and
  87. /// prefixes under the current StorageReference. It returns a `Result` enum
  88. /// with either the list or an `Error`.
  89. func list(maxResults: Int64,
  90. pageToken: String,
  91. completion: @escaping (Result<StorageListResult, Error>) -> Void) {
  92. list(maxResults: maxResults,
  93. pageToken: pageToken,
  94. completion: getResultCallback(completion: completion))
  95. }
  96. /// List up to `maxResults` items (files) and prefixes (folders) under this StorageReference.
  97. ///
  98. /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  99. /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  100. /// filtered.
  101. ///
  102. /// Only available for projects using Firebase Rules Version 2.
  103. ///
  104. /// - Parameters:
  105. /// - maxResults: The maximum number of results to return in a single page. Must be
  106. /// greater than 0 and at most 1000.
  107. /// - completion: A completion handler that will be invoked with the next items and
  108. /// prefixes under the current `StorageReference`. It returns a `Result` enum
  109. /// with either the list or an `Error`.
  110. func list(maxResults: Int64,
  111. completion: @escaping (Result<StorageListResult, Error>) -> Void) {
  112. list(maxResults: maxResults,
  113. completion: getResultCallback(completion: completion))
  114. }
  115. /// List all items (files) and prefixes (folders) under this StorageReference.
  116. ///
  117. /// This is a helper method for calling list() repeatedly until there are no more results.
  118. /// Consistency of the result is not guaranteed if objects are inserted or removed while this
  119. /// operation is executing. All results are buffered in memory.
  120. ///
  121. /// Only available for projects using Firebase Rules Version 2.
  122. ///
  123. /// - Parameters:
  124. /// - completion: A completion handler that will be invoked with all items and prefixes
  125. /// under the current StorageReference. It returns a `Result` enum with either the
  126. /// list or an `Error`.
  127. func listAll(completion: @escaping (Result<StorageListResult, Error>) -> Void) {
  128. listAll(completion: getResultCallback(completion: completion))
  129. }
  130. /// Asynchronously uploads data to the currently specified `StorageReference`.
  131. /// This is not recommended for large files, and one should instead upload a file from disk.
  132. ///
  133. /// - Parameters:
  134. /// - uploadData: The `Data` to upload.
  135. /// - metadata: `StorageMetadata` containing additional information (MIME type, etc.)
  136. /// about the object being uploaded.
  137. /// - completion: A completion block that returns a `Result` enum with either the
  138. /// object metadata or an `Error`.
  139. ///
  140. /// - Returns: An instance of `StorageUploadTask`, which can be used to monitor or manage
  141. /// the upload.
  142. @discardableResult
  143. func putData(_ uploadData: Data,
  144. metadata: StorageMetadata? = nil,
  145. completion: @escaping (Result<StorageMetadata, Error>) -> Void)
  146. -> StorageUploadTask {
  147. return putData(uploadData,
  148. metadata: metadata,
  149. completion: getResultCallback(completion: completion))
  150. }
  151. /// Asynchronously uploads a file to the currently specified `StorageReference`.
  152. ///
  153. /// - Parameters:
  154. /// - from: A URL representing the system file path of the object to be uploaded.
  155. /// - metadata: `StorageMetadata` containing additional information (MIME type, etc.)
  156. /// about the object being uploaded.
  157. /// - completion: A completion block that returns a `Result` enum with either the
  158. /// object metadata or an `Error`.
  159. ///
  160. /// - Returns: An instance of `StorageUploadTask`, which can be used to monitor or manage
  161. /// the upload.
  162. @discardableResult
  163. func putFile(from: URL,
  164. metadata: StorageMetadata? = nil,
  165. completion: @escaping (Result<StorageMetadata, Error>) -> Void)
  166. -> StorageUploadTask {
  167. return putFile(from: from,
  168. metadata: metadata,
  169. completion: getResultCallback(completion: completion))
  170. }
  171. /// Updates the metadata associated with an object at the current path.
  172. ///
  173. /// - Parameters:
  174. /// - metadata: A `StorageMetadata` object with the metadata to update.
  175. /// - completion: A completion block which returns a `Result` enum with either the
  176. /// object metadata or an `Error`.
  177. func updateMetadata(_ metadata: StorageMetadata,
  178. completion: @escaping (Result<StorageMetadata, Error>) -> Void) {
  179. updateMetadata(metadata, completion: getResultCallback(completion: completion))
  180. }
  181. /// Asynchronously downloads the object at the current path to a specified system filepath.
  182. ///
  183. /// - Parameters:
  184. /// - toFile: A file system URL representing the path the object should be downloaded to.
  185. /// - completion: A completion block that fires when the file download completes. The
  186. /// block returns a `Result` enum with either an NSURL pointing to the file
  187. /// path of the downloaded file or an `Error`.
  188. ///
  189. /// - Returns: A `StorageDownloadTask` that can be used to monitor or manage the download.
  190. @discardableResult
  191. func write(toFile: URL, completion: @escaping (Result<URL, Error>)
  192. -> Void) -> StorageDownloadTask {
  193. return write(toFile: toFile, completion: getResultCallback(completion: completion))
  194. }
  195. }