Fix 404 errors in http mode #376 (#378)

This commit is contained in:
Jaifroid 2018-05-28 13:43:43 +01:00 committed by GitHub
parent 26112de66c
commit 95afdeecb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -807,10 +807,12 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
var regexpZIMUrlWithNamespace = /(?:^|\/)([-ABIJMUVWX]\/.+)/; var regexpZIMUrlWithNamespace = /(?:^|\/)([-ABIJMUVWX]\/.+)/;
// Pattern to match a local anchor in a href // Pattern to match a local anchor in a href
var regexpLocalAnchorHref = /^#/; var regexpLocalAnchorHref = /^#/;
// These regular expressions match both relative and absolute URLs // Regex below finds images, scripts and stylesheets with ZIM-type metadata and image namespaces [kiwix-js #378]
// Since late 2014, all ZIM files should use relative URLs // It first searches for <img, <script, or <link, then scans forward to find, on a word boundary, either src=["']
var regexpImageUrl = /^(?:\.\.\/|\/)+(I\/.*)$/; // OR href=["'] (ignoring any extra whitespace), and it then tests everything up to the next ["'] against a pattern that
var regexpMetadataUrl = /^(?:\.\.\/|\/)+(-\/.*)$/; // matches ZIM URLs with namespaces [-I] ("-" = metadata or "I" = image). Finally it removes the relative or absolute path.
// DEV: If you want to support more namespaces, add them to the END of the character set [-I] (not to the beginning)
var regexpTagsWithZimUrl = /(<(?:img|script|link)\s+[^>]*?\b)(?:src|href)(\s*=\s*["']\s*)(?:\.\.\/|\/)+([-I]\/[^"']*)/ig;
// Cache for CSS styles contained in ZIM. // Cache for CSS styles contained in ZIM.
// It significantly speeds up subsequent page display. See kiwix-js issue #335 // It significantly speeds up subsequent page display. See kiwix-js issue #335
@ -828,8 +830,9 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
$("#articleContent").contents().scrollTop(0); $("#articleContent").contents().scrollTop(0);
if (contentInjectionMode === 'jquery') { if (contentInjectionMode === 'jquery') {
// Fast-replace img src with data-kiwixsrc [kiwix-js #272] // Replaces ZIM-style URLs of img, script and link tags with a data-url to prevent 404 errors [kiwix-js #272 #376]
htmlArticle = htmlArticle.replace(/(<img\s+[^>]*\b)src(\s*=)/ig, '$1data-kiwixsrc$2'); // This replacement also processes the URL to remove the path so that the URL is ready for subsequent jQuery functions
htmlArticle = htmlArticle.replace(regexpTagsWithZimUrl, "$1data-kiwixurl$2$3");
} }
// Compute base URL // Compute base URL
@ -917,22 +920,18 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
} }
function loadImagesJQuery() { function loadImagesJQuery() {
$('#articleContent').contents().find('body').find('img').each(function() { $('#articleContent').contents().find('body').find('img[data-kiwixurl]').each(function() {
var image = $(this); var image = $(this);
// It's a standard image contained in the ZIM file var imageUrl = image.attr("data-kiwixurl");
// We try to find its name (from an absolute or relative URL) var title = decodeURIComponent(imageUrl);
var imageMatch = image.attr("data-kiwixsrc").match(regexpImageUrl); //kiwix-js #272 selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) {
if (imageMatch) { selectedArchive.readBinaryFile(dirEntry, function (fileDirEntry, content) {
var title = decodeURIComponent(imageMatch[1]); // TODO : use the complete MIME-type of the image (as read from the ZIM file)
selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) { uiUtil.feedNodeWithBlob(image, 'src', content, 'image');
selectedArchive.readBinaryFile(dirEntry, function (fileDirEntry, content) {
// TODO : use the complete MIME-type of the image (as read from the ZIM file)
uiUtil.feedNodeWithBlob(image, 'src', content, 'image');
});
}).fail(function (e) {
console.error("could not find DirEntry for image:" + title, e);
}); });
} }).fail(function (e) {
console.error("could not find DirEntry for image:" + title, e);
});
}); });
} }
@ -948,56 +947,48 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies','abstractFiles
collapsedBlocks[i].classList.add('open-block'); collapsedBlocks[i].classList.add('open-block');
} }
$('#articleContent').contents().find('link[rel=stylesheet]').each(function() { $('#articleContent').contents().find('link[data-kiwixurl]').each(function() {
var link = $(this); var link = $(this);
// We try to find its name (from an absolute or relative URL) var linkUrl = link.attr("data-kiwixurl");
var hrefMatch = link.attr("href").match(regexpMetadataUrl); var title = uiUtil.removeUrlParameters(decodeURIComponent(linkUrl));
if (hrefMatch) { if (cssCache && cssCache.has(title)) {
// It's a CSS file contained in the ZIM file var cssContent = cssCache.get(title);
var title = uiUtil.removeUrlParameters(decodeURIComponent(hrefMatch[1])); uiUtil.replaceCSSLinkWithInlineCSS(link, cssContent);
if (cssCache && cssCache.has(title)) { } else {
var cssContent = cssCache.get(title); selectedArchive.getDirEntryByTitle(title)
uiUtil.replaceCSSLinkWithInlineCSS(link, cssContent); .then(function (dirEntry) {
} else { return selectedArchive.readUtf8File(dirEntry,
selectedArchive.getDirEntryByTitle(title) function (fileDirEntry, content) {
.then(function (dirEntry) { var fullUrl = fileDirEntry.namespace + "/" + fileDirEntry.url;
return selectedArchive.readUtf8File(dirEntry, if (cssCache) cssCache.set(fullUrl, content);
function (fileDirEntry, content) { uiUtil.replaceCSSLinkWithInlineCSS(link, content);
var fullUrl = fileDirEntry.namespace + "/" + fileDirEntry.url; }
if (cssCache) cssCache.set(fullUrl, content); );
uiUtil.replaceCSSLinkWithInlineCSS(link, content); }).fail(function (e) {
} console.error("could not find DirEntry for CSS : " + title, e);
); });
}).fail(function (e) {
console.error("could not find DirEntry for CSS : " + title, e);
});
}
} }
}); });
} }
function loadJavaScriptJQuery() { function loadJavaScriptJQuery() {
$('#articleContent').contents().find('script').each(function() { $('#articleContent').contents().find('script[data-kiwixurl]').each(function() {
var script = $(this); var script = $(this);
// We try to find its name (from an absolute or relative URL) var scriptUrl = script.attr("data-kiwixurl");
var srcMatch = script.attr("src") ? script.attr("src").match(regexpMetadataUrl) : null;
// TODO check that the type of the script is text/javascript or application/javascript // TODO check that the type of the script is text/javascript or application/javascript
if (srcMatch) { var title = uiUtil.removeUrlParameters(decodeURIComponent(scriptUrl));
// It's a Javascript file contained in the ZIM file selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) {
var title = uiUtil.removeUrlParameters(decodeURIComponent(srcMatch[1])); if (dirEntry === null) {
selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) { console.log("Error: js file not found: " + title);
if (dirEntry === null) { } else {
console.log("Error: js file not found: " + title); selectedArchive.readBinaryFile(dirEntry, function (fileDirEntry, content) {
} else { // TODO : JavaScript support not yet functional [kiwix-js #152]
selectedArchive.readBinaryFile(dirEntry, function (fileDirEntry, content) { uiUtil.feedNodeWithBlob(script, 'src', content, 'text/javascript');
// TODO : JavaScript support not yet functional [kiwix-js #152] });
uiUtil.feedNodeWithBlob(script, 'src', content, 'text/javascript'); }
}); }).fail(function (e) {
} console.error("could not find DirEntry for javascript : " + title, e);
}).fail(function (e) { });
console.error("could not find DirEntry for javascript : " + title, e);
});
}
}); });
} }
} }