AsyncAwait.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2021 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. #if compiler(>=5.5.2) && canImport(_Concurrency)
  16. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  17. public extension StorageReference {
  18. /// Asynchronously downloads the object at the StorageReference to a Data object in memory.
  19. /// A Data object of the provided max size will be allocated, so ensure that the device has
  20. /// enough free memory to complete the download. For downloading large files, the `write`
  21. /// API may be a better option.
  22. ///
  23. /// - Parameters:
  24. /// - size: The maximum size in bytes to download. If the download exceeds this size,
  25. /// the task will be cancelled and an error will be thrown.
  26. /// - Throws:
  27. /// - An error if the operation failed, for example if the data exceeded `maxSize`.
  28. /// - Returns: Data object.
  29. func data(maxSize: Int64) async throws -> Data {
  30. return try await withCheckedThrowingContinuation { continuation in
  31. _ = self.getData(maxSize: maxSize) { result in
  32. continuation.resume(with: result)
  33. }
  34. }
  35. }
  36. /// Asynchronously uploads data to the currently specified StorageReference.
  37. /// This is not recommended for large files, and one should instead upload a file from disk
  38. /// from the Firebase Console.
  39. ///
  40. /// - Parameters:
  41. /// - uploadData: The Data to upload.
  42. /// - metadata: Optional StorageMetadata containing additional information (MIME type, etc.)
  43. /// about the object being uploaded.
  44. /// - Throws:
  45. /// - An error if the operation failed, for example if Storage was unreachable.
  46. /// - Returns: StorageMetadata with additional information about the object being uploaded.
  47. func putDataAsync(_ uploadData: Data,
  48. metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
  49. return try await withCheckedThrowingContinuation { continuation in
  50. _ = self.putData(uploadData, metadata: metadata) { result in
  51. continuation.resume(with: result)
  52. }
  53. }
  54. }
  55. /// Asynchronously uploads a file to the currently specified StorageReference.
  56. /// `putDataAsync` should be used instead of `putFileAsync` in Extensions.
  57. ///
  58. /// - Parameters:
  59. /// - url: A URL representing the system file path of the object to be uploaded.
  60. /// - metadata: Optional StorageMetadata containing additional information (MIME type, etc.)
  61. /// about the object being uploaded.
  62. /// - Throws:
  63. /// - An error if the operation failed, for example if no file was present at the specified `url`.
  64. /// - Returns: `StorageMetadata` with additional information about the object being uploaded.
  65. func putFileAsync(from url: URL,
  66. metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
  67. return try await withCheckedThrowingContinuation { continuation in
  68. _ = self.putFile(from: url, metadata: metadata) { result in
  69. continuation.resume(with: result)
  70. }
  71. }
  72. }
  73. /// Asynchronously downloads the object at the current path to a specified system filepath.
  74. ///
  75. /// - Parameters:
  76. /// - fileUrl: A URL representing the system file path of the object to be uploaded.
  77. /// - Throws:
  78. /// - An error if the operation failed, for example if Storage was unreachable
  79. /// or `fileURL` did not reference a valid path on disk.
  80. /// - Returns: A `URL` pointing to the file path of the downloaded file.
  81. func writeAsync(toFile fileURL: URL) async throws -> URL {
  82. return try await withCheckedThrowingContinuation { continuation in
  83. _ = self.write(toFile: fileURL) { result in
  84. continuation.resume(with: result)
  85. }
  86. }
  87. }
  88. /// List up to `maxResults` items (files) and prefixes (folders) under this StorageReference.
  89. ///
  90. /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  91. /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  92. /// filtered.
  93. ///
  94. /// Only available for projects using Firebase Rules Version 2.
  95. ///
  96. /// - Parameters:
  97. /// - maxResults The maximum number of results to return in a single page. Must be
  98. /// greater than 0 and at most 1000.
  99. /// - Throws:
  100. /// - An error if the operation failed, for example if Storage was unreachable
  101. /// or the storage reference referenced an invalid path.
  102. /// - Returns:
  103. /// - A `StorageListResult` containing the contents of the storage reference.
  104. func list(maxResults: Int64) async throws -> StorageListResult {
  105. typealias ListContinuation = CheckedContinuation<StorageListResult, Error>
  106. return try await withCheckedThrowingContinuation { (continuation: ListContinuation) in
  107. self.list(maxResults: maxResults) { result in
  108. continuation.resume(with: result)
  109. }
  110. }
  111. }
  112. /// List up to `maxResults` items (files) and prefixes (folders) under this StorageReference.
  113. ///
  114. /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  115. /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  116. /// filtered.
  117. ///
  118. /// Only available for projects using Firebase Rules Version 2.
  119. ///
  120. /// - Parameters:
  121. /// - maxResults The maximum number of results to return in a single page. Must be
  122. /// greater than 0 and at most 1000.
  123. /// - pageToken A page token from a previous call to list.
  124. /// - Throws:
  125. /// - An error if the operation failed, for example if Storage was unreachable
  126. /// or the storage reference referenced an invalid path.
  127. /// - Returns:
  128. /// - completion A `Result` enum with either the list or an `Error`.
  129. func list(maxResults: Int64, pageToken: String) async throws -> StorageListResult {
  130. typealias ListContinuation = CheckedContinuation<StorageListResult, Error>
  131. return try await withCheckedThrowingContinuation { (continuation: ListContinuation) in
  132. self.list(maxResults: maxResults, pageToken: pageToken) { result in
  133. continuation.resume(with: result)
  134. }
  135. }
  136. }
  137. }
  138. #endif