From e5168d8b3d9397342fb23c62d10b80d37b093940 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 5 Jul 2021 23:50:54 +0400 Subject: [PATCH 1/2] Support for multiple illustrations in OPDS entry --- src/opds_dumper.cpp | 17 +++++++++++++++++ static/templates/catalog_entries.xml | 6 +++++- static/templates/catalog_v2_entries.xml | 6 +++++- test/server.cpp | 12 +++++++++--- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/opds_dumper.cpp b/src/opds_dumper.cpp index 440272c30..bfbe61c84 100644 --- a/src/opds_dumper.cpp +++ b/src/opds_dumper.cpp @@ -52,6 +52,22 @@ namespace typedef kainjow::mustache::data MustacheData; typedef kainjow::mustache::list BookData; +typedef kainjow::mustache::list IllustrationInfo; + +IllustrationInfo getBookIllustrationInfo(const Book& book) +{ + kainjow::mustache::list illustrations; + if ( book.isPathValid() ) { + for ( auto illustration_size : zim::Archive(book.getPath()).getIllustrationSizes() ) { + illustrations.push_back(kainjow::mustache::object{ + {"icon_width", to_string(illustration_size)}, + {"icon_height", to_string(illustration_size)}, + {"icon_scale", "1"}, + }); + } + } + return illustrations; +} BookData getBookData(const Library* library, const std::vector& bookIds) { @@ -78,6 +94,7 @@ BookData getBookData(const Library* library, const std::vector& boo {"publisher_name", book.getPublisher()}, {"url", bookUrl}, {"size", to_string(book.getSize())}, + {"icons", getBookIllustrationInfo(book)}, }); } diff --git a/static/templates/catalog_entries.xml b/static/templates/catalog_entries.xml index 32754d5b0..0a6a05dde 100644 --- a/static/templates/catalog_entries.xml +++ b/static/templates/catalog_entries.xml @@ -22,7 +22,11 @@ {{tags}} {{article_count}} {{media_count}} - /meta?name=favicon&content={{{content_id}}} + {{#icons}} + + {{/icons}} {{author_name}} diff --git a/static/templates/catalog_v2_entries.xml b/static/templates/catalog_v2_entries.xml index 05cbb5054..098744d86 100644 --- a/static/templates/catalog_v2_entries.xml +++ b/static/templates/catalog_v2_entries.xml @@ -34,7 +34,11 @@ {{tags}} {{article_count}} {{media_count}} - /meta?name=favicon&content={{{content_id}}} + {{#icons}} + + {{/icons}} {{author_name}} diff --git a/test/server.cpp b/test/server.cpp index f900cd938..fbe62cb0a 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -626,7 +626,9 @@ std::string maskVariableOPDSFeedData(std::string s) " unittest;wikipedia;_category:jazz;_pictures:no;_videos:no;_details:no;_ftindex:yes\n" \ " 284\n" \ " 2\n" \ - " /meta?name=favicon&content=zimfile\n" \ + " \n" \ " \n" \ " \n" \ " Wikipedia\n" \ @@ -650,7 +652,9 @@ std::string maskVariableOPDSFeedData(std::string s) " unittest;wikipedia;_category:wikipedia;_pictures:no;_videos:no;_details:no;_ftindex:yes\n" \ " 284\n" \ " 2\n" \ - " /meta?name=favicon&content=zimfile\n" \ + " \n" \ " \n" \ " \n" \ " Wikipedia\n" \ @@ -674,7 +678,9 @@ std::string maskVariableOPDSFeedData(std::string s) " unittest;wikipedia;_pictures:no;_videos:no;_details:no\n" \ " 284\n" \ " 2\n" \ - " /meta?name=favicon&content=zimfile\n" \ + " \n" \ " \n" \ " \n" \ " Wikipedia\n" \ From 452283cfe6db2f4bc2392a1623493a3d03bf846c Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 7 Jul 2021 23:41:46 +0400 Subject: [PATCH 2/2] Handling of /meta?name=Illustration_WxH@1 requests --- src/book.cpp | 2 +- src/reader.cpp | 2 +- src/server/internalServer.cpp | 14 +++++++++++++- src/tools/archiveTools.cpp | 4 ++-- src/tools/archiveTools.h | 2 +- test/server.cpp | 1 + 6 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/book.cpp b/src/book.cpp index da37ea042..020fa7f4c 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -104,7 +104,7 @@ void Book::update(const zim::Archive& archive) { m_mediaCount = getArchiveMediaCount(archive); m_size = static_cast(getArchiveFileSize(archive)) << 10; - getArchiveFavicon(archive, m_favicon, m_faviconMimeType); + getArchiveFavicon(archive, 48, m_favicon, m_faviconMimeType); } #define ATTR(name) node.attribute(name).value() diff --git a/src/reader.cpp b/src/reader.cpp index 4f0ed922f..2bbe58189 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -134,7 +134,7 @@ Entry Reader::getMainPage() const bool Reader::getFavicon(string& content, string& mimeType) const { - return kiwix::getArchiveFavicon(*zimArchive, content, mimeType); + return kiwix::getArchiveFavicon(*zimArchive, 48, content, mimeType); } string Reader::getZimFilePath() const diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 957b128f8..cd708a1f9 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -94,6 +94,16 @@ inline std::string normalizeRootUrl(std::string rootUrl) return rootUrl.empty() ? rootUrl : "/" + rootUrl; } +unsigned parseIllustration(const std::string& s) +{ + int nw(0), nh(0), nEnd(0); + long int w(-1), h(-1); + if ( sscanf(s.c_str(), "Illustration_%n%ldx%n%ld@1%n)", &nw, &w, &nh, &h, &nEnd) == 2 + && nEnd == (int)s.size() && !isspace(s[nw]) && !isspace(s[nh]) && w == h && w >= 0) { + return w; + } + return 0; +} } // unnamed namespace static IdNameMapper defaultNameMapper; @@ -408,7 +418,9 @@ std::unique_ptr InternalServer::handle_meta(const RequestContext& requ } else if (meta_name == "publisher") { content = getMetaPublisher(*archive); } else if (meta_name == "favicon") { - getArchiveFavicon(*archive, content, mimeType); + getArchiveFavicon(*archive, 48, content, mimeType); + } else if (const unsigned illustrationSize = parseIllustration(meta_name)) { + getArchiveFavicon(*archive, illustrationSize, content, mimeType); } else { return Response::build_404(*this, request, bookName, ""); } diff --git a/src/tools/archiveTools.cpp b/src/tools/archiveTools.cpp index ebd136557..5ec5bd9e9 100644 --- a/src/tools/archiveTools.cpp +++ b/src/tools/archiveTools.cpp @@ -97,10 +97,10 @@ std::string getArchiveId(const zim::Archive& archive) { return (std::string) archive.getUuid(); } -bool getArchiveFavicon(const zim::Archive& archive, +bool getArchiveFavicon(const zim::Archive& archive, unsigned size, std::string& content, std::string& mimeType){ try { - auto item = archive.getIllustrationItem(); + auto item = archive.getIllustrationItem(size); content = item.getData(); mimeType = item.getMimetype(); return true; diff --git a/src/tools/archiveTools.h b/src/tools/archiveTools.h index 456e749e7..cfcf1ced1 100644 --- a/src/tools/archiveTools.h +++ b/src/tools/archiveTools.h @@ -42,7 +42,7 @@ namespace kiwix std::string getMetaFlavour(const zim::Archive& archive); std::string getArchiveId(const zim::Archive& archive); - bool getArchiveFavicon(const zim::Archive& archive, + bool getArchiveFavicon(const zim::Archive& archive, unsigned size, std::string& content, std::string& mimeType); unsigned int getArchiveMediaCount(const zim::Archive& archive); diff --git a/test/server.cpp b/test/server.cpp index fbe62cb0a..d45e576e2 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -197,6 +197,7 @@ const ResourceCollection resources200Uncompressible{ { WITH_ETAG, "/meta?content=zimfile&name=creator" }, { WITH_ETAG, "/meta?content=zimfile&name=publisher" }, { WITH_ETAG, "/meta?content=zimfile&name=favicon" }, + { WITH_ETAG, "/meta?content=zimfile&name=Illustration_48x48@1" }, { WITH_ETAG, "/zimfile/I/m/Ray_Charles_classic_piano_pose.jpg" },