From aba0c206f8081a2416c031493b096727f2097141 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Sun, 20 Nov 2022 19:52:10 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=20opds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/opds/AuthorPage.js | 21 +++++++++++++++++++++ server/core/opds/BasePage.js | 22 ++++++++++++++++++---- server/core/opds/RootPage.js | 14 ++++---------- server/core/opds/index.js | 27 ++++++++++++++++++--------- 4 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 server/core/opds/AuthorPage.js diff --git a/server/core/opds/AuthorPage.js b/server/core/opds/AuthorPage.js new file mode 100644 index 0000000..491552b --- /dev/null +++ b/server/core/opds/AuthorPage.js @@ -0,0 +1,21 @@ +const BasePage = require('./BasePage'); + +class AuthorPage extends BasePage { + constructor(config) { + super(config); + + this.id = 'author'; + this.title = 'Авторы'; + } + + async body() { + const result = {}; + + result.entry = [ + ]; + + return this.makeBody(result); + } +} + +module.exports = AuthorPage; \ No newline at end of file diff --git a/server/core/opds/BasePage.js b/server/core/opds/BasePage.js index 7556e18..0b99bd6 100644 --- a/server/core/opds/BasePage.js +++ b/server/core/opds/BasePage.js @@ -7,7 +7,7 @@ class BasePage { this.webWorker = new WebWorker(config); this.rootTag = 'feed'; - this.opdsRoot = '/opds'; + this.opdsRoot = config.opdsRoot; } makeEntry(entry = {}) { @@ -23,6 +23,14 @@ class BasePage { return Object.assign(result, entry); } + myEntry() { + return this.makeEntry({ + id: this.id, + title: this.title, + link: this.navLink({rel: 'subsection', href: `/${this.id}`}), + }); + } + makeLink(attrs) { return {'*ATTRS': attrs}; } @@ -35,6 +43,13 @@ class BasePage { }); } + baseLinks() { + return [ + this.navLink({rel: 'start'}), + this.navLink({rel: 'self', href: (this.id ? `/${this.id}` : '')}), + ]; + } + makeBody(content) { const base = this.makeEntry({id: this.id, title: this.title}); base['*ATTRS'] = { @@ -43,9 +58,8 @@ class BasePage { 'xmlns:opds': 'http://opds-spec.org/2010/catalog', }; - base.link = [ - this.navLink({rel: 'start'}), - ]; + if (!content.link) + base.link = this.baseLinks(); const xml = new XmlParser(); const xmlObject = {}; diff --git a/server/core/opds/RootPage.js b/server/core/opds/RootPage.js index 53ca182..ad8e8c5 100644 --- a/server/core/opds/RootPage.js +++ b/server/core/opds/RootPage.js @@ -1,4 +1,5 @@ const BasePage = require('./BasePage'); +const AuthorPage = require('./AuthorPage'); class RootPage extends BasePage { constructor(config) { @@ -6,6 +7,8 @@ class RootPage extends BasePage { this.id = 'root'; this.title = ''; + + this.authorPage = new AuthorPage(config); } async body() { @@ -20,17 +23,8 @@ class RootPage extends BasePage { this.title = 'Неизвестная коллекция'; } - result.link = [ - this.navLink({rel: 'start'}), - this.navLink({rel: 'self'}), - ]; - result.entry = [ - this.makeEntry({ - id: 'author', - title: 'Авторы', - link: this.navLink({rel: 'subsection', href: '/author'}), - }), + this.authorPage.myEntry(), ]; return this.makeBody(result); diff --git a/server/core/opds/index.js b/server/core/opds/index.js index d6ad0d3..6f80b74 100644 --- a/server/core/opds/index.js +++ b/server/core/opds/index.js @@ -1,18 +1,27 @@ -const path = require('path'); - const RootPage = require('./RootPage'); +const AuthorPage = require('./AuthorPage'); module.exports = function(app, config) { - const root = new RootPage(config); + const opdsRoot = '/opds'; + config.opdsRoot = opdsRoot; - const pages = new Map([ - ['opds', root] - ]); + const root = new RootPage(config); + const author = new AuthorPage(config); + + const routes = [ + ['', root], + ['/root', root], + ['/author', author], + ]; + + const pages = new Map(); + for (const r of routes) { + pages.set(`${opdsRoot}${r[0]}`, r[1]); + } const opds = async(req, res, next) => { try { - const pageName = path.basename(req.path); - const page = pages.get(pageName); + const page = pages.get(req.path); if (page) { res.set('Content-Type', 'application/atom+xml; charset=utf-8'); @@ -29,6 +38,6 @@ module.exports = function(app, config) { } }; - app.get(['/opds', '/opds/*'], opds); + app.get([opdsRoot, `${opdsRoot}/*`], opds); };