Fix for images, to reflect the need of the namespace to read in the ZIM backend.

This commit is contained in:
mossroy 2016-01-07 19:03:48 +01:00
parent 71b1b2a9b5
commit 6b9f70f300
3 changed files with 12 additions and 9 deletions

View File

@ -90,6 +90,7 @@ function(util) {
event.respondWith(new Promise(function(resolve, reject) {
var nameSpace;
var titleName;
var titleNameWithNameSpace;
var contentType;
if (regexpContentUrlWithoutNamespace.test(event.request.url)) {
// When the request URL is in the same folder,
@ -141,11 +142,13 @@ function(util) {
return;
}
titleNameWithNameSpace = nameSpace + '/' + titleName;
// Let's instanciate a new messageChannel, to allow app.s to give us the content
var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) {
if (event.data.action === 'giveContent') {
console.log('content message received for ' + titleName, event.data);
console.log('content message received for ' + titleNameWithNameSpace, event.data);
var responseInit = {
status: 200,
statusText: 'OK',
@ -156,16 +159,16 @@ function(util) {
var httpResponse = new Response(event.data.content, responseInit);
console.log('ServiceWorker responding to the HTTP request for ' + titleName + ' (size=' + event.data.content.length + ' octets)' , httpResponse);
console.log('ServiceWorker responding to the HTTP request for ' + titleNameWithNameSpace + ' (size=' + event.data.content.length + ' octets)' , httpResponse);
resolve(httpResponse);
}
else {
console.log('Invalid message received from app.js for ' + titleName, event.data);
console.log('Invalid message received from app.js for ' + titleNameWithNameSpace, event.data);
reject(event.data);
}
};
console.log('Eventlistener added to listen for an answer to ' + titleName);
outgoingMessagePort.postMessage({'action': 'askForContent', 'titleName': titleName}, [messageChannel.port2]);
console.log('Eventlistener added to listen for an answer to ' + titleNameWithNameSpace);
outgoingMessagePort.postMessage({'action': 'askForContent', 'titleName': titleNameWithNameSpace}, [messageChannel.port2]);
console.log('Message sent to app.js through outgoingMessagePort');
}));
}

View File

@ -531,7 +531,7 @@ define(['jquery', 'title', 'archive', 'zimArchive', 'zimDirEntry', 'util', 'geom
});
});
});
asyncTest("Image 's/style.css' can be loaded", function() {
asyncTest("Stylesheet 's/style.css' can be loaded", function() {
expect(4);
localZimArchive.getTitleByName("-/s/style.css").then(function(title) {
ok(title !== null, "Title found");

View File

@ -201,7 +201,7 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'],
});
};
var regexpTitleNameWithNamespaceA = /^A\//;
var regexpTitleNameWithoutNameSpace = /^[^\/]+$/;
/**
* Searches a title (article / page) by name.
@ -210,8 +210,8 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'],
*/
ZIMArchive.prototype.getTitleByName = function(titleName) {
var that = this;
// If the namespace is not mentioned, we have to add it
if (!regexpTitleNameWithNamespaceA.test(titleName)) {
// If no namespace is mentioned, it's an article, and we have to add it
if (regexpTitleNameWithoutNameSpace.test(titleName)) {
titleName= "A/" + titleName;
}
return util.binarySearch(0, this._file.articleCount, function(i) {