|
1 | | -#ifndef DOWNLOAD_H |
2 | | -#define DOWNLOAD_H |
| 1 | +#include "download.h" |
| 2 | +#include <iostream> |
| 3 | +#include <fstream> |
| 4 | +#include <curl/curl.h> |
| 5 | +#include <sys/stat.h> |
| 6 | +#include <sys/types.h> |
| 7 | +#ifdef _WIN32 |
| 8 | + #include <direct.h> |
| 9 | + #define MKDIR(dir) _mkdir(dir) |
| 10 | +#else |
| 11 | + #include <errno.h> |
| 12 | + #define MKDIR(dir) mkdir(dir, 0755) |
| 13 | +#endif |
3 | 14 |
|
4 | | -#include <string> |
| 15 | +// Helper: Create directory if it does not exist |
| 16 | +bool ensure_directory(const std::string& filepath) { |
| 17 | + size_t pos = filepath.find_last_of("/\\"); |
| 18 | + if (pos == std::string::npos) return true; // No directory part |
5 | 19 |
|
6 | | -bool download(const std::string& url, const std::string& outputPath); |
| 20 | + std::string dir = filepath.substr(0, pos); |
| 21 | + struct stat info; |
| 22 | + if (stat(dir.c_str(), &info) != 0) { |
| 23 | + // Directory does not exist, try to create it |
| 24 | + if (MKDIR(dir.c_str()) != 0) { |
| 25 | + std::cerr << "[download] Failed to create directory: " << dir << std::endl; |
| 26 | + return false; |
| 27 | + } |
| 28 | + } else if (!(info.st_mode & S_IFDIR)) { |
| 29 | + std::cerr << "[download] Path exists but is not a directory: " << dir << std::endl; |
| 30 | + return false; |
| 31 | + } |
| 32 | + return true; |
| 33 | +} |
7 | 34 |
|
8 | | -#endif |
| 35 | +// Callback for libcurl to write data to file |
| 36 | +size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream) { |
| 37 | + std::ofstream* out = static_cast<std::ofstream*>(stream); |
| 38 | + const size_t totalSize = size * nmemb; |
| 39 | + out->write(static_cast<char*>(ptr), totalSize); |
| 40 | + return totalSize; |
| 41 | +} |
| 42 | + |
| 43 | +Download::Download(const std::string &url, const std::string &path) |
| 44 | + : url(url), path(path) {} |
| 45 | + |
| 46 | +bool Download::download() const { |
| 47 | + if (!ensure_directory(path)) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + CURL* curl = curl_easy_init(); |
| 52 | + if (!curl) { |
| 53 | + std::cerr << "[curl] Initialization failed" << std::endl; |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + std::ofstream outFile(path, std::ios::binary); |
| 58 | + if (!outFile.is_open()) { |
| 59 | + std::cerr << "[download] Could not open file: " << path << std::endl; |
| 60 | + curl_easy_cleanup(curl); |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 65 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); |
| 66 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outFile); |
| 67 | + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects |
| 68 | + |
| 69 | + CURLcode res = curl_easy_perform(curl); |
| 70 | + |
| 71 | + curl_easy_cleanup(curl); |
| 72 | + outFile.close(); |
| 73 | + |
| 74 | + if (res != CURLE_OK) { |
| 75 | + std::cerr << "[curl] Download error: " << curl_easy_strerror(res) << std::endl; |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + std::cout << "[download] Successfully saved as: " << path << std::endl; |
| 80 | + return true; |
| 81 | +} |
0 commit comments