Работа над opds

This commit is contained in:
Book Pauk
2022-11-20 19:52:10 +07:00
parent 037b42a5b4
commit aba0c206f8
4 changed files with 61 additions and 23 deletions

View File

@@ -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;

View File

@@ -7,7 +7,7 @@ class BasePage {
this.webWorker = new WebWorker(config); this.webWorker = new WebWorker(config);
this.rootTag = 'feed'; this.rootTag = 'feed';
this.opdsRoot = '/opds'; this.opdsRoot = config.opdsRoot;
} }
makeEntry(entry = {}) { makeEntry(entry = {}) {
@@ -23,6 +23,14 @@ class BasePage {
return Object.assign(result, entry); 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) { makeLink(attrs) {
return {'*ATTRS': 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) { makeBody(content) {
const base = this.makeEntry({id: this.id, title: this.title}); const base = this.makeEntry({id: this.id, title: this.title});
base['*ATTRS'] = { base['*ATTRS'] = {
@@ -43,9 +58,8 @@ class BasePage {
'xmlns:opds': 'http://opds-spec.org/2010/catalog', 'xmlns:opds': 'http://opds-spec.org/2010/catalog',
}; };
base.link = [ if (!content.link)
this.navLink({rel: 'start'}), base.link = this.baseLinks();
];
const xml = new XmlParser(); const xml = new XmlParser();
const xmlObject = {}; const xmlObject = {};

View File

@@ -1,4 +1,5 @@
const BasePage = require('./BasePage'); const BasePage = require('./BasePage');
const AuthorPage = require('./AuthorPage');
class RootPage extends BasePage { class RootPage extends BasePage {
constructor(config) { constructor(config) {
@@ -6,6 +7,8 @@ class RootPage extends BasePage {
this.id = 'root'; this.id = 'root';
this.title = ''; this.title = '';
this.authorPage = new AuthorPage(config);
} }
async body() { async body() {
@@ -20,17 +23,8 @@ class RootPage extends BasePage {
this.title = 'Неизвестная коллекция'; this.title = 'Неизвестная коллекция';
} }
result.link = [
this.navLink({rel: 'start'}),
this.navLink({rel: 'self'}),
];
result.entry = [ result.entry = [
this.makeEntry({ this.authorPage.myEntry(),
id: 'author',
title: 'Авторы',
link: this.navLink({rel: 'subsection', href: '/author'}),
}),
]; ];
return this.makeBody(result); return this.makeBody(result);

View File

@@ -1,18 +1,27 @@
const path = require('path');
const RootPage = require('./RootPage'); const RootPage = require('./RootPage');
const AuthorPage = require('./AuthorPage');
module.exports = function(app, config) { module.exports = function(app, config) {
const root = new RootPage(config); const opdsRoot = '/opds';
config.opdsRoot = opdsRoot;
const pages = new Map([ const root = new RootPage(config);
['opds', root] 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) => { const opds = async(req, res, next) => {
try { try {
const pageName = path.basename(req.path); const page = pages.get(req.path);
const page = pages.get(pageName);
if (page) { if (page) {
res.set('Content-Type', 'application/atom+xml; charset=utf-8'); 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);
}; };