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:
parent
1c15282114
commit
ef402e71e8
|
@ -13,7 +13,7 @@ let package = Package(
|
||||||
name: "carton",
|
name: "carton",
|
||||||
platforms: [.macOS(.v10_15)],
|
platforms: [.macOS(.v10_15)],
|
||||||
dependencies: [
|
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(
|
.package(
|
||||||
url: "https://github.com/apple/swift-argument-parser.git",
|
url: "https://github.com/apple/swift-argument-parser.git",
|
||||||
.upToNextMinor(from: "0.3.0")
|
.upToNextMinor(from: "0.3.0")
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -124,8 +124,8 @@ extension ToolchainSystem {
|
||||||
},
|
},
|
||||||
reportProgress: {
|
reportProgress: {
|
||||||
subject.send(.init(
|
subject.send(.init(
|
||||||
step: $1,
|
step: $0.receivedBytes,
|
||||||
total: $0 ?? expectedArchiveSize,
|
total: $0.totalBytes ?? expectedArchiveSize,
|
||||||
text: "saving to \(path)"
|
text: "saving to \(path)"
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue