Начата работа над opds
This commit is contained in:
30
server/core/opds/BasePage.js
Normal file
30
server/core/opds/BasePage.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const XmlParser = require('../xml/XmlParser');
|
||||
|
||||
class BasePage {
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
|
||||
this.rootTag = 'feed';
|
||||
}
|
||||
|
||||
makeBody(content) {
|
||||
if (!this.id)
|
||||
throw new Error('makeBody: no id');
|
||||
|
||||
content.id = this.id;
|
||||
|
||||
const xml = new XmlParser();
|
||||
const xmlObject = {};
|
||||
xmlObject[this.rootTag] = content;
|
||||
|
||||
xml.fromObject(xmlObject);
|
||||
|
||||
return xml.toString({format: true});
|
||||
}
|
||||
|
||||
async body() {
|
||||
throw new Error('Body not implemented');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BasePage;
|
||||
17
server/core/opds/RootPage.js
Normal file
17
server/core/opds/RootPage.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const BasePage = require('./BasePage');
|
||||
|
||||
class RootPage extends BasePage {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
|
||||
this.id = 'root';
|
||||
}
|
||||
|
||||
async body() {
|
||||
const result = {};
|
||||
|
||||
return this.makeBody(result);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RootPage;
|
||||
34
server/core/opds/index.js
Normal file
34
server/core/opds/index.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const path = require('path');
|
||||
|
||||
const RootPage = require('./RootPage');
|
||||
|
||||
module.exports = function(app, config) {
|
||||
const root = new RootPage(config);
|
||||
|
||||
const pages = new Map([
|
||||
['opds', root]
|
||||
]);
|
||||
|
||||
const opds = async(req, res, next) => {
|
||||
try {
|
||||
const pageName = path.basename(req.path);
|
||||
const page = pages.get(pageName);
|
||||
|
||||
if (page) {
|
||||
res.set('Content-Type', 'application/atom+xml; charset=utf-8');
|
||||
|
||||
const result = await page.body(req, res);
|
||||
|
||||
if (result !== false)
|
||||
res.send(result);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500).send({error: e.message});
|
||||
}
|
||||
};
|
||||
|
||||
app.get(['/opds', '/opds/*'], opds);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user