فهرست منبع

Работа над opds

Book Pauk 2 سال پیش
والد
کامیت
aba0c206f8
4فایلهای تغییر یافته به همراه60 افزوده شده و 22 حذف شده
  1. 21 0
      server/core/opds/AuthorPage.js
  2. 18 4
      server/core/opds/BasePage.js
  3. 4 10
      server/core/opds/RootPage.js
  4. 17 8
      server/core/opds/index.js

+ 21 - 0
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;

+ 18 - 4
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 = {};        

+ 4 - 10
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);

+ 17 - 8
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 opdsRoot = '/opds';
+    config.opdsRoot = opdsRoot;
+
     const root = new RootPage(config);
+    const author = new AuthorPage(config);
+
+    const routes = [
+        ['', root],
+        ['/root', root],
+        ['/author', author],
+    ];
 
-    const pages = new Map([
-        ['opds', root]
-    ]);
+    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);
 };