Result.swift 9.5 KB

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