Skip to content

Commit

Permalink
fixup! Drop Reader and Entry wrappers from handle_content
Browse files Browse the repository at this point in the history
  • Loading branch information
maneeshpm committed Jun 16, 2021
1 parent 9ea4a55 commit 554bb9e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 59 deletions.
24 changes: 12 additions & 12 deletions include/tools/archiveTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@

namespace kiwix
{
std::string getMetadata(const zim::Archive* const archive, const std::string& name);
std::string getArchiveTitle(const zim::Archive* const archive);
std::string getMetaDescription(const zim::Archive* const archive);
std::string getMetaTags(const zim::Archive* const archive, bool original = false);
bool getArchiveFavicon(const zim::Archive* const archive,
std::string getMetadata(const zim::Archive& archive, const std::string& name);
std::string getArchiveTitle(const zim::Archive& archive);
std::string getMetaDescription(const zim::Archive& archive);
std::string getMetaTags(const zim::Archive& archive, bool original = false);
bool getArchiveFavicon(const zim::Archive& archive,
std::string& content, std::string& mimeType);
std::string getMetaLanguage(const zim::Archive* const archive);
std::string getMetaName(const zim::Archive* const archive);
std::string getMetaDate(const zim::Archive* const archive);
std::string getMetaCreator(const zim::Archive* const archive);
std::string getMetaPublisher(const zim::Archive* const archive);
zim::Entry getFinalEntry(const zim::Archive* const archive, const zim::Entry& entry);
zim::Entry getEntryFromPath(const zim::Archive* const archive, const std::string& path);
std::string getMetaLanguage(const zim::Archive& archive);
std::string getMetaName(const zim::Archive& archive);
std::string getMetaDate(const zim::Archive& archive);
std::string getMetaCreator(const zim::Archive& archive);
std::string getMetaPublisher(const zim::Archive& archive);
zim::Item getFinalItem(const zim::Archive& archive, const zim::Entry& entry);
zim::Entry getEntryFromPath(const zim::Archive& archive, const std::string& path);
}

#endif
20 changes: 10 additions & 10 deletions src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Entry Reader::getMainPage() const

bool Reader::getFavicon(string& content, string& mimeType) const
{
return kiwix::getArchiveFavicon(zimArchive.get(), content, mimeType);
return kiwix::getArchiveFavicon(*zimArchive, content, mimeType);
}

string Reader::getZimFilePath() const
Expand All @@ -211,32 +211,32 @@ bool Reader::getMetadata(const string& name, string& value) const

string Reader::getName() const
{
return kiwix::getMetaName(zimArchive.get());
return kiwix::getMetaName(*zimArchive);
}

string Reader::getTitle() const
{
return kiwix::getArchiveTitle(zimArchive.get());
return kiwix::getArchiveTitle(*zimArchive);
}

string Reader::getCreator() const
{
return kiwix::getMetaCreator(zimArchive.get());
return kiwix::getMetaCreator(*zimArchive);
}

string Reader::getPublisher() const
{
return kiwix::getMetaPublisher(zimArchive.get());
return kiwix::getMetaPublisher(*zimArchive);
}

string Reader::getDate() const
{
return kiwix::getMetaDate(zimArchive.get());
return kiwix::getMetaDate(*zimArchive);
}

string Reader::getDescription() const
{
return kiwix::getMetaDescription(zimArchive.get());
return kiwix::getMetaDescription(*zimArchive);
}

string Reader::getLongDescription() const
Expand All @@ -246,7 +246,7 @@ string Reader::getLongDescription() const

string Reader::getLanguage() const
{
return kiwix::getMetaLanguage(zimArchive.get());
return kiwix::getMetaLanguage(*zimArchive);
}

string Reader::getLicense() const
Expand All @@ -256,7 +256,7 @@ string Reader::getLicense() const

string Reader::getTags(bool original) const
{
return kiwix::getMetaTags(zimArchive.get(), original);
return kiwix::getMetaTags(*zimArchive, original);
}


Expand Down Expand Up @@ -321,7 +321,7 @@ string Reader::getOrigId() const
Entry Reader::getEntryFromPath(const std::string& path) const
{
try {
return kiwix::getEntryFromPath(zimArchive.get(), path);
return kiwix::getEntryFromPath(*zimArchive, path);
} catch (zim::EntryNotFound& e) {
throw NoEntry();
}
Expand Down
34 changes: 17 additions & 17 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,23 +392,23 @@ std::unique_ptr<Response> InternalServer::handle_meta(const RequestContext& requ
std::string mimeType = "text";

if (meta_name == "title") {
content = getArchiveTitle(archive.get());
content = getArchiveTitle(*archive);
} else if (meta_name == "description") {
content = getMetaDescription(archive.get());
content = getMetaDescription(*archive);
} else if (meta_name == "language") {
content = getMetaLanguage(archive.get());
content = getMetaLanguage(*archive);
} else if (meta_name == "name") {
content = getMetaName(archive.get());
content = getMetaName(*archive);
} else if (meta_name == "tags") {
content = getMetaTags(archive.get());
content = getMetaTags(*archive);
} else if (meta_name == "date") {
content = getMetaDate(archive.get());
content = getMetaDate(*archive);
} else if (meta_name == "creator") {
content = getMetaCreator(archive.get());
content = getMetaCreator(*archive);
} else if (meta_name == "publisher") {
content = getMetaPublisher(archive.get());
content = getMetaPublisher(*archive);
} else if (meta_name == "favicon") {
getArchiveFavicon(archive.get(), content, mimeType);
getArchiveFavicon(*archive, content, mimeType);
} else {
return Response::build_404(*this, request, bookName);
}
Expand Down Expand Up @@ -552,7 +552,7 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
auto data = get_default_data();
data.set("pattern", encodeDiples(patternString));
auto response = ContentResponse::build(*this, RESOURCE::templates::no_search_result_html, data, "text/html; charset=utf-8");
response->set_taskbar(bookName, archive ? getArchiveTitle(archive.get()) : "");
response->set_taskbar(bookName, archive ? getArchiveTitle(*archive) : "");
response->set_code(MHD_HTTP_NOT_FOUND);
return std::move(response);
}
Expand Down Expand Up @@ -623,7 +623,7 @@ std::unique_ptr<Response> InternalServer::handle_search(const RequestContext& re
renderer.setSearchProtocolPrefix(m_root + "/search?");
renderer.setPageLength(pageLength);
auto response = ContentResponse::build(*this, renderer.getHtml(), "text/html; charset=utf-8");
response->set_taskbar(bookName, archive ? getArchiveTitle(archive.get()) : "");
response->set_taskbar(bookName, archive ? getArchiveTitle(*archive) : "");

return std::move(response);
} catch (const std::exception& e) {
Expand Down Expand Up @@ -655,7 +655,7 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re

try {
auto entry = archive->getRandomEntry();
return build_redirect(bookName, getFinalEntry(archive.get(), entry));
return build_redirect(bookName, getFinalItem(*archive, entry));
} catch(zim::EntryNotFound& e) {
return Response::build_404(*this, request, bookName);
}
Expand Down Expand Up @@ -804,9 +804,9 @@ std::string searchSuggestionHTML(const std::string& searchURL, const std::string
} // unnamed namespace

std::unique_ptr<Response>
InternalServer::build_redirect(const std::string& bookName, const kiwix::Entry& entry) const
InternalServer::build_redirect(const std::string& bookName, const zim::Item& item) const
{
auto redirectUrl = m_root + "/" + bookName + "/" + kiwix::urlEncode(entry.getPath());
auto redirectUrl = m_root + "/" + bookName + "/" + kiwix::urlEncode(item.getPath());
return Response::build_redirect(*this, redirectUrl);
}

Expand Down Expand Up @@ -841,15 +841,15 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
}

try {
auto entry = getEntryFromPath(archive.get(), urlStr);
auto entry = getEntryFromPath(*archive, urlStr);
if (entry.isRedirect() || urlStr.empty()) {
// If urlStr is empty, we want to mainPage.
// We must do a redirection to the real page.
return build_redirect(bookName, getFinalEntry(archive.get(), entry));
return build_redirect(bookName, getFinalItem(*archive, entry));
}
auto response = ItemResponse::build(*this, request, entry.getItem());
try {
dynamic_cast<ContentResponse&>(*response).set_taskbar(bookName, getArchiveTitle(archive.get()));
dynamic_cast<ContentResponse&>(*response).set_taskbar(bookName, getArchiveTitle(*archive));
} catch (std::bad_cast& e) {}

if (m_verbose.load()) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/internalServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class InternalServer {

private: // functions
std::unique_ptr<Response> handle_request(const RequestContext& request);
std::unique_ptr<Response> build_redirect(const std::string& bookName, const kiwix::Entry& entry) const;
std::unique_ptr<Response> build_redirect(const std::string& bookName, const zim::Item& item) const;
std::unique_ptr<Response> build_homepage(const RequestContext& request);
std::unique_ptr<Response> handle_skin(const RequestContext& request);
std::unique_ptr<Response> handle_catalog(const RequestContext& request);
Expand Down
42 changes: 23 additions & 19 deletions src/tools/archiveTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@

namespace kiwix
{
std::string getMetadata(const zim::Archive* const archive, const std::string& name) {
std::string getMetadata(const zim::Archive& archive, const std::string& name) {
try {
return archive->getMetadata(name);
return archive.getMetadata(name);
} catch (zim::EntryNotFound& e) {
return "";
}
}

std::string getArchiveTitle(const zim::Archive* const archive) {
std::string getArchiveTitle(const zim::Archive& archive) {
std::string value = getMetadata(archive, "Title");
if (value.empty()) {
value = getLastPathElement(archive->getFilename());
value = getLastPathElement(archive.getFilename());
std::replace(value.begin(), value.end(), '_', ' ');
size_t pos = value.find(".zim");
value = value.substr(0, pos);
}
return value;
}

std::string getMetaDescription(const zim::Archive* const archive) {
std::string getMetaDescription(const zim::Archive& archive) {
std::string value;
value = getMetadata(archive, "Description");

Expand All @@ -58,7 +58,7 @@ std::string getMetaDescription(const zim::Archive* const archive) {
return value;
}

std::string getMetaTags(const zim::Archive* const archive, bool original) {
std::string getMetaTags(const zim::Archive& archive, bool original) {
std::string tags_str = getMetadata(archive, "Tags");
if (original) {
return tags_str;
Expand All @@ -67,10 +67,10 @@ std::string getMetaTags(const zim::Archive* const archive, bool original) {
return join(tags, ";");
}

bool getArchiveFavicon(const zim::Archive* const archive,
bool getArchiveFavicon(const zim::Archive& archive,
std::string& content, std::string& mimeType){
try {
auto entry = archive->getFaviconEntry();
auto entry = archive.getFaviconEntry();
auto item = entry.getItem(true);
content = item.getData();
mimeType = item.getMimetype();
Expand All @@ -80,37 +80,41 @@ bool getArchiveFavicon(const zim::Archive* const archive,
return false;
}

std::string getMetaLanguage(const zim::Archive* const archive) {
std::string getMetaLanguage(const zim::Archive& archive) {
return getMetadata(archive, "Language");
}

std::string getMetaName(const zim::Archive* const archive) {
std::string getMetaName(const zim::Archive& archive) {
return getMetadata(archive, "Name");
}

std::string getMetaDate(const zim::Archive* const archive) {
std::string getMetaDate(const zim::Archive& archive) {
return getMetadata(archive, "Date");
}

std::string getMetaCreator(const zim::Archive* const archive) {
std::string getMetaCreator(const zim::Archive& archive) {
return getMetadata(archive, "Creator");
}

std::string getMetaPublisher(const zim::Archive* const archive) {
std::string getMetaPublisher(const zim::Archive& archive) {
return getMetadata(archive, "Publisher");
}

zim::Entry getFinalEntry(const zim::Archive* const archive, const zim::Entry& entry)
zim::Item getFinalItem(const zim::Archive& archive, const zim::Entry& entry)
{
return archive->getEntryByPath(entry.getItem(true).getPath());
return entry.getItem(true);
}

zim::Entry getEntryFromPath(const zim::Archive* const archive, const std::string& path)
zim::Entry getEntryFromPath(const zim::Archive& archive, const std::string& path)
{
if (path.empty() || path == "/") {
return archive->getMainEntry();
try {
return archive.getEntryByPath(path);
} catch (zim::EntryNotFound& e) {
if (path.empty()) {
return archive.getMainEntry();
}
}
return archive->getEntryByPath(path);
throw zim::EntryNotFound("Cannot find entry for non empty path");
}

} // kiwix

0 comments on commit 554bb9e

Please sign in to comment.