|
|
@ -3,11 +3,56 @@ |
|
|
|
#include <memory>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "fetchfile.hpp"
|
|
|
|
#include <sstream>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
//#include <filesystem> // c++17
|
|
|
|
|
|
|
|
//namespace fs = std::filesystem;
|
|
|
|
namespace fs = boost::filesystem; |
|
|
|
|
|
|
|
|
|
|
|
using namespace std; |
|
|
|
|
|
|
|
Fetchfile::Fetchfile( string basedir ) { |
|
|
|
Fetchfile::basedir = basedir; |
|
|
|
// test if file exists
|
|
|
|
string filename = basedir + "fetch.txt"; |
|
|
|
fs::path p{ filename }; |
|
|
|
fs::file_status s = fs::status( p ); |
|
|
|
cout << "path "<< p.string() << endl; |
|
|
|
cout << "is file: "<< fs::is_regular_file( s) << endl; |
|
|
|
if (fs::is_regular_file( s)) { |
|
|
|
this->exist_fetchfile = true; |
|
|
|
// map entries
|
|
|
|
// from spec: A metadata element MUST consist of a label, a colon, and a value,
|
|
|
|
// each separated by optional whitespace
|
|
|
|
// Long values may be continued
|
|
|
|
// onto the next line by inserting a newline (LF), a carriage return
|
|
|
|
// (CR), or carriage return plus newline (CRLF) and indenting the next
|
|
|
|
// line with linear white space (spaces or tabs)
|
|
|
|
ifstream file; |
|
|
|
file.open( filename ); |
|
|
|
if (file.is_open()) { |
|
|
|
string line; |
|
|
|
while (getline(file, line)) { |
|
|
|
stringstream line_ss ( line ); |
|
|
|
string url; |
|
|
|
string length; |
|
|
|
string subfile; |
|
|
|
line_ss >> url; |
|
|
|
line_ss >> length; |
|
|
|
line_ss >> subfile; |
|
|
|
fetch_t entry; |
|
|
|
entry.url = url; |
|
|
|
entry.size = stoi( length ); |
|
|
|
entry.filename = subfile; |
|
|
|
this->entries.push_back( entry ); |
|
|
|
} |
|
|
|
file.close(); |
|
|
|
} |
|
|
|
} else { // no file
|
|
|
|
this->exist_fetchfile = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
bool Fetchfile::fetch( fetch_t fetch ) { |
|
|
@ -15,7 +60,7 @@ bool Fetchfile::fetch( fetch_t fetch ) { |
|
|
|
CURLcode result; |
|
|
|
curl = curl_easy_init(); |
|
|
|
if (curl) { |
|
|
|
FILE *fp; |
|
|
|
FILE *fp; |
|
|
|
fp = fopen( fetch.filename.c_str(), "wb"); |
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, fetch.url.c_str()); |
|
|
|
// FIXME: curl needs a write function if WIN32,
|
|
|
@ -26,18 +71,59 @@ bool Fetchfile::fetch( fetch_t fetch ) { |
|
|
|
curl_easy_cleanup(curl); |
|
|
|
fclose(fp); |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
bool Fetchfile::has_fetchfile() { |
|
|
|
return this->exist_fetchfile; |
|
|
|
} |
|
|
|
|
|
|
|
list<fetch_t> Fetchfile::get_entries() { |
|
|
|
return this->entries; |
|
|
|
} |
|
|
|
|
|
|
|
bool Fetchfile::fetch_all_entries() { |
|
|
|
bool is_successful = true; |
|
|
|
for (list<fetch_t>::iterator it=this->entries.begin(); it!=this->entries.end(); ++it) { |
|
|
|
bool result = this->fetch( *it ); |
|
|
|
if (false == result) { |
|
|
|
is_successful = false; |
|
|
|
} |
|
|
|
} |
|
|
|
return is_successful; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool Fetchfile::validate() { |
|
|
|
return true; |
|
|
|
bool is_valid = true; |
|
|
|
if (this->has_fetchfile()) { |
|
|
|
// TODO:
|
|
|
|
} |
|
|
|
return is_valid; |
|
|
|
} |
|
|
|
|
|
|
|
bool Fetchfile::store( string basedir ) { |
|
|
|
return false; |
|
|
|
fs::path p{ basedir }; |
|
|
|
fs::file_status s = fs::status( p ); |
|
|
|
if (! fs::is_directory( s)) { |
|
|
|
Fetchfile::log << "directory '" << basedir << "' does not exist" << endl; |
|
|
|
return false; |
|
|
|
} |
|
|
|
string filename = basedir + "fetch.txt"; |
|
|
|
|
|
|
|
ofstream fetchfile_txt_file; |
|
|
|
fetchfile_txt_file.open( filename ); |
|
|
|
if (fetchfile_txt_file.is_open()) { |
|
|
|
for (list<fetch_t>::iterator it=this->entries.begin(); it!=this->entries.end(); ++it) { |
|
|
|
fetchfile_txt_file << it->url << " " << it->size << " " << it->filename << endl; |
|
|
|
} |
|
|
|
fetchfile_txt_file.close(); |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
void Fetchfile::get_logstream( stringstream & log ) { |
|
|
|
log << this->log.rdbuf(); |
|
|
|
} |
|
|
|