Skip to content

Commit

Permalink
Final cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaifroid committed Feb 24, 2020
1 parent b880e6a commit b1f3d6a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
6 changes: 3 additions & 3 deletions www/js/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ define(['q'], function(Q) {
* @returns {Promise<Uint8Array>} A Promise for an array buffer with the read data
*/
function readFileSlice(file, begin, size) {
return Q.Promise(function(resolve, reject) {
return Q.Promise(function (resolve, reject) {
var reader = new FileReader();
reader.onload = function(e) {
reader.onload = function (e) {
resolve(new Uint8Array(e.target.result));
};
reader.onerror = reader.onabort = function(e) {
reader.onerror = reader.onabort = function (e) {
reject(e);
};
reader.readAsArrayBuffer(file.slice(begin, begin + size));
Expand Down
37 changes: 18 additions & 19 deletions www/js/lib/zimfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,21 @@ define(['xzdec_wrapper', 'util', 'utf8', 'q', 'zimDirEntry'], function(xz, util,
* and is stored as ZIMFile.mimeTypes
*
* @param {File} file The ZIM file (or first file in array of files) from which the MIME type list
* is to be extracted
* is to be extracted
* @param {Integer} mimeListPos The offset in <file> at which the MIME type list is found
* @param {Integer} urlPtrPos The offset of the byte after the end of the MIME type list in <file>
* @returns {Promise} A promise for the MIME Type list as a Map
*/
function readMimetypeMap(file, mimeListPos, urlPtrPos) {
var typeMap = new Map;
var size = urlPtrPos - mimeListPos;
return util.readFileSlice(file, mimeListPos, size).then(function(data) {
return util.readFileSlice(file, mimeListPos, size).then(function (data) {
if (data.subarray) {
var i = 0;
var pos = -1;
var mimeString;
while (pos < size) {
pos++;
pos++;
mimeString = utf8.parse(data.subarray(pos), true);
// If the parsed data is an empty string, we have reached the end of the MIME type list, so break
if (!mimeString) break;
Expand All @@ -241,35 +241,34 @@ define(['xzdec_wrapper', 'util', 'utf8', 'q', 'zimDirEntry'], function(xz, util,
}
}
return typeMap;
}).catch(function(err) {
}).catch(function (err) {
console.error('Unable to read MIME type list', err);
return new Map;
});
}

return {
/**
*
* @param {Array.<File>} fileArray
* @returns {Promise}
* @param {Array.<File>} fileArray An array of picked archive files
* @returns {Promise<Object>} A Promise for the ZimFile Object
*/
fromFileArray: function(fileArray) {
fromFileArray: function (fileArray) {
// Array of blob objects should be sorted by their name property
fileArray.sort(function(a, b) {
var nameA = a.name.toUpperCase();
var nameB = b.name.toUpperCase();
if (nameA < nameB) {
fileArray.sort(function (a, b) {
var nameA = a.name.toUpperCase();
var nameB = b.name.toUpperCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
}
if (nameA > nameB) {
return 1;
}
return 0;
}
return 0;
});
return util.readFileSlice(fileArray[0], 0, 80).then(function(header) {
return util.readFileSlice(fileArray[0], 0, 80).then(function (header) {
var mimeListPos = readInt(header, 56, 8);
var urlPtrPos = readInt(header, 32, 8);
return readMimetypeMap(fileArray[0], mimeListPos, urlPtrPos).then(function(data) {
return readMimetypeMap(fileArray[0], mimeListPos, urlPtrPos).then(function (data) {
var zf = new ZIMFile(fileArray);
zf.articleCount = readInt(header, 24, 4);
zf.clusterCount = readInt(header, 28, 4);
Expand Down

0 comments on commit b1f3d6a

Please sign in to comment.