Do not always download the favicon of a book. Download as needed.

When parsing a opds feed, the favicon is a url, not a dataurl.
If we download the favicon all the times, it may take a lot of time to
parse the feed.

We store the url and download the favicon only when needed (when displayed)
This commit is contained in:
Matthieu Gautier 2018-10-24 11:56:05 +02:00
parent c20ae18bff
commit c6206edfb4
3 changed files with 22 additions and 22 deletions

View File

@ -45,7 +45,7 @@ class Book
bool update(const Book& other); bool update(const Book& other);
void update(const Reader& reader); void update(const Reader& reader);
void updateFromXml(const pugi::xml_node& node, const std::string& baseDir); void updateFromXml(const pugi::xml_node& node, const std::string& baseDir);
void updateFromOpds(const pugi::xml_node& node); void updateFromOpds(const pugi::xml_node& node, const std::string& urlHost);
std::string getHumanReadableIdFromPath(); std::string getHumanReadableIdFromPath();
bool readOnly() const { return m_readOnly; } bool readOnly() const { return m_readOnly; }
@ -67,7 +67,7 @@ class Book
const uint64_t& getArticleCount() const { return m_articleCount; } const uint64_t& getArticleCount() const { return m_articleCount; }
const uint64_t& getMediaCount() const { return m_mediaCount; } const uint64_t& getMediaCount() const { return m_mediaCount; }
const uint64_t& getSize() const { return m_size; } const uint64_t& getSize() const { return m_size; }
const std::string& getFavicon() const { return m_favicon; } const std::string& getFavicon() const;
const std::string& getFaviconMimeType() const { return m_faviconMimeType; } const std::string& getFaviconMimeType() const { return m_faviconMimeType; }
const std::string& getDownloadId() const { return m_downloadId; } const std::string& getDownloadId() const { return m_downloadId; }
@ -115,7 +115,8 @@ class Book
uint64_t m_mediaCount; uint64_t m_mediaCount;
bool m_readOnly; bool m_readOnly;
uint64_t m_size; uint64_t m_size;
std::string m_favicon; mutable std::string m_favicon;
std::string m_faviconUrl;
std::string m_faviconMimeType; std::string m_faviconMimeType;
}; };

View File

@ -22,6 +22,7 @@
#include "common/base64.h" #include "common/base64.h"
#include "common/regexTools.h" #include "common/regexTools.h"
#include "common/networkTools.h"
#include <pugixml.hpp> #include <pugixml.hpp>
@ -131,7 +132,7 @@ void Book::updateFromXml(const pugi::xml_node& node, const std::string& baseDir)
#define VALUE(name) node.child(name).child_value() #define VALUE(name) node.child(name).child_value()
void Book::updateFromOpds(const pugi::xml_node& node) void Book::updateFromOpds(const pugi::xml_node& node, const std::string& urlHost)
{ {
m_id = VALUE("id"); m_id = VALUE("id");
if (!m_id.compare(0, 9, "urn:uuid:")) { if (!m_id.compare(0, 9, "urn:uuid:")) {
@ -149,7 +150,10 @@ void Book::updateFromOpds(const pugi::xml_node& node)
if (rel == "http://opds-spec.org/acquisition/open-access") { if (rel == "http://opds-spec.org/acquisition/open-access") {
m_url = linkNode.attribute("href").value(); m_url = linkNode.attribute("href").value();
m_size = strtoull(linkNode.attribute("length").value(), 0, 0); m_size = strtoull(linkNode.attribute("length").value(), 0, 0);
break; }
if (rel == "http://opds-spec.org/image/thumbnail") {
m_faviconUrl = urlHost + linkNode.attribute("href").value();
m_faviconMimeType = linkNode.attribute("type").value();
} }
} }
@ -189,4 +193,15 @@ void Book::setIndexPath(const std::string& indexPath)
: indexPath; : indexPath;
} }
const std::string& Book::getFavicon() const {
if (m_favicon.empty() && !m_faviconUrl.empty()) {
try {
m_favicon = download(m_faviconUrl);
} catch(...) {
std::cerr << "Cannot download favicon from " << m_faviconUrl;
}
}
return m_favicon;
}
} }

View File

@ -18,7 +18,6 @@
*/ */
#include "manager.h" #include "manager.h"
#include "common/networkTools.h"
#include <pugixml.hpp> #include <pugixml.hpp>
@ -101,22 +100,7 @@ bool Manager::parseOpdsDom(const pugi::xml_document& doc, const std::string& url
kiwix::Book book; kiwix::Book book;
book.setReadOnly(false); book.setReadOnly(false);
book.updateFromOpds(entryNode); book.updateFromOpds(entryNode, urlHost);
for(pugi::xml_node linkNode = entryNode.child("link"); linkNode;
linkNode = linkNode.next_sibling("link")) {
std::string rel = linkNode.attribute("rel").value();
if (rel == "http://opds-spec.org/image/thumbnail") {
auto faviconUrl = urlHost + linkNode.attribute("href").value();
try {
book.setFavicon(download(faviconUrl));
book.setFaviconMimeType(linkNode.attribute("type").value());
} catch (...) {
std::cerr << "Cannot get favicon content from " << faviconUrl << std::endl;
}
break;
}
}
/* Update the book properties with the new importer */ /* Update the book properties with the new importer */
manipulator->addBookToLibrary(book); manipulator->addBookToLibrary(book);