#include #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 = nullptr; do { delete out; out = new std::fstream(filename, std::ios::out | std::ios::binary); } while (!out->is_open()); 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; do res = cpr::Get(cpr::Url{url}); while (res.status_code != 200); std::fstream* file = this->open_file(filename); file->write(res.text.data(), res.downloaded_bytes); // can't use << for binary data this->close_file(file); }); download_queue.push_back(download); return download; }