Result.swift 9.4 KB

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