Add GitHub update checker and installer
This commit is contained in:
@@ -45,7 +45,41 @@ public class HTTPClient {
|
||||
}
|
||||
}
|
||||
|
||||
public func writeTo(_ url: String?, path: String, progress: ((Int64, Int64) -> Void)? = nil) throws {
|
||||
#if DEBUG
|
||||
precondition(!Thread.isMainThread, "HTTPClient.writeTo(...) must not be called on the main thread")
|
||||
#endif
|
||||
let request = client.newRequest()!
|
||||
request.setUserAgent(HTTPClient.userAgent)
|
||||
try request.setURL(url)
|
||||
let response = try request.execute()
|
||||
if let progress {
|
||||
let handler = WriteToProgressHandler(progress)
|
||||
try response.writeTo(withProgress: path, handler: handler)
|
||||
} else {
|
||||
try response.write(to: path)
|
||||
}
|
||||
}
|
||||
|
||||
public static func writeToAsync(_ url: String?, path: String, progress: ((Int64, Int64) -> Void)? = nil) async throws {
|
||||
try await BlockingIO.run {
|
||||
try HTTPClient().writeTo(url, path: path, progress: progress)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
|
||||
private class WriteToProgressHandler: NSObject, LibboxHTTPResponseWriteToProgressHandlerProtocol {
|
||||
private let handler: (Int64, Int64) -> Void
|
||||
|
||||
init(_ handler: @escaping (Int64, Int64) -> Void) {
|
||||
self.handler = handler
|
||||
}
|
||||
|
||||
func update(_ progress: Int64, total: Int64) {
|
||||
handler(progress, total)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user