r2-shared-js
Version:
Readium 2 'shared' for NodeJS (TypeScript)
1,075 lines (1,074 loc) • 61.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMediaOverlay = exports.getAllMediaOverlays = exports.EpubParsePromise = exports.isEPUBlication = exports.EPUBis = exports.addCoverDimensions = exports.mediaOverlayURLParam = exports.mediaOverlayURLPath = exports.BCP47_UNKNOWN_LANG = void 0;
var tslib_1 = require("tslib");
var debug_ = require("debug");
var fs = require("fs");
var image_size_1 = require("image-size");
var path = require("path");
var url_1 = require("url");
var xmldom = require("@xmldom/xmldom");
var xpath = require("xpath");
var media_overlay_1 = require("../models/media-overlay");
var metadata_1 = require("../models/metadata");
var metadata_belongsto_1 = require("../models/metadata-belongsto");
var metadata_contributor_1 = require("../models/metadata-contributor");
var metadata_properties_1 = require("../models/metadata-properties");
var publication_1 = require("../models/publication");
var publication_link_1 = require("../models/publication-link");
var metadata_encrypted_1 = require("r2-lcp-js/dist/es5/src/models/metadata-encrypted");
var lcp_1 = require("r2-lcp-js/dist/es5/src/parser/epub/lcp");
var serializable_1 = require("r2-lcp-js/dist/es5/src/serializable");
var UrlUtils_1 = require("r2-utils-js/dist/es5/src/_utils/http/UrlUtils");
var BufferUtils_1 = require("r2-utils-js/dist/es5/src/_utils/stream/BufferUtils");
var xml_js_mapper_1 = require("r2-utils-js/dist/es5/src/_utils/xml-js-mapper");
var zipFactory_1 = require("r2-utils-js/dist/es5/src/_utils/zip/zipFactory");
var decodeURI_1 = require("../_utils/decodeURI");
var zipHasEntry_1 = require("../_utils/zipHasEntry");
var epub_daisy_common_1 = require("./epub-daisy-common");
var container_1 = require("./epub/container");
var display_options_1 = require("./epub/display-options");
var encryption_1 = require("./epub/encryption");
var debug = debug_("r2:shared#parser/epub");
exports.BCP47_UNKNOWN_LANG = epub_daisy_common_1.BCP47_UNKNOWN_LANG;
exports.mediaOverlayURLPath = epub_daisy_common_1.mediaOverlayURLPath;
exports.mediaOverlayURLParam = epub_daisy_common_1.mediaOverlayURLParam;
var addCoverDimensions = function (publication, coverLink) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var zipInternal, zip, coverLinkHrefDecoded, has, zipEntries, _i, zipEntries_1, zipEntry, zipStream, err_1, zipData, imageInfo, err_2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
zipInternal = publication.findFromInternal("zip");
if (!zipInternal) return [3, 11];
zip = zipInternal.Value;
coverLinkHrefDecoded = coverLink.HrefDecoded;
if (!coverLinkHrefDecoded) {
return [2];
}
return [4, (0, zipHasEntry_1.zipHasEntry)(zip, coverLinkHrefDecoded, coverLink.Href)];
case 1:
has = _a.sent();
if (!!has) return [3, 3];
debug("NOT IN ZIP (addCoverDimensions): ".concat(coverLink.Href, " --- ").concat(coverLinkHrefDecoded));
return [4, zip.getEntries()];
case 2:
zipEntries = _a.sent();
for (_i = 0, zipEntries_1 = zipEntries; _i < zipEntries_1.length; _i++) {
zipEntry = zipEntries_1[_i];
if (zipEntry.startsWith("__MACOSX/")) {
continue;
}
debug(zipEntry);
}
return [2];
case 3:
zipStream = void 0;
_a.label = 4;
case 4:
_a.trys.push([4, 6, , 7]);
return [4, zip.entryStreamPromise(coverLinkHrefDecoded)];
case 5:
zipStream = _a.sent();
return [3, 7];
case 6:
err_1 = _a.sent();
debug(coverLinkHrefDecoded);
debug(coverLink.TypeLink);
debug(err_1);
return [2];
case 7:
zipData = void 0;
_a.label = 8;
case 8:
_a.trys.push([8, 10, , 11]);
return [4, (0, BufferUtils_1.streamToBufferPromise)(zipStream.stream)];
case 9:
zipData = _a.sent();
imageInfo = (0, image_size_1.imageSize)(zipData);
if (imageInfo && imageInfo.width && imageInfo.height) {
coverLink.Width = imageInfo.width;
coverLink.Height = imageInfo.height;
if (coverLink.TypeLink &&
coverLink.TypeLink.replace("jpeg", "jpg").replace("+xml", "")
!== ("image/" + imageInfo.type)) {
debug("Wrong image type? ".concat(coverLink.TypeLink, " -- ").concat(imageInfo.type));
}
}
return [3, 11];
case 10:
err_2 = _a.sent();
debug(coverLinkHrefDecoded);
debug(coverLink.TypeLink);
debug(err_2);
return [3, 11];
case 11: return [2];
}
});
}); };
exports.addCoverDimensions = addCoverDimensions;
var EPUBis;
(function (EPUBis) {
EPUBis["LocalExploded"] = "LocalExploded";
EPUBis["LocalPacked"] = "LocalPacked";
EPUBis["RemoteExploded"] = "RemoteExploded";
EPUBis["RemotePacked"] = "RemotePacked";
})(EPUBis = exports.EPUBis || (exports.EPUBis = {}));
function isEPUBlication(urlOrPath) {
var p = urlOrPath;
var http = (0, UrlUtils_1.isHTTP)(urlOrPath);
if (http) {
var url = new url_1.URL(urlOrPath);
p = url.pathname;
}
else if (fs.existsSync(path.join(urlOrPath, "META-INF", "container.xml"))) {
return EPUBis.LocalExploded;
}
var fileName = path.basename(p);
var ext = path.extname(fileName);
var epub = /\.epub3?$/i.test(ext);
if (epub) {
return http ? EPUBis.RemotePacked : EPUBis.LocalPacked;
}
if (/META-INF[\/|\\]container.xml$/.test(p)) {
return http ? EPUBis.RemoteExploded : EPUBis.LocalExploded;
}
return undefined;
}
exports.isEPUBlication = isEPUBlication;
function EpubParsePromise(filePath) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var isAnEPUB, filePathToLoad, url, zip, err_3, publication, lcpl, lcplZipPath, has, lcplZipStream_, err_4, lcplZipStream, lcplZipData, err_5, lcplStr, lcplJson, mime, encryption, encZipPath, encryptionXmlZipStream_, err_6, encryptionXmlZipStream, encryptionXmlZipData, err_7, encryptionXmlStr, encryptionXmlDoc, containerZipPath, containerXmlZipStream_, err_8, containerXmlZipStream, containerXmlZipData, err_9, containerXmlStr, containerXmlDoc, container, rootfile, rootfilePathDecoded, opf, ex_1, ncx, ncxManItem, pageMapLink;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
isAnEPUB = isEPUBlication(filePath);
filePathToLoad = filePath;
if (isAnEPUB === EPUBis.LocalExploded) {
filePathToLoad = filePathToLoad.replace(/META-INF[\/|\\]container.xml$/, "");
}
else if (isAnEPUB === EPUBis.RemoteExploded) {
url = new url_1.URL(filePathToLoad);
url.pathname = url.pathname.replace(/META-INF[\/|\\]container.xml$/, "");
filePathToLoad = url.toString();
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4, (0, zipFactory_1.zipLoadPromise)(filePathToLoad)];
case 2:
zip = _a.sent();
return [3, 4];
case 3:
err_3 = _a.sent();
debug(err_3);
return [2, Promise.reject(err_3)];
case 4:
if (!zip.hasEntries()) {
return [2, Promise.reject("EPUB zip empty")];
}
publication = new publication_1.Publication();
publication.Context = ["https://readium.org/webpub-manifest/context.jsonld"];
publication.Metadata = new metadata_1.Metadata();
publication.Metadata.RDFType = "http://schema.org/Book";
publication.AddToInternal("filename", path.basename(filePath));
publication.AddToInternal("type", "epub");
publication.AddToInternal("zip", zip);
lcplZipPath = "META-INF/license.lcpl";
return [4, (0, zipHasEntry_1.zipHasEntry)(zip, lcplZipPath, undefined)];
case 5:
has = _a.sent();
if (!has) return [3, 14];
lcplZipStream_ = void 0;
_a.label = 6;
case 6:
_a.trys.push([6, 8, , 9]);
return [4, zip.entryStreamPromise(lcplZipPath)];
case 7:
lcplZipStream_ = _a.sent();
return [3, 9];
case 8:
err_4 = _a.sent();
debug(err_4);
return [2, Promise.reject(err_4)];
case 9:
lcplZipStream = lcplZipStream_.stream;
lcplZipData = void 0;
_a.label = 10;
case 10:
_a.trys.push([10, 12, , 13]);
return [4, (0, BufferUtils_1.streamToBufferPromise)(lcplZipStream)];
case 11:
lcplZipData = _a.sent();
return [3, 13];
case 12:
err_5 = _a.sent();
debug(err_5);
return [2, Promise.reject(err_5)];
case 13:
lcplStr = lcplZipData.toString("utf8");
lcplJson = global.JSON.parse(lcplStr);
lcpl = (0, serializable_1.TaJsonDeserialize)(lcplJson, lcp_1.LCP);
lcpl.ZipPath = lcplZipPath;
lcpl.JsonSource = lcplStr;
lcpl.init();
publication.LCP = lcpl;
mime = "application/vnd.readium.lcp.license.v1.0+json";
publication.AddLink(mime, ["license"], lcpl.ZipPath, undefined);
_a.label = 14;
case 14:
encZipPath = "META-INF/encryption.xml";
return [4, (0, zipHasEntry_1.zipHasEntry)(zip, encZipPath, undefined)];
case 15:
has = _a.sent();
if (!has) return [3, 24];
encryptionXmlZipStream_ = void 0;
_a.label = 16;
case 16:
_a.trys.push([16, 18, , 19]);
return [4, zip.entryStreamPromise(encZipPath)];
case 17:
encryptionXmlZipStream_ = _a.sent();
return [3, 19];
case 18:
err_6 = _a.sent();
debug(err_6);
return [2, Promise.reject(err_6)];
case 19:
encryptionXmlZipStream = encryptionXmlZipStream_.stream;
encryptionXmlZipData = void 0;
_a.label = 20;
case 20:
_a.trys.push([20, 22, , 23]);
return [4, (0, BufferUtils_1.streamToBufferPromise)(encryptionXmlZipStream)];
case 21:
encryptionXmlZipData = _a.sent();
return [3, 23];
case 22:
err_7 = _a.sent();
debug(err_7);
return [2, Promise.reject(err_7)];
case 23:
encryptionXmlStr = encryptionXmlZipData.toString("utf8");
encryptionXmlDoc = new xmldom.DOMParser().parseFromString(encryptionXmlStr);
encryption = xml_js_mapper_1.XML.deserialize(encryptionXmlDoc, encryption_1.Encryption);
encryption.ZipPath = encZipPath;
_a.label = 24;
case 24:
containerZipPath = "META-INF/container.xml";
_a.label = 25;
case 25:
_a.trys.push([25, 27, , 28]);
return [4, zip.entryStreamPromise(containerZipPath)];
case 26:
containerXmlZipStream_ = _a.sent();
return [3, 28];
case 27:
err_8 = _a.sent();
debug(err_8);
return [2, Promise.reject(err_8)];
case 28:
containerXmlZipStream = containerXmlZipStream_.stream;
_a.label = 29;
case 29:
_a.trys.push([29, 31, , 32]);
return [4, (0, BufferUtils_1.streamToBufferPromise)(containerXmlZipStream)];
case 30:
containerXmlZipData = _a.sent();
return [3, 32];
case 31:
err_9 = _a.sent();
debug(err_9);
return [2, Promise.reject(err_9)];
case 32:
containerXmlStr = containerXmlZipData.toString("utf8");
containerXmlDoc = new xmldom.DOMParser().parseFromString(containerXmlStr);
container = xml_js_mapper_1.XML.deserialize(containerXmlDoc, container_1.Container);
container.ZipPath = containerZipPath;
rootfile = container.Rootfile[0];
rootfilePathDecoded = rootfile.PathDecoded;
if (!rootfilePathDecoded) {
return [2, Promise.reject("?!rootfile.PathDecoded")];
}
return [4, (0, epub_daisy_common_1.getOpf)(zip, rootfilePathDecoded, rootfile.Path)];
case 33:
opf = _a.sent();
(0, epub_daisy_common_1.addLanguage)(publication, opf);
(0, epub_daisy_common_1.addTitle)(publication, rootfile, opf);
(0, epub_daisy_common_1.addIdentifier)(publication, opf);
(0, epub_daisy_common_1.addOtherMetadata)(publication, rootfile, opf);
(0, epub_daisy_common_1.setPublicationDirection)(publication, opf);
(0, epub_daisy_common_1.findContributorInMeta)(publication, rootfile, opf);
return [4, addRendition(publication, opf, zip)];
case 34:
_a.sent();
return [4, (0, epub_daisy_common_1.fillSpineAndResource)(publication, rootfile, opf, zip, addLinkData)];
case 35:
_a.sent();
return [4, addCoverRel(publication, rootfile, opf, zip)];
case 36:
_a.sent();
if (encryption) {
fillEncryptionInfo(publication, encryption, lcpl);
}
_a.label = 37;
case 37:
_a.trys.push([37, 39, , 40]);
return [4, fillTOCFromNavDoc(publication, zip)];
case 38:
_a.sent();
return [3, 40];
case 39:
ex_1 = _a.sent();
publication.TOC = [];
console.log(ex_1);
return [3, 40];
case 40:
if (!(!publication.TOC || !publication.TOC.length)) return [3, 43];
ncx = void 0;
if (!(opf.Manifest && opf.Spine.Toc)) return [3, 42];
ncxManItem = opf.Manifest.find(function (manifestItem) {
return manifestItem.ID === opf.Spine.Toc;
});
if (!ncxManItem) return [3, 42];
return [4, (0, epub_daisy_common_1.getNcx)(ncxManItem, opf, zip)];
case 41:
ncx = _a.sent();
_a.label = 42;
case 42:
(0, epub_daisy_common_1.fillTOC)(publication, opf, ncx);
_a.label = 43;
case 43:
if (!(!publication.PageList && publication.Resources)) return [3, 45];
pageMapLink = publication.Resources.find(function (item) {
return item.TypeLink === "application/oebps-page-map+xml";
});
if (!pageMapLink) return [3, 45];
return [4, fillPageListFromAdobePageMap(publication, zip, pageMapLink)];
case 44:
_a.sent();
_a.label = 45;
case 45:
fillCalibreSerieInfo(publication, opf);
(0, epub_daisy_common_1.fillSubject)(publication, opf);
(0, epub_daisy_common_1.fillPublicationDate)(publication, rootfile, opf);
return [2, publication];
}
});
});
}
exports.EpubParsePromise = EpubParsePromise;
function getAllMediaOverlays(publication) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var mos, links, _i, links_1, link, mo, err_10;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
mos = [];
links = [].
concat(publication.Spine ? publication.Spine : []).
concat(publication.Resources ? publication.Resources : []);
_i = 0, links_1 = links;
_a.label = 1;
case 1:
if (!(_i < links_1.length)) return [3, 7];
link = links_1[_i];
if (!link.MediaOverlays) return [3, 6];
mo = link.MediaOverlays;
if (!!mo.initialized) return [3, 5];
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4, (0, epub_daisy_common_1.lazyLoadMediaOverlays)(publication, mo)];
case 3:
_a.sent();
return [3, 5];
case 4:
err_10 = _a.sent();
return [2, Promise.reject(err_10)];
case 5:
mos.push(mo);
_a.label = 6;
case 6:
_i++;
return [3, 1];
case 7: return [2, Promise.resolve(mos)];
}
});
});
}
exports.getAllMediaOverlays = getAllMediaOverlays;
function getMediaOverlay(publication, spineHref) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var links, _i, links_2, link, mo, err_11;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
links = [].
concat(publication.Spine ? publication.Spine : []).
concat(publication.Resources ? publication.Resources : []);
_i = 0, links_2 = links;
_a.label = 1;
case 1:
if (!(_i < links_2.length)) return [3, 7];
link = links_2[_i];
if (!(link.MediaOverlays && link.Href.indexOf(spineHref) >= 0)) return [3, 6];
mo = link.MediaOverlays;
if (!!mo.initialized) return [3, 5];
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
return [4, (0, epub_daisy_common_1.lazyLoadMediaOverlays)(publication, mo)];
case 3:
_a.sent();
return [3, 5];
case 4:
err_11 = _a.sent();
return [2, Promise.reject(err_11)];
case 5: return [2, Promise.resolve(mo)];
case 6:
_i++;
return [3, 1];
case 7: return [2, Promise.reject("No Media Overlays ".concat(spineHref))];
}
});
});
}
exports.getMediaOverlay = getMediaOverlay;
var addRelAndPropertiesToLink = function (publication, link, linkEpub, opf) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var spineProperties;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!linkEpub.Properties) return [3, 2];
return [4, addToLinkFromProperties(publication, link, linkEpub.Properties)];
case 1:
_a.sent();
_a.label = 2;
case 2:
spineProperties = findPropertiesInSpineForManifest(linkEpub, opf);
if (!spineProperties) return [3, 4];
return [4, addToLinkFromProperties(publication, link, spineProperties)];
case 3:
_a.sent();
_a.label = 4;
case 4: return [2];
}
});
}); };
var addToLinkFromProperties = function (publication, link, propertiesString) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var properties, propertiesStruct, _i, properties_1, p, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
properties = (0, epub_daisy_common_1.parseSpaceSeparatedString)(propertiesString);
propertiesStruct = new metadata_properties_1.Properties();
_i = 0, properties_1 = properties;
_b.label = 1;
case 1:
if (!(_i < properties_1.length)) return [3, 34];
p = properties_1[_i];
_a = p;
switch (_a) {
case "cover-image": return [3, 2];
case "nav": return [3, 4];
case "scripted": return [3, 5];
case "mathml": return [3, 6];
case "onix-record": return [3, 7];
case "svg": return [3, 8];
case "xmp-record": return [3, 9];
case "remote-resources": return [3, 10];
case "rendition:page-spread-left": return [3, 11];
case "page-spread-left": return [3, 12];
case "rendition:page-spread-right": return [3, 13];
case "page-spread-right": return [3, 14];
case "rendition:page-spread-center": return [3, 15];
case "page-spread-center": return [3, 16];
case "rendition:spread-none": return [3, 17];
case "rendition:spread-auto": return [3, 18];
case "rendition:spread-landscape": return [3, 19];
case "rendition:spread-portrait": return [3, 20];
case "rendition:spread-both": return [3, 21];
case "rendition:layout-reflowable": return [3, 22];
case "rendition:layout-pre-paginated": return [3, 23];
case "rendition:orientation-auto": return [3, 24];
case "rendition:orientation-landscape": return [3, 25];
case "rendition:orientation-portrait": return [3, 26];
case "rendition:flow-auto": return [3, 27];
case "rendition:flow-paginated": return [3, 28];
case "rendition:flow-scrolled-continuous": return [3, 29];
case "rendition:flow-scrolled-doc": return [3, 30];
}
return [3, 31];
case 2:
link.AddRel("cover");
return [4, (0, exports.addCoverDimensions)(publication, link)];
case 3:
_b.sent();
return [3, 32];
case 4:
{
link.AddRel("contents");
return [3, 32];
}
_b.label = 5;
case 5:
{
if (!propertiesStruct.Contains) {
propertiesStruct.Contains = [];
}
propertiesStruct.Contains.push("js");
return [3, 32];
}
_b.label = 6;
case 6:
{
if (!propertiesStruct.Contains) {
propertiesStruct.Contains = [];
}
propertiesStruct.Contains.push("mathml");
return [3, 32];
}
_b.label = 7;
case 7:
{
if (!propertiesStruct.Contains) {
propertiesStruct.Contains = [];
}
propertiesStruct.Contains.push("onix");
return [3, 32];
}
_b.label = 8;
case 8:
{
if (!propertiesStruct.Contains) {
propertiesStruct.Contains = [];
}
propertiesStruct.Contains.push("svg");
return [3, 32];
}
_b.label = 9;
case 9:
{
if (!propertiesStruct.Contains) {
propertiesStruct.Contains = [];
}
propertiesStruct.Contains.push("xmp");
return [3, 32];
}
_b.label = 10;
case 10:
{
if (!propertiesStruct.Contains) {
propertiesStruct.Contains = [];
}
propertiesStruct.Contains.push("remote-resources");
return [3, 32];
}
_b.label = 11;
case 11:
{
propertiesStruct.Page = metadata_properties_1.PageEnum.Left;
return [3, 32];
}
_b.label = 12;
case 12:
{
propertiesStruct.Page = metadata_properties_1.PageEnum.Left;
return [3, 32];
}
_b.label = 13;
case 13:
{
propertiesStruct.Page = metadata_properties_1.PageEnum.Right;
return [3, 32];
}
_b.label = 14;
case 14:
{
propertiesStruct.Page = metadata_properties_1.PageEnum.Right;
return [3, 32];
}
_b.label = 15;
case 15:
{
propertiesStruct.Page = metadata_properties_1.PageEnum.Center;
return [3, 32];
}
_b.label = 16;
case 16:
{
propertiesStruct.Page = metadata_properties_1.PageEnum.Center;
return [3, 32];
}
_b.label = 17;
case 17:
{
propertiesStruct.Spread = metadata_properties_1.SpreadEnum.None;
return [3, 32];
}
_b.label = 18;
case 18:
{
propertiesStruct.Spread = metadata_properties_1.SpreadEnum.Auto;
return [3, 32];
}
_b.label = 19;
case 19:
{
propertiesStruct.Spread = metadata_properties_1.SpreadEnum.Landscape;
return [3, 32];
}
_b.label = 20;
case 20:
{
propertiesStruct.Spread = metadata_properties_1.SpreadEnum.Both;
return [3, 32];
}
_b.label = 21;
case 21:
{
propertiesStruct.Spread = metadata_properties_1.SpreadEnum.Both;
return [3, 32];
}
_b.label = 22;
case 22:
{
propertiesStruct.Layout = metadata_properties_1.LayoutEnum.Reflowable;
return [3, 32];
}
_b.label = 23;
case 23:
{
propertiesStruct.Layout = metadata_properties_1.LayoutEnum.Fixed;
return [3, 32];
}
_b.label = 24;
case 24:
{
propertiesStruct.Orientation = metadata_properties_1.OrientationEnum.Auto;
return [3, 32];
}
_b.label = 25;
case 25:
{
propertiesStruct.Orientation = metadata_properties_1.OrientationEnum.Landscape;
return [3, 32];
}
_b.label = 26;
case 26:
{
propertiesStruct.Orientation = metadata_properties_1.OrientationEnum.Portrait;
return [3, 32];
}
_b.label = 27;
case 27:
{
propertiesStruct.Overflow = metadata_properties_1.OverflowEnum.Auto;
return [3, 32];
}
_b.label = 28;
case 28:
{
propertiesStruct.Overflow = metadata_properties_1.OverflowEnum.Paginated;
return [3, 32];
}
_b.label = 29;
case 29:
{
propertiesStruct.Overflow = metadata_properties_1.OverflowEnum.ScrolledContinuous;
return [3, 32];
}
_b.label = 30;
case 30:
{
propertiesStruct.Overflow = metadata_properties_1.OverflowEnum.Scrolled;
return [3, 32];
}
_b.label = 31;
case 31:
{
return [3, 32];
}
_b.label = 32;
case 32:
if (propertiesStruct.Layout ||
propertiesStruct.Orientation ||
propertiesStruct.Overflow ||
propertiesStruct.Page ||
propertiesStruct.Spread ||
(propertiesStruct.Contains && propertiesStruct.Contains.length)) {
link.Properties = propertiesStruct;
}
_b.label = 33;
case 33:
_i++;
return [3, 1];
case 34: return [2];
}
});
}); };
var addMediaOverlay = function (link, linkEpub, opf, zip) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var meta, manItemSmil;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!linkEpub.MediaOverlay) return [3, 2];
meta = (0, epub_daisy_common_1.findMetaByRefineAndProperty)(opf, linkEpub.MediaOverlay, "media:duration");
if (meta) {
link.Duration = (0, media_overlay_1.timeStrToSeconds)(meta.Data);
}
manItemSmil = opf.Manifest.find(function (mi) {
if (mi.ID === linkEpub.MediaOverlay) {
return true;
}
return false;
});
if (!manItemSmil) return [3, 2];
return [4, (0, epub_daisy_common_1.addMediaOverlaySMIL)(link, manItemSmil, opf, zip)];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2];
}
});
}); };
var addRendition = function (publication, opf, zip) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var rendition_1, displayOptionsZipPath, has, displayOptionsZipStream_, err_12, displayOptionsZipStream, displayOptionsZipData, err_13, displayOptionsStr, displayOptionsDoc, displayOptions, renditionPlatformAll_1, renditionPlatformIpad_1, renditionPlatformIphone_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(opf.Metadata && opf.Metadata.Meta && opf.Metadata.Meta.length)) return [3, 15];
rendition_1 = new metadata_properties_1.Properties();
opf.Metadata.Meta.forEach(function (meta) {
switch (meta.Property) {
case "rendition:layout": {
switch (meta.Data) {
case "pre-paginated": {
rendition_1.Layout = metadata_properties_1.LayoutEnum.Fixed;
break;
}
case "reflowable": {
rendition_1.Layout = metadata_properties_1.LayoutEnum.Reflowable;
break;
}
}
break;
}
case "rendition:orientation": {
switch (meta.Data) {
case "auto": {
rendition_1.Orientation = metadata_properties_1.OrientationEnum.Auto;
break;
}
case "landscape": {
rendition_1.Orientation = metadata_properties_1.OrientationEnum.Landscape;
break;
}
case "portrait": {
rendition_1.Orientation = metadata_properties_1.OrientationEnum.Portrait;
break;
}
}
break;
}
case "rendition:spread": {
switch (meta.Data) {
case "auto": {
rendition_1.Spread = metadata_properties_1.SpreadEnum.Auto;
break;
}
case "both": {
rendition_1.Spread = metadata_properties_1.SpreadEnum.Both;
break;
}
case "none": {
rendition_1.Spread = metadata_properties_1.SpreadEnum.None;
break;
}
case "landscape": {
rendition_1.Spread = metadata_properties_1.SpreadEnum.Landscape;
break;
}
case "portrait": {
rendition_1.Spread = metadata_properties_1.SpreadEnum.Both;
break;
}
}
break;
}
case "rendition:flow": {
switch (meta.Data) {
case "auto": {
rendition_1.Overflow = metadata_properties_1.OverflowEnum.Auto;
break;
}
case "paginated": {
rendition_1.Overflow = metadata_properties_1.OverflowEnum.Paginated;
break;
}
case "scrolled": {
rendition_1.Overflow = metadata_properties_1.OverflowEnum.Scrolled;
break;
}
case "scrolled-continuous": {
rendition_1.Overflow = metadata_properties_1.OverflowEnum.ScrolledContinuous;
break;
}
}
break;
}
default: {
break;
}
}
});
if (!(!rendition_1.Layout || !rendition_1.Orientation)) return [3, 14];
displayOptionsZipPath = "META-INF/com.apple.ibooks.display-options.xml";
return [4, (0, zipHasEntry_1.zipHasEntry)(zip, displayOptionsZipPath, undefined)];
case 1:
has = _a.sent();
if (!has) return [3, 2];
debug("Info: found iBooks display-options XML");
return [3, 4];
case 2:
displayOptionsZipPath = "META-INF/com.kobobooks.display-options.xml";
return [4, (0, zipHasEntry_1.zipHasEntry)(zip, displayOptionsZipPath, undefined)];
case 3:
has = _a.sent();
if (has) {
debug("Info: found Kobo display-options XML");
}
_a.label = 4;
case 4:
if (!!has) return [3, 5];
debug("Info: not found iBooks or Kobo display-options XML");
return [3, 14];
case 5:
displayOptionsZipStream_ = void 0;
_a.label = 6;
case 6:
_a.trys.push([6, 8, , 9]);
return [4, zip.entryStreamPromise(displayOptionsZipPath)];
case 7:
displayOptionsZipStream_ = _a.sent();
return [3, 9];
case 8:
err_12 = _a.sent();
debug(err_12);
return [3, 9];
case 9:
if (!displayOptionsZipStream_) return [3, 14];
displayOptionsZipStream = displayOptionsZipStream_.stream;
displayOptionsZipData = void 0;
_a.label = 10;
case 10:
_a.trys.push([10, 12, , 13]);
return [4, (0, BufferUtils_1.streamToBufferPromise)(displayOptionsZipStream)];
case 11:
displayOptionsZipData = _a.sent();
return [3, 13];
case 12:
err_13 = _a.sent();
debug(err_13);
return [3, 13];
case 13:
if (displayOptionsZipData) {
try {
displayOptionsStr = displayOptionsZipData.toString("utf8");
displayOptionsDoc = new xmldom.DOMParser().parseFromString(displayOptionsStr);
displayOptions = xml_js_mapper_1.XML.deserialize(displayOptionsDoc, display_options_1.DisplayOptions);
displayOptions.ZipPath = displayOptionsZipPath;
if (displayOptions && displayOptions.Platforms) {
renditionPlatformAll_1 = new metadata_properties_1.Properties();
renditionPlatformIpad_1 = new metadata_properties_1.Properties();
renditionPlatformIphone_1 = new metadata_properties_1.Properties();
displayOptions.Platforms.forEach(function (platform) {
if (platform.Options) {
platform.Options.forEach(function (option) {
if (!rendition_1.Layout) {
if (option.Name === "fixed-layout") {
if (option.Value === "true") {
rendition_1.Layout = metadata_properties_1.LayoutEnum.Fixed;
}
else {
rendition_1.Layout = metadata_properties_1.LayoutEnum.Reflowable;
}
}
}
if (!rendition_1.Orientation) {
if (option.Name === "orientation-lock") {
var rend = platform.Name === "*" ? renditionPlatformAll_1 :
(platform.Name === "ipad" ? renditionPlatformIpad_1 :
(platform.Name === "iphone" ? renditionPlatformIphone_1 :
renditionPlatformAll_1));
switch (option.Value) {
case "none": {
rend.Orientation = metadata_properties_1.OrientationEnum.Auto;
break;
}
case "landscape-only": {
rend.Orientation = metadata_properties_1.OrientationEnum.Landscape;
break;
}
case "portrait-only": {
rend.Orientation = metadata_properties_1.OrientationEnum.Portrait;
break;
}
default: {
rend.Orientation = metadata_properties_1.OrientationEnum.Auto;
break;
}
}
}
}
});
}
});
if (renditionPlatformAll_1.Orientation) {
rendition_1.Orientation = renditionPlatformAll_1.Orientation;
}
else if (renditionPlatformIpad_1.Orientation) {
rendition_1.Orientation = renditionPlatformIpad_1.Orientation;
}
else if (renditionPlatformIphone_1.Orientation) {
rendition_1.Orientation = renditionPlatformIphone_1.Orientation;
}
}
}
catch (err) {
debug(err);
}
}
_a.label = 14;
case 14:
if (rendition_1.Layout || rendition_1.Orientation || rendition_1.Overflow || rendition_1.Page || rendition_1.Spread) {
publication.Metadata.Rendition = rendition_1;
}
_a.label = 15;
case 15: return [2];
}
});
}); };
var addLinkData = function (publication, rootfile, opf, zip, linkItem, item) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!rootfile) return [3, 2];
return [4, addRelAndPropertiesToLink(publication, linkItem, item, opf)];
case 1:
_a.sent();
_a.label = 2;
case 2: return [4, addMediaOverlay(linkItem, item, opf, zip)];
case 3:
_a.sent();
return [2];
}
});
}); };
var fillEncryptionInfo = function (publication, encryption, lcp) {
encryption.EncryptedData.forEach(function (encInfo) {
var encrypted = new metadata_encrypted_1.Encrypted();
encrypted.Algorithm = encInfo.EncryptionMethod.Algorithm;
if (lcp &&
encrypted.Algorithm !== "http://www.idpf.org/2008/embedding" &&
encrypted.Algorithm !== "http://ns.adobe.com/pdf/enc#RC") {
encrypted.Profile = lcp.Encryption.Profile;
encrypted.Scheme = "http://readium.org/2014/01/lcp";
}
if (encInfo.EncryptionProperties && encInfo.EncryptionProperties.length) {
encInfo.EncryptionProperties.forEach(function (prop) {
if (prop.Compression) {
if (prop.Compression.OriginalLength) {
encrypted.OriginalLength = parseFloat(prop.Compression.OriginalLength);
}
if (prop.Compression.Method === "8") {
encrypted.Compression = "deflate";
}
else {
encrypted.Compression = "none";
}
}
});
}
if (publication.Resources) {
publication.Resources.forEach(function (l) {
var filePath = l.Href;
if (filePath === (0, decodeURI_1.tryDecodeURI)(encInfo.CipherData.CipherReference.URI)) {
if (!l.Properties) {
l.Properties = new metadata_properties_1.Properties();
}
l.Properties.Encrypted = encrypted;
}
});
}
if (publication.Spine) {
publication.Spine.forEach(function (l) {
var filePath = l.Href;
if (filePath === (0, decodeURI_1.tryDecodeURI)(encInfo.CipherData.CipherReference.URI)) {
if (!l.Properties) {
l.Properties = new metadata_properties_1.Properties();
}
l.Properties.Encrypted = encrypted;
}
});
}
});
};
var fillPageListFromAdobePageMap = function (publication, zip, l) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var pageMapContent, pageMapXmlDoc, pages, i, page, link, href, title, hrefDecoded, zipPath;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!l.HrefDecoded) {
return [2];
}
return [4, (0, epub_daisy_common_1.loadFileStrFromZipPath)(l.Href, l.HrefDecoded, zip)];
case 1:
pageMapContent = _a.sent();
if (!pageMapContent) {
return [2];
}
pageMapXmlDoc = new xmldom.DOMParser().parseFromString(pageMapContent);
pages = pageMapXmlDoc.getElementsByTagName("page");
if (pages && pages.length) {
for (i = 0; i < pages.length; i += 1) {
page = pages.item(i);
if (!page) {
continue;
}
link = new publication_link_1.Link();
href = page.getAttribute("href");
title = page.getAttribute("name");
if (href === null || title === null) {
continue;
}
if (!publication.PageList) {
publication.PageList = [];
}
hrefDecoded = (0, decodeURI_1.tryDecodeURI)(href);
if (!hrefDecoded) {
continue;
}
zipPath = path.join(path.dirname(l.HrefDecoded), hrefDecoded)
.replace(/\\/g, "/");
link.setHrefDecoded(zipPath);
link.Title = title;
publication.PageList.push(link);
}
}
return [2];
}
});
}); };
var fillCalibreSerieInfo = function (publication, opf) {
var serie;
var seriePosition;
if (opf.Metadata && opf.Metadata.Meta && opf.Metadata.Meta.length) {
opf.Metadata.Meta.forEach(function (m) {
if (m.Name === "calibre:series") {
serie = m.Content;
}
if (m.Name === "calibre:series_index") {
seriePosition = parseFloat(m.Content);
}
});