From 8a71c4040ce2c1b87402bde93d83a5e28e0fdacb Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Sun, 20 Nov 2022 17:47:15 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D1=82=D0=B0=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BD=D0=B0=D0=B4=20opds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/opds/BasePage.js | 30 ++++++++++++++++++++++++++++++ server/core/opds/RootPage.js | 17 +++++++++++++++++ server/core/opds/index.js | 34 ++++++++++++++++++++++++++++++++++ server/index.js | 2 ++ 4 files changed, 83 insertions(+) create mode 100644 server/core/opds/BasePage.js create mode 100644 server/core/opds/RootPage.js create mode 100644 server/core/opds/index.js diff --git a/server/core/opds/BasePage.js b/server/core/opds/BasePage.js new file mode 100644 index 0000000..3527328 --- /dev/null +++ b/server/core/opds/BasePage.js @@ -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; \ No newline at end of file diff --git a/server/core/opds/RootPage.js b/server/core/opds/RootPage.js new file mode 100644 index 0000000..7b01694 --- /dev/null +++ b/server/core/opds/RootPage.js @@ -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; \ No newline at end of file diff --git a/server/core/opds/index.js b/server/core/opds/index.js new file mode 100644 index 0000000..d6ad0d3 --- /dev/null +++ b/server/core/opds/index.js @@ -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); +}; + diff --git a/server/index.js b/server/index.js index 9e2e8aa..d86e51c 100644 --- a/server/index.js +++ b/server/index.js @@ -5,6 +5,7 @@ const express = require('express'); const http = require('http'); const WebSocket = require ('ws'); +const opds = require('./core/opds'); const utils = require('./core/utils'); const ayncExit = new (require('./core/AsyncExit'))(); @@ -154,6 +155,7 @@ async function main() { if (devModule) devModule.logQueries(app); + opds(app, config); initStatic(app, config); const { WebSocketController } = require('./controllers');