AsyncAwait.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /// - Returns: Data object.
  27. func data(maxSize: Int64) async throws -> Data {
  28. return try await withCheckedThrowingContinuation { continuation in
  29. // TODO: Use task to handle progress and cancellation.
  30. _ = self.getData(maxSize: maxSize) { result in
  31. continuation.resume(with: result)
  32. }
  33. }
  34. }
  35. /// Asynchronously uploads data to the currently specified StorageReference.
  36. /// This is not recommended for large files, and one should instead upload a file from disk
  37. /// from the Firebase Console.
  38. ///
  39. /// - Parameters:
  40. /// - uploadData: The Data to upload.
  41. /// - metadata: Optional StorageMetadata containing additional information (MIME type, etc.)
  42. /// about the object being uploaded.
  43. /// - Returns: StorageMetadata with additional information about the object being uploaded.
  44. func putDataAsync(_ uploadData: Data,
  45. metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
  46. return try await withCheckedThrowingContinuation { continuation in
  47. // TODO: Use task to handle progress and cancellation.
  48. _ = self.putData(uploadData, metadata: metadata) { result in
  49. continuation.resume(with: result)
  50. }
  51. }
  52. }
  53. /// Asynchronously uploads a file to the currently specified StorageReference.
  54. ///
  55. /// - Parameters:
  56. /// - url: A URL representing the system file path of the object to be uploaded.
  57. /// - metadata: Optional StorageMetadata containing additional information (MIME type, etc.)
  58. /// about the object being uploaded.
  59. /// - Returns: StorageMetadata with additional information about the object being uploaded.
  60. func putFileAsync(from url: URL,
  61. metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
  62. return try await withCheckedThrowingContinuation { continuation in
  63. // TODO: Use task to handle progress and cancellation.
  64. _ = self.putFile(from: url, metadata: metadata) { result in
  65. continuation.resume(with: result)
  66. }
  67. }
  68. }
  69. /// Asynchronously downloads the object at the current path to a specified system filepath.
  70. ///
  71. /// - Parameters:
  72. /// - fileUrl: A URL representing the system file path of the object to be uploaded.
  73. /// - Returns: URL pointing to the file path of the downloaded file.
  74. func writeAsync(toFile fileURL: URL) async throws -> URL {
  75. return try await withCheckedThrowingContinuation { continuation in
  76. // TODO: Use task to handle progress and cancellation.
  77. _ = self.write(toFile: fileURL) { result in
  78. continuation.resume(with: result)
  79. }
  80. }
  81. }
  82. /// List up to `maxResults` items (files) and prefixes (folders) under this StorageReference.
  83. ///
  84. /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  85. /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  86. /// filtered.
  87. ///
  88. /// Only available for projects using Firebase Rules Version 2.
  89. ///
  90. /// - Parameters:
  91. /// - maxResults The maximum number of results to return in a single page. Must be
  92. /// greater than 0 and at most 1000.
  93. /// - Returns:
  94. /// - completion A `Result` enum with either the list or an `Error`.
  95. func list(maxResults: Int64) async throws -> StorageListResult {
  96. typealias ListContinuation = CheckedContinuation<StorageListResult, Error>
  97. return try await withCheckedThrowingContinuation { (continuation: ListContinuation) in
  98. self.list(maxResults: maxResults) { result in
  99. continuation.resume(with: result)
  100. }
  101. }
  102. }
  103. /// List up to `maxResults` items (files) and prefixes (folders) under this StorageReference.
  104. ///
  105. /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
  106. /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
  107. /// filtered.
  108. ///
  109. /// Only available for projects using Firebase Rules Version 2.
  110. ///
  111. /// - Parameters:
  112. /// - maxResults The maximum number of results to return in a single page. Must be
  113. /// greater than 0 and at most 1000.
  114. /// - pageToken A page token from a previous call to list.
  115. /// - Returns:
  116. /// - completion A `Result` enum with either the list or an `Error`.
  117. func list(maxResults: Int64, pageToken: String) async throws -> StorageListResult {
  118. typealias ListContinuation = CheckedContinuation<StorageListResult, Error>
  119. return try await withCheckedThrowingContinuation { (continuation: ListContinuation) in
  120. self.list(maxResults: maxResults, pageToken: pageToken) { result in
  121. continuation.resume(with: result)
  122. }
  123. }
  124. }
  125. }
  126. #endif