From a6d9df7dec78115785573bc62b46674a2b8d54f2 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Wed, 23 Nov 2022 14:38:23 +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 | 6 +++--- server/core/opds/BasePage.js | 28 ++++++++++++++++++---------- server/core/opds/BookPage.js | 31 ++++++++++++++++++++----------- server/core/opds/RootPage.js | 4 ++-- 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/server/core/opds/AuthorPage.js b/server/core/opds/AuthorPage.js index 42f732e..8e80742 100644 --- a/server/core/opds/AuthorPage.js +++ b/server/core/opds/AuthorPage.js @@ -94,7 +94,7 @@ class AuthorPage extends BasePage { this.makeEntry({ id: book._uid, title, - link: this.navLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}), + link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}), }) ); } @@ -124,7 +124,7 @@ class AuthorPage extends BasePage { this.makeEntry({ id: b.book._uid, title, - link: this.navLink({href: `/book?uid=${encodeURIComponent(b.book._uid)}`}), + link: this.acqLink({href: `/book?uid=${encodeURIComponent(b.book._uid)}`}), }) ); } @@ -146,7 +146,7 @@ class AuthorPage extends BasePage { } result.entry = entry; - return this.makeBody(result); + return this.makeBody(result, req); } } diff --git a/server/core/opds/BasePage.js b/server/core/opds/BasePage.js index 7f8d299..08a1614 100644 --- a/server/core/opds/BasePage.js +++ b/server/core/opds/BasePage.js @@ -50,28 +50,36 @@ class BasePage { navLink(attrs) { return this.makeLink({ - href: this.opdsRoot + (attrs.href || ''), + href: (attrs.hrefAsIs ? attrs.href : `${this.opdsRoot}${attrs.href || ''}`), rel: attrs.rel || 'subsection', - type: 'application/atom+xml; profile=opds-catalog; kind=navigation', + type: 'application/atom+xml;profile=opds-catalog;kind=navigation', }); } acqLink(attrs) { + return this.makeLink({ + href: this.opdsRoot + (attrs.href || ''), + rel: attrs.rel || 'subsection', + type: 'application/atom+xml;profile=opds-catalog;kind=acquisition', + }); + } + + downLink(attrs) { if (!attrs.href) - throw new Error('acqLink: no href'); + throw new Error('downLink: no href'); if (!attrs.type) - throw new Error('acqLink: no type'); + throw new Error('downLink: no type'); return this.makeLink({ href: attrs.href, - rel: 'http://opds-spec.org/acquisition/open-access', + rel: 'http://opds-spec.org/acquisition', type: attrs.type, }); } imgLink(attrs) { if (!attrs.href) - throw new Error('acqLink: no href'); + throw new Error('imgLink: no href'); return this.makeLink({ href: attrs.href, @@ -80,14 +88,14 @@ class BasePage { }); } - baseLinks() { + baseLinks(req) { return [ this.navLink({rel: 'start'}), - this.navLink({rel: 'self', href: (this.id ? `/${this.id}` : '')}), + this.navLink({rel: 'self', href: req.originalUrl, hrefAsIs: true}), ]; } - makeBody(content) { + makeBody(content, req) { const base = this.makeEntry({id: this.id, title: this.title}); base['*ATTRS'] = { 'xmlns': 'http://www.w3.org/2005/Atom', @@ -96,7 +104,7 @@ class BasePage { }; if (!content.link) - base.link = this.baseLinks(); + base.link = this.baseLinks(req); const xml = new XmlParser(); const xmlObject = {}; diff --git a/server/core/opds/BookPage.js b/server/core/opds/BookPage.js index 469e5db..88db7d9 100644 --- a/server/core/opds/BookPage.js +++ b/server/core/opds/BookPage.js @@ -1,3 +1,4 @@ +const path = require('path'); const BasePage = require('./BasePage'); class BookPage extends BasePage { @@ -16,21 +17,29 @@ class BookPage extends BasePage { if (bookUid) { const {bookInfo} = await this.webWorker.getBookInfo(bookUid); if (bookInfo) { - entry.push( - this.makeEntry({ - id: bookUid, - title: bookInfo.book.title || 'Без названия', - link: [ - //this.imgLink({href: bookInfo.cover, type: coverType}), - this.acqLink({href: bookInfo.link, type: `application/${bookInfo.book.ext}+gzip`}), - ], - }) - ); + const e = this.makeEntry({ + id: bookUid, + title: bookInfo.book.title || 'Без названия', + link: [ + this.downLink({href: bookInfo.link, type: `application/${bookInfo.book.ext}+zip`}), + ], + }); + + if (bookInfo.cover) { + let coverType = 'image/jpeg'; + if (path.extname(bookInfo.cover) == '.png') + coverType = 'image/png'; + + e.link.push(this.imgLink({href: bookInfo.cover, type: coverType})); + e.link.push(this.imgLink({href: bookInfo.cover, type: coverType, thumb: true})); + } + + entry.push(e); } } result.entry = entry; - return this.makeBody(result); + return this.makeBody(result, req); } } diff --git a/server/core/opds/RootPage.js b/server/core/opds/RootPage.js index ffbe16c..3edb10a 100644 --- a/server/core/opds/RootPage.js +++ b/server/core/opds/RootPage.js @@ -11,7 +11,7 @@ class RootPage extends BasePage { this.authorPage = new AuthorPage(config); } - async body() { + async body(req) { const result = {}; if (!this.title) { @@ -26,7 +26,7 @@ class RootPage extends BasePage { this.authorPage.myEntry(), ]; - return this.makeBody(result); + return this.makeBody(result, req); } }