Работа над opds

This commit is contained in:
Book Pauk
2022-11-23 14:38:23 +07:00
parent 8cf370c79d
commit a6d9df7dec
4 changed files with 43 additions and 26 deletions

View File

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

View File

@@ -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 = {};

View File

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

View File

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