forked from compnerd/swift-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
@_exported | ||
import firebase | ||
@_spi(FirebaseInternal) | ||
import FirebaseCore | ||
|
||
import CxxShim | ||
|
||
public class Storage { | ||
let impl: swift_firebase.swift_cxx_shims.firebase.storage.StorageRef | ||
|
||
init(_ impl: swift_firebase.swift_cxx_shims.firebase.storage.StorageRef) { | ||
self.impl = impl | ||
} | ||
|
||
public static func storage(app: FirebaseApp) -> Storage { | ||
let instance = swift_firebase.swift_cxx_shims.firebase.storage.storage_get_instance(app) | ||
guard swift_firebase.swift_cxx_shims.firebase.storage.storage_is_valid(instance) else { | ||
fatalError("Invalid Storage Instance") | ||
} | ||
return .init(instance) | ||
} | ||
|
||
public func reference(withPath path: String) -> StorageReference { | ||
.init(swift_firebase.swift_cxx_shims.firebase.storage.storage_get_reference(impl, path)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
@_exported | ||
import firebase | ||
@_spi(FirebaseInternal) | ||
import FirebaseCore | ||
|
||
public struct StorageErrorCode: Error { | ||
public let rawValue: Int | ||
public let localizedDescription: String | ||
|
||
internal init(_ params: (code: Int32, message: String)) { | ||
self.rawValue = Int(params.code) | ||
localizedDescription = params.message | ||
} | ||
|
||
private init(_ error: firebase.storage.Error) { | ||
self.init(rawValue: Int(error.rawValue)) | ||
} | ||
} | ||
|
||
extension StorageErrorCode: RawRepresentable { | ||
public typealias RawValue = Int | ||
|
||
public init(rawValue: Int) { | ||
self.rawValue = rawValue | ||
localizedDescription = "\(rawValue)" | ||
} | ||
} | ||
|
||
extension StorageErrorCode { | ||
init(_ error: firebase.storage.Error, errorMessage: String?) { | ||
self.init((code: error.rawValue, message: errorMessage ?? "\(error.rawValue)")) | ||
} | ||
|
||
init?(_ error: firebase.storage.Error?, errorMessage: UnsafePointer<CChar>?) { | ||
guard let actualError = error, actualError.rawValue != 0 else { return nil } | ||
var errorMessageString: String? | ||
if let errorMessage { | ||
errorMessageString = .init(cString: errorMessage) | ||
} | ||
self.init(actualError, errorMessage: errorMessageString) | ||
} | ||
} | ||
|
||
extension StorageErrorCode { | ||
public static var none: Self { .init(firebase.storage.kErrorNone) } | ||
public static var unknown: Self { .init(firebase.storage.kErrorUnknown) } | ||
public static var objectNotFound: Self { .init(firebase.storage.kErrorObjectNotFound) } | ||
public static var bucketNotFound: Self { .init(firebase.storage.kErrorBucketNotFound) } | ||
public static var projectNotFound: Self { .init(firebase.storage.kErrorProjectNotFound) } | ||
public static var quotaExceeded: Self { .init(firebase.storage.kErrorQuotaExceeded) } | ||
public static var unauthenticated: Self { .init(firebase.storage.kErrorUnauthenticated) } | ||
public static var unauthorized: Self { .init(firebase.storage.kErrorUnauthorized) } | ||
public static var retryLimitExceeded: Self { .init(firebase.storage.kErrorRetryLimitExceeded) } | ||
public static var nonMatchingChecksum: Self { .init(firebase.storage.kErrorNonMatchingChecksum) } | ||
public static var downloadSizeExceeded: Self { .init(firebase.storage.kErrorDownloadSizeExceeded) } | ||
public static var cancelled: Self { .init(firebase.storage.kErrorCancelled) } | ||
} | ||
|
||
extension StorageErrorCode: Equatable {} | ||
|
||
extension StorageErrorCode { | ||
// The Obj C API provides this type as well, so provide it here for consistency. | ||
public typealias Code = StorageErrorCode | ||
|
||
// This allows us to re-expose self as a code similarly | ||
// to what the Firebase SDK does when it creates the | ||
// underlying NSErrors on iOS/macOS. | ||
public var code: Code { | ||
return self | ||
} | ||
|
||
public init(_ code: Code) { | ||
self.init(rawValue: code.rawValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
@_exported | ||
import firebase | ||
@_spi(FirebaseInternal) | ||
import FirebaseCore | ||
|
||
import CxxShim | ||
import Foundation | ||
|
||
public class StorageReference { | ||
let impl: firebase.storage.StorageReference | ||
|
||
init(_ impl: firebase.storage.StorageReference) { | ||
self.impl = impl | ||
} | ||
|
||
public func child(_ path: String) -> StorageReference { | ||
.init(impl.Child(path)) | ||
} | ||
|
||
public func downloadURL(completion: @escaping (URL?, Error?) -> Void) { | ||
downloadURLImpl() { result, error in | ||
DispatchQueue.main.async { | ||
completion(result, error) | ||
} | ||
} | ||
} | ||
|
||
public func downloadURL() async throws -> URL { | ||
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<URL, any Error>) in | ||
downloadURLImpl() { result, error in | ||
if let error { | ||
continuation.resume(throwing: error) | ||
} else { | ||
continuation.resume(returning: result!) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func downloadURLImpl(completion: @escaping (URL?, Error?) -> Void) { | ||
let future = swift_firebase.swift_cxx_shims.firebase.storage.storage_reference_get_download_url(impl) | ||
future.setCompletion({ | ||
let (result, error) = future.resultAndError { StorageErrorCode($0) } | ||
completion(result.flatMap { .init(string: String($0)) }, error) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters