From 6815a4c6a939e10bf1bb648f24d600779c1001be Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Fri, 7 Oct 2022 11:40:15 +0200 Subject: [PATCH] Make the OPDSDumper use the ServerConfiguration. --- include/opds_dumper.h | 18 ++------- src/opds_dumper.cpp | 49 ++++++++++++------------ src/server/internalServer.cpp | 3 +- src/server/internalServer_catalog_v2.cpp | 12 ++---- 4 files changed, 34 insertions(+), 48 deletions(-) diff --git a/include/opds_dumper.h b/include/opds_dumper.h index b6ed2e85b..f16678a27 100644 --- a/include/opds_dumper.h +++ b/include/opds_dumper.h @@ -23,11 +23,11 @@ #include #include #include +#include #include -#include "library.h" -#include "name_mapper.h" +#include "server.h" using namespace std; @@ -41,8 +41,7 @@ namespace kiwix class OPDSDumper { public: - OPDSDumper() = default; - OPDSDumper(std::shared_ptr library, std::shared_ptr NameMapper); + OPDSDumper(Server::Configuration configuration); ~OPDSDumper(); /** @@ -93,13 +92,6 @@ class OPDSDumper */ void setLibraryId(const std::string& id) { this->libraryId = id;} - /** - * Set the root location used when generating url. - * - * @param rootLocation the root location to use. - */ - void setRootLocation(const std::string& rootLocation) { this->rootLocation = rootLocation; } - /** * Set some informations about the search results. * @@ -110,10 +102,8 @@ class OPDSDumper void setOpenSearchInfo(int totalResult, int startIndex, int count); protected: - std::shared_ptr library; - std::shared_ptr nameMapper; + Server::Configuration m_configuration; std::string libraryId; - std::string rootLocation; int m_totalResults; int m_startIndex; int m_count; diff --git a/src/opds_dumper.cpp b/src/opds_dumper.cpp index 683e1af1e..557d196e9 100644 --- a/src/opds_dumper.cpp +++ b/src/opds_dumper.cpp @@ -19,6 +19,8 @@ #include "opds_dumper.h" #include "book.h" +#include "library.h" +#include "name_mapper.h" #include "libkiwix-resources.h" #include @@ -30,9 +32,8 @@ namespace kiwix { /* Constructor */ -OPDSDumper::OPDSDumper(std::shared_ptr library, std::shared_ptr nameMapper) - : library(library), - nameMapper(nameMapper) +OPDSDumper::OPDSDumper(Server::Configuration configuration) + : m_configuration(configuration) { } /* Destructor */ @@ -72,11 +73,11 @@ IllustrationInfo getBookIllustrationInfo(const Book& book) return illustrations; } -std::string fullEntryXML(const Book& book, const std::string& rootLocation, const std::string& bookName) +std::string fullEntryXML(const Server::Configuration& configuration, const Book& book, const std::string& bookName) { const auto bookDate = book.getDate() + "T00:00:00Z"; const kainjow::mustache::object data{ - {"root", rootLocation}, + {"root", configuration.m_root}, {"id", book.getId()}, {"name", book.getName()}, {"title", book.getTitle()}, @@ -99,12 +100,12 @@ std::string fullEntryXML(const Book& book, const std::string& rootLocation, cons return render_template(RESOURCE::templates::catalog_v2_entry_xml, data); } -std::string partialEntryXML(const Book& book, const std::string& rootLocation) +std::string partialEntryXML(const Server::Configuration& configuration, const Book& book) { const auto bookDate = book.getDate() + "T00:00:00Z"; const kainjow::mustache::object data{ - {"root", rootLocation}, - {"endpoint_root", rootLocation + "/catalog/v2"}, + {"root", configuration.m_root}, + {"endpoint_root", configuration.m_root + "/catalog/v2"}, {"id", book.getId()}, {"title", book.getTitle()}, {"updated", bookDate}, // XXX: this should be the entry update datetime @@ -113,16 +114,16 @@ std::string partialEntryXML(const Book& book, const std::string& rootLocation) return render_template(xmlTemplate, data); } -BooksData getBooksData(const Library& library, const NameMapper& nameMapper, const std::vector& bookIds, const std::string& rootLocation, bool partial) +BooksData getBooksData(const Server::Configuration& configuration, const std::vector& bookIds, bool partial) { BooksData booksData; for ( const auto& bookId : bookIds ) { try { - const Book book = library.getBookByIdThreadSafe(bookId); - const std::string bookName = nameMapper.getNameForId(bookId); + const Book book = configuration.mp_library->getBookByIdThreadSafe(bookId); + const std::string bookName = configuration.mp_nameMapper->getNameForId(bookId); const auto entryXML = partial - ? partialEntryXML(book, rootLocation) - : fullEntryXML(book, rootLocation, bookName); + ? partialEntryXML(configuration, book) + : fullEntryXML(configuration, book, bookName); booksData.push_back(kainjow::mustache::object{ {"entry", entryXML} }); } catch ( const std::out_of_range& ) { // the book was removed from the library since its id was obtained @@ -190,10 +191,10 @@ std::string getLanguageSelfName(const std::string& lang) { string OPDSDumper::dumpOPDSFeed(const std::vector& bookIds, const std::string& query) const { - const auto booksData = getBooksData(*library, *nameMapper, bookIds, rootLocation, false); + const auto booksData = getBooksData(m_configuration, bookIds, false); const kainjow::mustache::object template_data{ {"date", gen_date_str()}, - {"root", rootLocation}, + {"root", m_configuration.m_root}, {"feed_id", gen_uuid(libraryId + "/catalog/search?"+query)}, {"filter", onlyAsNonEmptyMustacheValue(query)}, {"totalResults", to_string(m_totalResults)}, @@ -207,8 +208,8 @@ string OPDSDumper::dumpOPDSFeed(const std::vector& bookIds, const s string OPDSDumper::dumpOPDSFeedV2(const std::vector& bookIds, const std::string& query, bool partial) const { - const auto endpointRoot = rootLocation + "/catalog/v2"; - const auto booksData = getBooksData(*library, *nameMapper, bookIds, rootLocation, partial); + const auto endpointRoot = m_configuration.m_root + "/catalog/v2"; + const auto booksData = getBooksData(m_configuration, bookIds, partial); const char* const endpoint = partial ? "/partial_entries" : "/entries"; const kainjow::mustache::object template_data{ @@ -229,18 +230,18 @@ string OPDSDumper::dumpOPDSFeedV2(const std::vector& bookIds, const std::string OPDSDumper::dumpOPDSCompleteEntry(const std::string& bookId) const { - const auto book = library->getBookById(bookId); - const std::string bookName = nameMapper->getNameForId(bookId); + const auto book = m_configuration.mp_library->getBookById(bookId); + const std::string bookName = m_configuration.mp_nameMapper->getNameForId(bookId); return XML_HEADER + "\n" - + fullEntryXML(book, rootLocation, bookName); + + fullEntryXML(m_configuration, book, bookName); } std::string OPDSDumper::categoriesOPDSFeed() const { const auto now = gen_date_str(); kainjow::mustache::list categoryData; - for ( const auto& category : library->getBooksCategories() ) { + for ( const auto& category : m_configuration.mp_library->getBooksCategories() ) { const auto urlencodedCategoryName = urlEncode(category); categoryData.push_back(kainjow::mustache::object{ {"name", category}, @@ -254,7 +255,7 @@ std::string OPDSDumper::categoriesOPDSFeed() const RESOURCE::templates::catalog_v2_categories_xml, kainjow::mustache::object{ {"date", now}, - {"endpoint_root", rootLocation + "/catalog/v2"}, + {"endpoint_root", m_configuration.m_root + "/catalog/v2"}, {"feed_id", gen_uuid(libraryId + "/categories")}, {"categories", categoryData } } @@ -266,7 +267,7 @@ std::string OPDSDumper::languagesOPDSFeed() const const auto now = gen_date_str(); kainjow::mustache::list languageData; std::call_once(fillLanguagesFlag, fillLanguagesMap); - for ( const auto& langAndBookCount : library->getBooksLanguagesWithCounts() ) { + for ( const auto& langAndBookCount : m_configuration.mp_library->getBooksLanguagesWithCounts() ) { const std::string languageCode = langAndBookCount.first; const int bookCount = langAndBookCount.second; const auto languageSelfName = getLanguageSelfName(languageCode); @@ -283,7 +284,7 @@ std::string OPDSDumper::languagesOPDSFeed() const RESOURCE::templates::catalog_v2_languages_xml, kainjow::mustache::object{ {"date", now}, - {"endpoint_root", rootLocation + "/catalog/v2"}, + {"endpoint_root", m_configuration.m_root + "/catalog/v2"}, {"feed_id", gen_uuid(libraryId + "/languages")}, {"languages", languageData } } diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 3c92db8d3..7eece05b5 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -932,8 +932,7 @@ std::unique_ptr InternalServer::handle_catalog(const RequestContext& r } zim::Uuid uuid; - kiwix::OPDSDumper opdsDumper(mp_library, mp_nameMapper); - opdsDumper.setRootLocation(m_root); + kiwix::OPDSDumper opdsDumper(*this); opdsDumper.setLibraryId(m_library_id); std::vector bookIdsToDump; if (url == "root.xml") { diff --git a/src/server/internalServer_catalog_v2.cpp b/src/server/internalServer_catalog_v2.cpp index c5b57c743..7820af46c 100644 --- a/src/server/internalServer_catalog_v2.cpp +++ b/src/server/internalServer_catalog_v2.cpp @@ -95,8 +95,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_root(const RequestCo std::unique_ptr InternalServer::handle_catalog_v2_entries(const RequestContext& request, bool partial) { - OPDSDumper opdsDumper(mp_library, mp_nameMapper); - opdsDumper.setRootLocation(m_root); + OPDSDumper opdsDumper(*this); opdsDumper.setLibraryId(m_library_id); const auto bookIds = search_catalog(request, opdsDumper); const auto opdsFeed = opdsDumper.dumpOPDSFeedV2(bookIds, request.get_query(), partial); @@ -116,8 +115,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_complete_entry(const + urlNotFoundMsg; } - OPDSDumper opdsDumper(mp_library, mp_nameMapper); - opdsDumper.setRootLocation(m_root); + OPDSDumper opdsDumper(*this); opdsDumper.setLibraryId(m_library_id); const auto opdsFeed = opdsDumper.dumpOPDSCompleteEntry(entryId); return ContentResponse::build( @@ -129,8 +127,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_complete_entry(const std::unique_ptr InternalServer::handle_catalog_v2_categories(const RequestContext& request) { - OPDSDumper opdsDumper(mp_library, mp_nameMapper); - opdsDumper.setRootLocation(m_root); + OPDSDumper opdsDumper(*this); opdsDumper.setLibraryId(m_library_id); return ContentResponse::build( *this, @@ -141,8 +138,7 @@ std::unique_ptr InternalServer::handle_catalog_v2_categories(const Req std::unique_ptr InternalServer::handle_catalog_v2_languages(const RequestContext& request) { - OPDSDumper opdsDumper(mp_library, mp_nameMapper); - opdsDumper.setRootLocation(m_root); + OPDSDumper opdsDumper(*this); opdsDumper.setLibraryId(m_library_id); return ContentResponse::build( *this,