mirror of https://github.com/kiwix/libkiwix.git
Move the download method from the downloader to networkTools.
The download method is a simple method to download content. It use curl to download the content instead of aria.
This commit is contained in:
parent
a73ef23f6e
commit
ad69fdd8c0
|
@ -44,6 +44,7 @@ namespace kiwix
|
||||||
{
|
{
|
||||||
std::map<std::string, std::string> getNetworkInterfaces();
|
std::map<std::string, std::string> getNetworkInterfaces();
|
||||||
std::string getBestPublicIp();
|
std::string getBestPublicIp();
|
||||||
|
std::string download(const std::string& url);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -89,14 +89,6 @@ class Downloader
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
/**
|
|
||||||
* Download a content.
|
|
||||||
*
|
|
||||||
* @param url the url to download
|
|
||||||
* @return the content downloaded.
|
|
||||||
*/
|
|
||||||
DownloadedFile download(const std::string& url);
|
|
||||||
|
|
||||||
Download* startDownload(const std::string& uri);
|
Download* startDownload(const std::string& uri);
|
||||||
Download* getDownload(const std::string& did);
|
Download* getDownload(const std::string& did);
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,9 @@
|
||||||
|
|
||||||
#include <common/networkTools.h>
|
#include <common/networkTools.h>
|
||||||
|
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
std::map<std::string, std::string> kiwix::getNetworkInterfaces()
|
std::map<std::string, std::string> kiwix::getNetworkInterfaces()
|
||||||
|
@ -160,3 +163,32 @@ std::string kiwix::getBestPublicIp()
|
||||||
|
|
||||||
return "127.0.0.1";
|
return "127.0.0.1";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t write_callback_to_iss(char* ptr, size_t size, size_t nmemb, void* userdata)
|
||||||
|
{
|
||||||
|
auto str = static_cast<std::stringstream*>(userdata);
|
||||||
|
str->write(ptr, nmemb);
|
||||||
|
return nmemb;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string kiwix::download(const std::string& url) {
|
||||||
|
auto curl = curl_easy_init();
|
||||||
|
std::stringstream ss;
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_PORT, 80);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_callback_to_iss);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ss);
|
||||||
|
auto res = curl_easy_perform(curl);
|
||||||
|
if (res != CURLE_OK) {
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
|
throw std::runtime_error("Cannot perform request");
|
||||||
|
}
|
||||||
|
long response_code;
|
||||||
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
|
if (response_code != 200) {
|
||||||
|
throw std::runtime_error("Invalid return code from server");
|
||||||
|
}
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
|
@ -122,33 +122,6 @@ std::vector<std::string> Downloader::getDownloadIds() {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
DownloadedFile Downloader::download(const std::string& url) {
|
|
||||||
DownloadedFile fileHandle;
|
|
||||||
try {
|
|
||||||
std::vector<std::string> status_key = {"status", "files"};
|
|
||||||
auto download = startDownload(url);
|
|
||||||
std::cerr << "gid is : " << download->getDid() << std::endl;
|
|
||||||
pugi::xml_document ret;
|
|
||||||
while(true) {
|
|
||||||
download->updateStatus();
|
|
||||||
std::cerr << "Status is " << download->getStatus() << std::endl;
|
|
||||||
if (download->getStatus() == Download::COMPLETE) {
|
|
||||||
fileHandle.success = true;
|
|
||||||
fileHandle.path = download->getPath();
|
|
||||||
std::cerr << "FilePath is " << fileHandle.path << std::endl;
|
|
||||||
} else if (download->getStatus() == Download::ERROR) {
|
|
||||||
fileHandle.success = false;
|
|
||||||
} else {
|
|
||||||
// [TODO] Be wise here.
|
|
||||||
std::this_thread::sleep_for(std::chrono::microseconds(100000));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (...) { std::cerr << "waautena " << std::endl; };
|
|
||||||
return fileHandle;
|
|
||||||
}
|
|
||||||
|
|
||||||
Download* Downloader::startDownload(const std::string& uri)
|
Download* Downloader::startDownload(const std::string& uri)
|
||||||
{
|
{
|
||||||
for (auto& p: m_knownDownloads) {
|
for (auto& p: m_knownDownloads) {
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "manager.h"
|
#include "manager.h"
|
||||||
#include "downloader.h"
|
#include "common/networkTools.h"
|
||||||
|
|
||||||
#include <pugixml.hpp>
|
#include <pugixml.hpp>
|
||||||
|
|
||||||
|
@ -108,13 +108,10 @@ bool Manager::parseOpdsDom(const pugi::xml_document& doc, const std::string& url
|
||||||
|
|
||||||
if (rel == "http://opds-spec.org/image/thumbnail") {
|
if (rel == "http://opds-spec.org/image/thumbnail") {
|
||||||
auto faviconUrl = urlHost + linkNode.attribute("href").value();
|
auto faviconUrl = urlHost + linkNode.attribute("href").value();
|
||||||
Downloader downloader;
|
try {
|
||||||
auto fileHandle = downloader.download(faviconUrl);
|
book.setFavicon(download(faviconUrl));
|
||||||
if (fileHandle.success) {
|
|
||||||
auto content = getFileContent(fileHandle.path);
|
|
||||||
book.setFavicon(content);
|
|
||||||
book.setFaviconMimeType(linkNode.attribute("type").value());
|
book.setFaviconMimeType(linkNode.attribute("type").value());
|
||||||
} else {
|
} catch (...) {
|
||||||
std::cerr << "Cannot get favicon content from " << faviconUrl << std::endl;
|
std::cerr << "Cannot get favicon content from " << faviconUrl << std::endl;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in New Issue