Use FileDownloadDelegate from the AHC package (#171)

`FileDownloadDelegate` was introduced in https://github.com/swift-server/async-http-client/pull/275, which almost directly mirrors the implementation in `carton`. Now that it's available in a released version of AHC, let's delete some code here and replace it with the version from that package.
This commit is contained in:
Max Desiatov 2020-12-01 11:05:15 +00:00 committed by GitHub
parent 1c15282114
commit ef402e71e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 84 deletions

View File

@ -13,7 +13,7 @@ let package = Package(
name: "carton",
platforms: [.macOS(.v10_15)],
dependencies: [
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.1.1"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.2.2"),
.package(
url: "https://github.com/apple/swift-argument-parser.git",
.upToNextMinor(from: "0.3.0")

View File

@ -1,81 +0,0 @@
// Copyright 2020 Carton contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import AsyncHTTPClient
import NIO
import NIOHTTP1
final class FileDownloadDelegate: HTTPClientResponseDelegate {
typealias Response = (totalBytes: Int?, receivedBytes: Int)
private var totalBytes: Int?
private var receivedBytes = 0
private let handle: NIOFileHandle
private let io: NonBlockingFileIO
private let reportHead: ((HTTPResponseHead) -> ())?
private let reportProgress: ((_ totalBytes: Int?, _ receivedBytes: Int) -> ())?
private var writeFuture: EventLoopFuture<()>?
init(
path: String,
pool: NIOThreadPool = NIOThreadPool(numberOfThreads: 1),
reportHead: ((HTTPResponseHead) -> ())? = nil,
reportProgress: ((_ totalBytes: Int?, _ receivedBytes: Int) -> ())? = nil
) throws {
handle = try NIOFileHandle(path: path, mode: .write, flags: .allowFileCreation())
pool.start()
io = NonBlockingFileIO(threadPool: pool)
self.reportHead = reportHead
self.reportProgress = reportProgress
}
func didReceiveHead(
task: HTTPClient.Task<Response>,
_ head: HTTPResponseHead
) -> EventLoopFuture<()> {
reportHead?(head)
if let totalBytesString = head.headers.first(name: "Content-Length"),
let totalBytes = Int(totalBytesString)
{
self.totalBytes = totalBytes
}
return task.eventLoop.makeSucceededFuture(())
}
func didReceiveBodyPart(
task: HTTPClient.Task<Response>,
_ buffer: ByteBuffer
) -> EventLoopFuture<()> {
receivedBytes += buffer.readableBytes
reportProgress?(totalBytes, receivedBytes)
let writeFuture = io.write(fileHandle: handle, buffer: buffer, eventLoop: task.eventLoop)
self.writeFuture = writeFuture
return writeFuture
}
func didFinishRequest(task: HTTPClient.Task<Response>) throws -> Response {
writeFuture?.whenComplete { [weak self] _ in
// swiftlint:disable:next force_try
try! self?.handle.close()
self?.writeFuture = nil
}
return (totalBytes, receivedBytes)
}
}

View File

@ -124,8 +124,8 @@ extension ToolchainSystem {
},
reportProgress: {
subject.send(.init(
step: $1,
total: $0 ?? expectedArchiveSize,
step: $0.receivedBytes,
total: $0.totalBytes ?? expectedArchiveSize,
text: "saving to \(path)"
))
}