From 9fe81e9bce76786130a9e6bae470f7488aac1fd1 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 27 Feb 2024 19:13:08 +0400 Subject: [PATCH] Introduced downloadCanBeReused() helper The dubious logic of when an existing download can be reused by Downloader::startDownload() is preserved. --- src/downloader.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/downloader.cpp b/src/downloader.cpp index d5a8adf7e..44de4d40d 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -166,13 +166,27 @@ std::vector Downloader::getDownloadIds() const { return ret; } +namespace +{ + +bool downloadCanBeReused(const Download& d, + const std::string& uri, + const Downloader::Options& /*options*/) +{ + const auto& uris = d.getUris(); + const bool sameURI = std::find(uris.begin(), uris.end(), uri) != uris.end(); + + return sameURI; +} + +} // unnamed namespace + std::shared_ptr Downloader::startDownload(const std::string& uri, const Options& options) { std::unique_lock lock(m_lock); for (auto& p: m_knownDownloads) { auto& d = p.second; - auto& uris = d->getUris(); - if (std::find(uris.begin(), uris.end(), uri) != uris.end()) + if ( downloadCanBeReused(*d, uri, options) ) return d; } std::vector uris = {uri};