Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Filter {
uint64_t activeFilters;
Tags _acceptTags;
Tags _rejectTags;
std::string _id;
std::string _category;
std::string _lang;
std::string _publisher;
Expand Down Expand Up @@ -141,7 +142,7 @@ class Filter {
* which case a book in any of those languages will match).
*/
Filter& lang(std::string lang);

Filter& id(std::string id);
Filter& publisher(std::string publisher);
Filter& creator(std::string creator);
Filter& maxSize(size_t size);
Expand All @@ -154,7 +155,8 @@ class Filter {
bool hasQuery() const;
const std::string& getQuery() const { return _query; }
bool queryIsPartial() const { return _queryIsPartial; }

bool hasId() const;
const std::string& getId() const { return _id; }
bool hasName() const;
const std::string& getName() const { return _name; }

Expand Down
21 changes: 19 additions & 2 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,10 @@ Xapian::Query langQuery(const std::string& commaSeparatedLanguageList)
{
return multipleParamQuery(commaSeparatedLanguageList, "L");
}

Xapian::Query idQuery(const std::string& commaSeparatedIdList)
{
return multipleParamQuery(commaSeparatedIdList, "Q");
}
Xapian::Query publisherQuery(const std::string& publisher)
{
Xapian::QueryParser queryParser;
Expand Down Expand Up @@ -743,6 +746,9 @@ Xapian::Query buildXapianQuery(const Filter& filter)
if ( filter.hasCreator() ) {
q = Xapian::Query(Xapian::Query::OP_AND, q, creatorQuery(filter.getCreator()));
}
if (filter.hasId()) {
q = Xapian::Query(Xapian::Query::OP_AND, q, idQuery(filter.getId()));
}
if ( !filter.getAcceptTags().empty() || !filter.getRejectTags().empty() ) {
const auto tq = tagsQuery(filter.getAcceptTags(), filter.getRejectTags());
q = Xapian::Query(Xapian::Query::OP_AND, q, tq);;
Expand Down Expand Up @@ -897,6 +903,7 @@ enum filterTypes {
NAME = FLAG(13),
CATEGORY = FLAG(14),
FLAVOUR = FLAG(15),
ID = FLAG(16)
};

Filter& Filter::local(bool accept)
Expand Down Expand Up @@ -962,7 +969,12 @@ Filter& Filter::lang(std::string lang)
activeFilters |= LANG;
return *this;
}

Filter& Filter::id(std::string id)
{
_id = id;
activeFilters |= ID;
return *this;
}
Filter& Filter::publisher(std::string publisher)
{
_publisher = publisher;
Expand Down Expand Up @@ -1040,6 +1052,11 @@ bool Filter::hasLang() const
return ACTIVE(LANG);
}

bool Filter::hasId() const
{
return ACTIVE(ID);
}

bool Filter::hasPublisher() const
{
return ACTIVE(_PUBLISHER);
Expand Down
19 changes: 16 additions & 3 deletions src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ extern "C" {

#include <atomic>
#include <string>
#include <regex>
#include <vector>
#include <chrono>
#include <fstream>
Expand Down Expand Up @@ -142,6 +143,9 @@ Filter get_search_filter(const RequestContext& request, const std::string& prefi
try {
filter.rejectTags(kiwix::split(request.get_argument(prefix+"notag"), ";"));
} catch (...) {}
try {
filter.id(request.get_argument(prefix + "id"));
} catch (const std::out_of_range&) {}
return filter;
}

Expand Down Expand Up @@ -1169,11 +1173,20 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
const std::string contentPrefix = "/content/";
const bool isContentPrefixedUrl = startsWith(url, contentPrefix);
const size_t prefixLength = isContentPrefixedUrl ? contentPrefix.size() : 1;
const std::string bookName = request.get_url_part(isContentPrefixedUrl);
const std::regex uuid_regex("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");

const std::string requestedBookNameOrUUID = request.get_url_part(isContentPrefixedUrl);
std::shared_ptr<zim::Archive> archive;
std::string bookName;
try {
const std::string bookId = mp_nameMapper->getIdForName(bookName);
std::string bookId;
if (std::regex_match(requestedBookNameOrUUID, uuid_regex)) {
bookId = requestedBookNameOrUUID;
} else {
bookId = mp_nameMapper->getIdForName(requestedBookNameOrUUID);
}

bookName = mp_nameMapper->getNameForId(bookId);
archive = mp_library->getArchiveById(bookId);
} catch (const std::out_of_range& e) {}

Expand All @@ -1186,7 +1199,7 @@ std::unique_ptr<Response> InternalServer::handle_content(const RequestContext& r
if ( etag )
return Response::build_304(etag);

auto urlStr = url.substr(prefixLength + bookName.size());
auto urlStr = url.substr(prefixLength + requestedBookNameOrUUID.size());
if (urlStr[0] == '/') {
urlStr = urlStr.substr(1);
}
Expand Down
24 changes: 22 additions & 2 deletions static/skin/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,29 @@ function setIframeUrl(path) {
}
contentIframe.contentWindow.location.replace(path);
}
async function getBookNameFromUUID(uuid) {
const url = `${root}/catalog/v2/entries?id=${uuid}`;
return await fetch(url).then(async (resp) => {
const data = new window.DOMParser().parseFromString(await resp.text(), 'application/xml');
const result = parseInt(data.querySelector('totalResults').innerHTML);
let bookName;
if (result > 0) {
const bookContentLink = data.querySelector('link[type="text/html"]');
const urlComponents = bookContentLink.getAttribute('href').split('/');
bookName = urlComponents.pop();
} else {
bookName = null
}
return bookName;
});
}

function handle_location_hash_change() {
const hash = window.location.hash.slice(1);
async function handle_location_hash_change() {
hash = window.location.hash.slice(1);
//this regex exact matches zim UUIDS
if (hash.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/) != null) {
hash = await getBookNameFromUUID(hash)
}
console.log("handle_location_hash_change: " + hash);
updateCurrentBookIfNeeded(hash);
setIframeUrl(userUrl2IframeUrl(hash));
Expand Down
7 changes: 4 additions & 3 deletions test/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const ResourceCollection resources200Compressible{
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css" },
{ STATIC_CONTENT, "/ROOT%23%3F/skin/taskbar.css?cacheid=42e90cb9" },
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/viewer.js" },
{ STATIC_CONTENT, "/ROOT%23%3F/skin/viewer.js?cacheid=3208c3ed" },
{ STATIC_CONTENT, "/ROOT%23%3F/skin/viewer.js?cacheid=a754e1ec" },
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf" },
{ STATIC_CONTENT, "/ROOT%23%3F/skin/fonts/Poppins.ttf?cacheid=af705837" },
{ DYNAMIC_CONTENT, "/ROOT%23%3F/skin/fonts/Roboto.ttf" },
Expand Down Expand Up @@ -242,7 +242,8 @@ TEST_F(ServerTest, 200)

TEST_F(ServerTest, 200_IdNameMapper)
{
EXPECT_EQ(404, zfs1_->GET("/ROOT%23%3F/content/6f1d19d0-633f-087b-fb55-7ac324ff9baf/A/index")->status);
//both uuid and name should work but after NO_NAME_MAPPER only uuid should work
EXPECT_EQ(200, zfs1_->GET("/ROOT%23%3F/content/6f1d19d0-633f-087b-fb55-7ac324ff9baf/A/index")->status);
EXPECT_EQ(200, zfs1_->GET("/ROOT%23%3F/content/zimfile/A/index")->status);
resetServer(ZimFileServer::NO_NAME_MAPPER);
EXPECT_EQ(200, zfs1_->GET("/ROOT%23%3F/content/6f1d19d0-633f-087b-fb55-7ac324ff9baf/A/index")->status);
Expand Down Expand Up @@ -338,7 +339,7 @@ R"EXPECTEDRESULT( <link type="text/css" href="./skin/kiwix.css?cacheid=b4e29e
<script type="text/javascript" src="./skin/polyfills.js?cacheid=a0e0343d"></script>
<script type="module" src="./skin/i18n.js?cacheid=e9a10ac1" defer></script>
<script type="text/javascript" src="./skin/languages.js?cacheid=08955948" defer></script>
<script type="text/javascript" src="./skin/viewer.js?cacheid=3208c3ed" defer></script>
<script type="text/javascript" src="./skin/viewer.js?cacheid=a754e1ec" defer></script>
<script type="text/javascript" src="./skin/autoComplete/autoComplete.min.js?cacheid=1191aaaf"></script>
const blankPageUrl = root + "/skin/blank.html?cacheid=6b1fa032";
<label for="kiwix_button_show_toggle"><img src="./skin/caret.png?cacheid=22b942b4" alt=""></label>
Expand Down
Loading