Only emit download progress updates every 100ms

This commit is contained in:
Dan Paulat 2024-03-26 22:48:52 -05:00
parent 3ab05a1654
commit 6448826d60

View file

@ -105,26 +105,52 @@ void DownloadManager::Download(
return; return;
} }
std::chrono::system_clock::time_point lastUpdated {};
cpr::cpr_off_t lastDownloadNow {};
cpr::cpr_off_t lastDownloadTotal {};
// Download file // Download file
cpr::Response response = cpr::Get( cpr::Response response =
cpr::Url {request->url()}, cpr::Get(cpr::Url {request->url()},
cpr::ProgressCallback( cpr::ProgressCallback(
[=](cpr::cpr_off_t downloadTotal, [&](cpr::cpr_off_t downloadTotal,
cpr::cpr_off_t downloadNow, cpr::cpr_off_t downloadNow,
cpr::cpr_off_t /* uploadTotal */, cpr::cpr_off_t /* uploadTotal */,
cpr::cpr_off_t /* uploadNow */, cpr::cpr_off_t /* uploadNow */,
std::intptr_t /* userdata */) std::intptr_t /* userdata */)
{ {
Q_EMIT request->ProgressUpdated(downloadNow, downloadTotal); using namespace std::chrono_literals;
return !request->IsCanceled();
}), std::chrono::system_clock::time_point now =
cpr::WriteCallback( std::chrono::system_clock::now();
[=, &ofs](std::string data, std::intptr_t /* userdata */)
{ // Only emit an update every 100ms
// Write file if ((now > lastUpdated + 100ms ||
ofs << data; downloadNow == downloadTotal) &&
return !request->IsCanceled(); (downloadNow != lastDownloadNow ||
})); downloadTotal != lastDownloadTotal))
{
logger_->trace("Downloaded: {} / {}",
downloadNow,
downloadTotal);
Q_EMIT request->ProgressUpdated(downloadNow,
downloadTotal);
lastUpdated = now;
lastDownloadNow = downloadNow;
lastDownloadTotal = downloadTotal;
}
return !request->IsCanceled();
}),
cpr::WriteCallback(
[&](std::string data, std::intptr_t /* userdata */)
{
// Write file
ofs << data;
return !request->IsCanceled();
}));
bool ofsGood = ofs.good(); bool ofsGood = ofs.good();
ofs.close(); ofs.close();
@ -138,14 +164,13 @@ void DownloadManager::Download(
if (request->IsCanceled()) if (request->IsCanceled())
{ {
logger_->info("Download request cancelled: \"{}\"", logger_->info("Download request cancelled: {}", request->url());
request->url());
reason = request::DownloadRequest::CompleteReason::Canceled; reason = request::DownloadRequest::CompleteReason::Canceled;
} }
else if (response.error.code != cpr::ErrorCode::OK) else if (response.error.code != cpr::ErrorCode::OK)
{ {
logger_->error("Error downloading file ({}): \"{}\"", logger_->error("Error downloading file ({}): {}",
response.error.message, response.error.message,
request->url()); request->url());
@ -153,8 +178,7 @@ void DownloadManager::Download(
} }
else if (!ofsGood) else if (!ofsGood)
{ {
logger_->error("File I/O error: \"{}\"", logger_->error("File I/O error: {}", destinationPath.string());
destinationPath.string());
reason = request::DownloadRequest::CompleteReason::IOError; reason = request::DownloadRequest::CompleteReason::IOError;
} }
@ -162,7 +186,7 @@ void DownloadManager::Download(
std::error_code error; std::error_code error;
if (!std::filesystem::remove(destinationPath, error)) if (!std::filesystem::remove(destinationPath, error))
{ {
logger_->error("Unable to remove destination file: \"{}\", {}", logger_->error("Unable to remove destination file: {}, {}",
destinationPath.string(), destinationPath.string(),
error.message()); error.message());
} }
@ -182,9 +206,8 @@ void DownloadManager::Download(
std::ios_base::in | std::ios_base::binary}; std::ios_base::in | std::ios_base::binary};
if (!is.is_open() || !is.good()) if (!is.is_open() || !is.good())
{ {
logger_->error( logger_->error("Unable to open destination file for reading: {}",
"Unable to open destination file for reading: \"{}\"", destinationPath.string());
destinationPath.string());
Q_EMIT request->RequestComplete( Q_EMIT request->RequestComplete(
request::DownloadRequest::CompleteReason::IOError); request::DownloadRequest::CompleteReason::IOError);
@ -196,7 +219,7 @@ void DownloadManager::Download(
std::vector<std::uint8_t> digest {}; std::vector<std::uint8_t> digest {};
if (!util::ComputeDigest(EVP_md5(), is, digest)) if (!util::ComputeDigest(EVP_md5(), is, digest))
{ {
logger_->error("Failed to compute MD5: \"{}\"", logger_->error("Failed to compute MD5: {}",
destinationPath.string()); destinationPath.string());
Q_EMIT request->RequestComplete( Q_EMIT request->RequestComplete(
@ -227,7 +250,7 @@ void DownloadManager::Download(
} }
} }
logger_->info("Download complete: \"{}\"", request->url()); logger_->info("Download complete: {}", request->url());
Q_EMIT request->RequestComplete( Q_EMIT request->RequestComplete(
request::DownloadRequest::CompleteReason::OK); request::DownloadRequest::CompleteReason::OK);
}); });