AsyncAwait.swift 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 FirebaseStorage
  15. #if compiler(>=5.5) && canImport(_Concurrency)
  16. @available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
  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. typealias DataContinuation = CheckedContinuation<Data, Error>
  29. return try await withCheckedThrowingContinuation { (continuation: DataContinuation) in
  30. // TODO: Use task to handle progress and cancellation.
  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. /// - Returns: StorageMetadata with additional information about the object being uploaded.
  45. func putDataAsync(_ uploadData: Data,
  46. metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
  47. typealias MetadataContinuation = CheckedContinuation<StorageMetadata, Error>
  48. return try await withCheckedThrowingContinuation { (continuation: MetadataContinuation) in
  49. // TODO: Use task to handle progress and cancellation.
  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. ///
  57. /// - Parameters:
  58. /// - url: A URL representing the system file path of the object to be uploaded.
  59. /// - metadata: Optional StorageMetadata containing additional information (MIME type, etc.)
  60. /// about the object being uploaded.
  61. /// - Returns: StorageMetadata with additional information about the object being uploaded.
  62. func putFileAsync(from url: URL,
  63. metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
  64. typealias MetadataContinuation = CheckedContinuation<StorageMetadata, Error>
  65. return try await withCheckedThrowingContinuation { (continuation: MetadataContinuation) in
  66. // TODO: Use task to handle progress and cancellation.
  67. _ = self.putFile(from: url, metadata: metadata) { result in
  68. continuation.resume(with: result)
  69. }
  70. }
  71. }
  72. /// Asynchronously downloads the object at the current path to a specified system filepath.
  73. ///
  74. /// - Parameters:
  75. /// - fileUrl: A URL representing the system file path of the object to be uploaded.
  76. /// - Returns: URL pointing to the file path of the downloaded file.
  77. func writeAsync(toFile fileURL: URL) async throws -> URL {
  78. typealias URLContinuation = CheckedContinuation<URL, Error>
  79. return try await withCheckedThrowingContinuation { (continuation: URLContinuation) in
  80. // TODO: Use task to handle progress and cancellation.
  81. _ = self.write(toFile: fileURL) { result in
  82. continuation.resume(with: result)
  83. }
  84. }
  85. }
  86. }
  87. #endif