Skip to content

Commit

Permalink
Fix again the bug that cannot load unzipped EPUB when directory listi…
Browse files Browse the repository at this point in the history
…ng is enabled on server

The previous fix 6d741b3 has problem that always try to load "{url}/META-INF/container.xml" before check to load the url that may be an (X)HTML document.
  • Loading branch information
MurakamiShinyu committed Mar 9, 2019
1 parent 017839a commit 6cbe824
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
82 changes: 57 additions & 25 deletions src/adapt/epub.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,33 +110,17 @@ adapt.epub.EPUBDocStore.prototype.startLoadingAsJSON = function(url) {
* @param {boolean} haveZipMetadata
* @return {!adapt.task.Result.<adapt.epub.OPFDoc>}
*/
adapt.epub.EPUBDocStore.prototype.loadPUBDoc = function(url, haveZipMetadata) {
adapt.epub.EPUBDocStore.prototype.loadPubDoc = function(url, haveZipMetadata) {
/** @type {!adapt.task.Frame.<adapt.epub.OPFDoc>} */ const frame
= adapt.task.newFrame("loadPUBDoc");
= adapt.task.newFrame("loadPubDoc");

adapt.net.ajax(url, null, "HEAD").then(response => {
if (response.status >= 400) {
// This url can be the root of an unzipped EPUB.
let pubURL = url;
if (pubURL.substring(pubURL.length - 1) !== "/") {
pubURL = `${pubURL}/`;
}
if (haveZipMetadata) {
this.startLoadingAsJSON(`${pubURL}?r=list`);
}
this.startLoadingAsPlainXML(`${pubURL}META-INF/encryption.xml`);
const containerURL = `${pubURL}META-INF/container.xml`;
this.loadAsPlainXML(containerURL).then(containerXML => {
if (containerXML) {
const roots = containerXML.doc().child("container").child("rootfiles")
.child("rootfile").attribute("full-path");

for (const root of roots) {
if (root) {
this.loadOPF(pubURL, root, haveZipMetadata).thenFinish(frame);
return;
}
}
this.loadEPUBDoc(url, haveZipMetadata).then(opf => {
if (opf) {
frame.finish(opf);
return;
}
vivliostyle.logging.logger.error(`Failed to fetch a source document from ${url} (${response.status}${response.statusText ? ' ' + response.statusText : ''})`);
frame.finish(null);
Expand Down Expand Up @@ -174,13 +158,59 @@ adapt.epub.EPUBDocStore.prototype.loadPUBDoc = function(url, haveZipMetadata) {
});
} else {
// Web Publication primary entry (X)HTML
this.loadWebPub(url).thenFinish(frame);
this.loadWebPub(url).then(opf => {
if (opf) {
frame.finish(opf);
return;
}
// This url can be the root of an unzipped EPUB.
this.loadEPUBDoc(url, haveZipMetadata).then(opf => {
if (opf) {
frame.finish(opf);
return;
}
vivliostyle.logging.logger.error(`Failed to load ${url}.`);
frame.finish(null);
});
});
}
}
});
return frame.result();
};

/**
* @param {string} url
* @param {boolean} haveZipMetadata
* @return {!adapt.task.Result.<adapt.epub.OPFDoc>}
*/
adapt.epub.EPUBDocStore.prototype.loadEPUBDoc = function(url, haveZipMetadata) {
/** @type {!adapt.task.Frame.<adapt.epub.OPFDoc>} */ const frame
= adapt.task.newFrame("loadEPUBDoc");
if (!url.endsWith("/")) {
url = url + "/";
}
if (haveZipMetadata) {
this.startLoadingAsJSON(url + "?r=list");
}
this.startLoadingAsPlainXML(url + "META-INF/encryption.xml");
const containerURL = url + "META-INF/container.xml";
this.loadAsPlainXML(containerURL).then(containerXML => {
if (containerXML) {
const roots = containerXML.doc().child("container").child("rootfiles")
.child("rootfile").attribute("full-path");
for (const root of roots) {
if (root) {
this.loadOPF(url, root, haveZipMetadata).thenFinish(frame);
return;
}
}
}
frame.finish(null);
});
return frame.result();
};

/**
* @param {string} pubURL
* @param {string} root
Expand Down Expand Up @@ -229,8 +259,10 @@ adapt.epub.EPUBDocStore.prototype.loadWebPub = function(url) {
this.load(url).then(xmldoc => {
if (!xmldoc) {
vivliostyle.logging.logger.error(`Received an empty response for ${url}. This may be caused by the server not allowing cross-origin resource sharing (CORS).`);
}
else {
} else if (xmldoc.document.querySelector("a[href='META-INF/']")) {
// This is likely the directory listing of unzipped EPUB top directory
frame.finish(null);
} else {
const doc = xmldoc.document;
const opf = new adapt.epub.OPFDoc(this, url);

Expand Down
2 changes: 1 addition & 1 deletion src/adapt/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ adapt.viewer.Viewer.prototype.loadPublication = function(command) {
store.init(authorStyleSheet, userStyleSheet).then(() => {
const pubURL = adapt.base.resolveURL(adapt.base.convertSpecialURL(url), self.window.location.href);
self.packageURL = [pubURL];
store.loadPUBDoc(pubURL, haveZipMetadata).then(opf => {
store.loadPubDoc(pubURL, haveZipMetadata).then(opf => {
if (opf) {
self.opf = opf;
self.render(fragment).then(() => {
Expand Down

0 comments on commit 6cbe824

Please sign in to comment.