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) { event.respondWith(new Promise(function(resolve, reject) {
var nameSpace; var nameSpace;
var titleName; var titleName;
var titleNameWithNameSpace;
var contentType; var contentType;
if (regexpContentUrlWithoutNamespace.test(event.request.url)) { if (regexpContentUrlWithoutNamespace.test(event.request.url)) {
// When the request URL is in the same folder, // When the request URL is in the same folder,
@ -140,12 +141,14 @@ function(util) {
resolve(httpResponse); resolve(httpResponse);
return; return;
} }
titleNameWithNameSpace = nameSpace + '/' + titleName;
// Let's instanciate a new messageChannel, to allow app.s to give us the content // Let's instanciate a new messageChannel, to allow app.s to give us the content
var messageChannel = new MessageChannel(); var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) { messageChannel.port1.onmessage = function(event) {
if (event.data.action === 'giveContent') { 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 = { var responseInit = {
status: 200, status: 200,
statusText: 'OK', statusText: 'OK',
@ -156,16 +159,16 @@ function(util) {
var httpResponse = new Response(event.data.content, responseInit); 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); resolve(httpResponse);
} }
else { 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); reject(event.data);
} }
}; };
console.log('Eventlistener added to listen for an answer to ' + titleName); console.log('Eventlistener added to listen for an answer to ' + titleNameWithNameSpace);
outgoingMessagePort.postMessage({'action': 'askForContent', 'titleName': titleName}, [messageChannel.port2]); outgoingMessagePort.postMessage({'action': 'askForContent', 'titleName': titleNameWithNameSpace}, [messageChannel.port2]);
console.log('Message sent to app.js through outgoingMessagePort'); 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); expect(4);
localZimArchive.getTitleByName("-/s/style.css").then(function(title) { localZimArchive.getTitleByName("-/s/style.css").then(function(title) {
ok(title !== null, "Title found"); 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. * Searches a title (article / page) by name.
@ -210,8 +210,8 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'],
*/ */
ZIMArchive.prototype.getTitleByName = function(titleName) { ZIMArchive.prototype.getTitleByName = function(titleName) {
var that = this; var that = this;
// If the namespace is not mentioned, we have to add it // If no namespace is mentioned, it's an article, and we have to add it
if (!regexpTitleNameWithNamespaceA.test(titleName)) { if (regexpTitleNameWithoutNameSpace.test(titleName)) {
titleName= "A/" + titleName; titleName= "A/" + titleName;
} }
return util.binarySearch(0, this._file.articleCount, function(i) { return util.binarySearch(0, this._file.articleCount, function(i) {