|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | + |
| 3 | +@_exported |
| 4 | +import firebase |
| 5 | +@_spi(FirebaseInternal) |
| 6 | +import FirebaseCore |
| 7 | + |
| 8 | +import CxxShim |
| 9 | +import Foundation |
| 10 | + |
| 11 | +public class StorageReference { |
| 12 | + let impl: firebase.storage.StorageReference |
| 13 | + |
| 14 | + init(_ impl: firebase.storage.StorageReference) { |
| 15 | + self.impl = impl |
| 16 | + } |
| 17 | + |
| 18 | + public func child(_ path: String) -> StorageReference { |
| 19 | + .init(impl.Child(path)) |
| 20 | + } |
| 21 | + |
| 22 | + public func downloadURL(completion: @escaping (URL?, Error?) -> Void) { |
| 23 | + downloadURLImpl() { result, error in |
| 24 | + DispatchQueue.main.async { |
| 25 | + completion(result, error) |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public func downloadURL() async throws -> URL { |
| 31 | + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<URL, any Error>) in |
| 32 | + downloadURLImpl() { result, error in |
| 33 | + if let error { |
| 34 | + continuation.resume(throwing: error) |
| 35 | + } else { |
| 36 | + continuation.resume(returning: result!) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private func downloadURLImpl(completion: @escaping (URL?, Error?) -> Void) { |
| 43 | + let future = swift_firebase.swift_cxx_shims.firebase.storage.storage_reference_get_download_url(impl) |
| 44 | + future.setCompletion({ |
| 45 | + let (result, error) = future.resultAndError { StorageErrorCode($0) } |
| 46 | + completion(result.flatMap { .init(string: String($0)) }, error) |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + public func putDataAsync( |
| 51 | + _ uploadData: Data, |
| 52 | + metadata: StorageMetadata? = nil, |
| 53 | + onProgress: ((Progress?) -> Void)? = nil |
| 54 | + ) async throws -> StorageMetadata { |
| 55 | + // TODO(PRENG-63978): Add support for `onProgress` callback. |
| 56 | + assert(onProgress == nil, "Missing support for non-nil onProgress") |
| 57 | + let controller = ControllerRef() |
| 58 | + return try await withTaskCancellationHandler { |
| 59 | + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<StorageMetadata, any Error>) in |
| 60 | + let future = uploadData.withUnsafeBytes { ptr in |
| 61 | + let bytes = ptr.baseAddress!.assumingMemoryBound(to: UInt8.self) |
| 62 | + let numBytes = uploadData.count |
| 63 | + if let metadata { |
| 64 | + return swift_firebase.swift_cxx_shims.firebase.storage.storage_reference_put_bytes( |
| 65 | + self.impl, bytes, numBytes, metadata.impl, &controller.impl |
| 66 | + ) |
| 67 | + } else { |
| 68 | + return swift_firebase.swift_cxx_shims.firebase.storage.storage_reference_put_bytes( |
| 69 | + self.impl, bytes, numBytes, &controller.impl |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | + future.setCompletion({ |
| 74 | + let (result, error) = future.resultAndError { StorageErrorCode($0) } |
| 75 | + if let error { |
| 76 | + continuation.resume(throwing: error) |
| 77 | + } else { |
| 78 | + continuation.resume(returning: result.map { .init($0) } ?? .init()) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | + } onCancel: { |
| 83 | + controller.impl.Cancel() |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// The underlying `firebase.storage.Controller` type is thread-safe. |
| 89 | +private class ControllerRef: @unchecked Sendable { |
| 90 | + var impl: firebase.storage.Controller = .init() |
| 91 | +} |
0 commit comments