#include #include #include "DownloadManager.h" DownloadManager::DownloadManager() { } DownloadManager::~DownloadManager() { for (std::thread* download : download_queue) { download->join(); delete download; download = nullptr; } } std::fstream* DownloadManager::open_file(std::string filename) { while (this->file_queue.size() > this->max_files); std::fstream* out = new std::fstream(filename); this->file_queue.push_back(out); return out; } void DownloadManager::close_file(std::fstream* file_ptr) { file_ptr->close(); delete file_ptr; this->file_queue.erase(std::remove(this->file_queue.begin(), this->file_queue.end(), file_ptr)); } std::thread* DownloadManager::wget(std::string url, std::string filename) { std::thread* download = new std::thread([url, filename, this] { cpr::Response res = cpr::Get(cpr::Url{url}); std::fstream* file = this->open_file(filename); *file << res.text; this->close_file(file); }); download_queue.push_back(download); return download; }