Skip to content

Commit

Permalink
Merge pull request #577 from kiwix/opds_multiple_icons
Browse files Browse the repository at this point in the history
Support for multiple illustrations in OPDS entry
  • Loading branch information
kelson42 authored Aug 5, 2021
2 parents b8aee8a + 452283c commit 6e26c5a
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void Book::update(const zim::Archive& archive) {
m_mediaCount = getArchiveMediaCount(archive);
m_size = static_cast<uint64_t>(getArchiveFileSize(archive)) << 10;

getArchiveFavicon(archive, m_favicon, m_faviconMimeType);
getArchiveFavicon(archive, 48, m_favicon, m_faviconMimeType);
}

#define ATTR(name) node.attribute(name).value()
Expand Down
17 changes: 17 additions & 0 deletions src/opds_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& bookIds)
{
Expand All @@ -78,6 +94,7 @@ BookData getBookData(const Library* library, const std::vector<std::string>& boo
{"publisher_name", book.getPublisher()},
{"url", bookUrl},
{"size", to_string(book.getSize())},
{"icons", getBookIllustrationInfo(book)},
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -408,7 +418,9 @@ std::unique_ptr<Response> 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, "");
}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/archiveTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/archiveTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion static/templates/catalog_entries.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
<tags>{{tags}}</tags>
<articleCount>{{article_count}}</articleCount>
<mediaCount>{{media_count}}</mediaCount>
<icon>/meta?name=favicon&amp;content={{{content_id}}}</icon>
{{#icons}}
<link rel="http://opds-spec.org/image/thumbnail"
href="/meta?name=Illustration_{{icon_width}}x{{icon_height}}@{{icon_scale}}&amp;content={{{content_id}}}"
type="image/png;width={{icon_width}};height={{icon_height}};scale={{icon_scale}}"/>
{{/icons}}
<link type="text/html" href="/{{{content_id}}}" />
<author>
<name>{{author_name}}</name>
Expand Down
6 changes: 5 additions & 1 deletion static/templates/catalog_v2_entries.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
<tags>{{tags}}</tags>
<articleCount>{{article_count}}</articleCount>
<mediaCount>{{media_count}}</mediaCount>
<icon>/meta?name=favicon&amp;content={{{content_id}}}</icon>
{{#icons}}
<link rel="http://opds-spec.org/image/thumbnail"
href="/meta?name=Illustration_{{icon_width}}x{{icon_height}}@{{icon_scale}}&amp;content={{{content_id}}}"
type="image/png;width={{icon_width}};height={{icon_height}};scale={{icon_scale}}"/>
{{/icons}}
<link type="text/html" href="/{{{content_id}}}" />
<author>
<name>{{author_name}}</name>
Expand Down
13 changes: 10 additions & 3 deletions test/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" },

Expand Down Expand Up @@ -626,7 +627,9 @@ std::string maskVariableOPDSFeedData(std::string s)
" <tags>unittest;wikipedia;_category:jazz;_pictures:no;_videos:no;_details:no;_ftindex:yes</tags>\n" \
" <articleCount>284</articleCount>\n" \
" <mediaCount>2</mediaCount>\n" \
" <icon>/meta?name=favicon&amp;content=zimfile</icon>\n" \
" <link rel=\"http://opds-spec.org/image/thumbnail\"\n" \
" href=\"/meta?name=Illustration_48x48@1&amp;content=zimfile\"\n" \
" type=\"image/png;width=48;height=48;scale=1\"/>\n" \
" <link type=\"text/html\" href=\"/zimfile\" />\n" \
" <author>\n" \
" <name>Wikipedia</name>\n" \
Expand All @@ -650,7 +653,9 @@ std::string maskVariableOPDSFeedData(std::string s)
" <tags>unittest;wikipedia;_category:wikipedia;_pictures:no;_videos:no;_details:no;_ftindex:yes</tags>\n" \
" <articleCount>284</articleCount>\n" \
" <mediaCount>2</mediaCount>\n" \
" <icon>/meta?name=favicon&amp;content=zimfile</icon>\n" \
" <link rel=\"http://opds-spec.org/image/thumbnail\"\n" \
" href=\"/meta?name=Illustration_48x48@1&amp;content=zimfile\"\n" \
" type=\"image/png;width=48;height=48;scale=1\"/>\n" \
" <link type=\"text/html\" href=\"/zimfile\" />\n" \
" <author>\n" \
" <name>Wikipedia</name>\n" \
Expand All @@ -674,7 +679,9 @@ std::string maskVariableOPDSFeedData(std::string s)
" <tags>unittest;wikipedia;_pictures:no;_videos:no;_details:no</tags>\n" \
" <articleCount>284</articleCount>\n" \
" <mediaCount>2</mediaCount>\n" \
" <icon>/meta?name=favicon&amp;content=zimfile</icon>\n" \
" <link rel=\"http://opds-spec.org/image/thumbnail\"\n" \
" href=\"/meta?name=Illustration_48x48@1&amp;content=zimfile\"\n" \
" type=\"image/png;width=48;height=48;scale=1\"/>\n" \
" <link type=\"text/html\" href=\"/zimfile\" />\n" \
" <author>\n" \
" <name>Wikipedia</name>\n" \
Expand Down

0 comments on commit 6e26c5a

Please sign in to comment.