Introduced downloadCanBeReused() helper

The dubious logic of when an existing download can be reused by
Downloader::startDownload() is preserved.
This commit is contained in:
Veloman Yunkan 2024-02-27 19:13:08 +04:00
parent 4ab6215046
commit 9fe81e9bce
1 changed files with 16 additions and 2 deletions

View File

@ -166,13 +166,27 @@ std::vector<std::string> Downloader::getDownloadIds() const {
return ret; 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<Download> Downloader::startDownload(const std::string& uri, const Options& options) std::shared_ptr<Download> Downloader::startDownload(const std::string& uri, const Options& options)
{ {
std::unique_lock<std::mutex> lock(m_lock); std::unique_lock<std::mutex> lock(m_lock);
for (auto& p: m_knownDownloads) { for (auto& p: m_knownDownloads) {
auto& d = p.second; auto& d = p.second;
auto& uris = d->getUris(); if ( downloadCanBeReused(*d, uri, options) )
if (std::find(uris.begin(), uris.end(), uri) != uris.end())
return d; return d;
} }
std::vector<std::string> uris = {uri}; std::vector<std::string> uris = {uri};