mirror of https://github.com/kiwix/libkiwix.git
Drop Reader from InternalServer::handle_random
This commit is contained in:
parent
a236751c74
commit
75b4d311d7
|
@ -40,6 +40,7 @@ namespace kiwix
|
||||||
std::string getMetaDate(const zim::Archive* const archive);
|
std::string getMetaDate(const zim::Archive* const archive);
|
||||||
std::string getMetaCreator(const zim::Archive* const archive);
|
std::string getMetaCreator(const zim::Archive* const archive);
|
||||||
std::string getMetaPublisher(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);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -58,6 +58,8 @@ extern "C" {
|
||||||
#include <zim/uuid.h>
|
#include <zim/uuid.h>
|
||||||
#include <zim/error.h>
|
#include <zim/error.h>
|
||||||
#include <zim/search.h>
|
#include <zim/search.h>
|
||||||
|
#include <zim/entry.h>
|
||||||
|
#include <zim/item.h>
|
||||||
|
|
||||||
#include <mustache.hpp>
|
#include <mustache.hpp>
|
||||||
|
|
||||||
|
@ -363,20 +365,6 @@ SuggestionsList_t getSuggestions(const zim::Archive* const archive,
|
||||||
return suggestions;
|
return suggestions;
|
||||||
}
|
}
|
||||||
|
|
||||||
zim::Entry getFinalEntry(const zim::Archive* const archive, const zim::Entry& entry)
|
|
||||||
{
|
|
||||||
int loopCounter = 42;
|
|
||||||
auto final_entry = entry;
|
|
||||||
while (final_entry.isRedirect() && loopCounter--) {
|
|
||||||
final_entry = final_entry.getRedirectEntry();
|
|
||||||
}
|
|
||||||
// Prevent infinite loops.
|
|
||||||
if (final_entry.isRedirect()) {
|
|
||||||
throw zim::EntryNotFound("Unable to resolve entry redirects.");
|
|
||||||
}
|
|
||||||
return final_entry;
|
|
||||||
}
|
|
||||||
|
|
||||||
zim::Entry getEntryFromPath(const zim::Archive* const archive, const std::string& path)
|
zim::Entry getEntryFromPath(const zim::Archive* const archive, const std::string& path)
|
||||||
{
|
{
|
||||||
if (path.empty() || path == "/") {
|
if (path.empty() || path == "/") {
|
||||||
|
@ -428,6 +416,20 @@ SuggestionsList_t getSuggestions(const zim::Archive* const archive,
|
||||||
return suggestions;
|
return suggestions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zim::Entry getFinalEntry(const zim::Archive* const archive, const zim::Entry& entry)
|
||||||
|
{
|
||||||
|
int loopCounter = 42;
|
||||||
|
auto final_entry = entry;
|
||||||
|
while (final_entry.isRedirect() && loopCounter--) {
|
||||||
|
final_entry = final_entry.getRedirectEntry();
|
||||||
|
}
|
||||||
|
// Prevent infinite loops.
|
||||||
|
if (final_entry.isRedirect()) {
|
||||||
|
throw zim::EntryNotFound("Unable to resolve entry redirects.");
|
||||||
|
}
|
||||||
|
return final_entry;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Archive and Zim handlers end
|
* Archive and Zim handlers end
|
||||||
**/
|
**/
|
||||||
|
@ -683,23 +685,23 @@ std::unique_ptr<Response> InternalServer::handle_random(const RequestContext& re
|
||||||
|
|
||||||
std::string bookName;
|
std::string bookName;
|
||||||
std::string bookId;
|
std::string bookId;
|
||||||
std::shared_ptr<Reader> reader;
|
std::shared_ptr<zim::Archive> archive;
|
||||||
try {
|
try {
|
||||||
bookName = request.get_argument("content");
|
bookName = request.get_argument("content");
|
||||||
bookId = mp_nameMapper->getIdForName(bookName);
|
bookId = mp_nameMapper->getIdForName(bookName);
|
||||||
reader = mp_library->getReaderById(bookId);
|
archive = mp_library->getArchiveById(bookId);
|
||||||
} catch (const std::out_of_range&) {
|
} catch (const std::out_of_range&) {
|
||||||
return Response::build_404(*this, request, bookName, "");
|
return Response::build_404(*this, request, bookName, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reader == nullptr) {
|
if (archive == nullptr) {
|
||||||
return Response::build_404(*this, request, bookName, "");
|
return Response::build_404(*this, request, bookName, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
auto entry = reader->getRandomPage();
|
auto entry = archive->getRandomEntry();
|
||||||
return build_redirect(bookName, entry.getFinalEntry());
|
return build_redirect(bookName, getFinalEntry(archive.get(), entry));
|
||||||
} catch(kiwix::NoEntry& e) {
|
} catch(zim::EntryNotFound& e) {
|
||||||
return Response::build_404(*this, request, bookName, "");
|
return Response::build_404(*this, request, bookName, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,4 +100,9 @@ std::string getMetaPublisher(const zim::Archive* const archive) {
|
||||||
return getMetadata(archive, "Publisher");
|
return getMetadata(archive, "Publisher");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
zim::Entry getFinalEntry(const zim::Archive* const archive, const zim::Entry& entry)
|
||||||
|
{
|
||||||
|
return archive->getEntryByPath(entry.getItem(true).getPath());
|
||||||
|
}
|
||||||
|
|
||||||
} // kiwix
|
} // kiwix
|
||||||
|
|
Loading…
Reference in New Issue