kiwix-js-pwa/www/js/lib/zimArchiveLoader.js
Jaifroid 55d3cdf311 Refactor zimit transforms (#253)
Former-commit-id: 8c57cc6c615bd60aa845e61f8636d3084f5878fa [formerly f5d148317fa24622a0ffe613e08885a5c851eb6a] [formerly 456cd8743102ec76d2cce1da0c2dbb2901c4f660] [formerly f18a7d19c99ff628cfde607eba6d1df5e7b2b221 [formerly a2683ae1c3f7649f40bd50e8cfe6522c2d04a10a [formerly c26be4ebd676181ece94e7a939af0f189b6e50d1]]]
Former-commit-id: 1ff7777ea29a4b5325e8b8eec85029e08a944cff [formerly 3c18e16e01ec49c134f5115ec2e0ac75ca5d96e2 [formerly 0edf0ad6d3d65b08c1c32aa54c2f7adea2761dc8]]
Former-commit-id: 2c65468cba9ee13c8d7c95fc1d9d2c66b0837d01 [formerly 6cb4d73d4f10598fe5626687379114613b43d96d]
Former-commit-id: b6920babef86b9eb085a7adcb6f7feb541ccb1b4
2022-04-28 00:11:09 +01:00

92 lines
3.4 KiB
JavaScript

/**
* zimArchiveLoader.js: Functions to search and read ZIM archives.
*
* Copyright 2015 Mossroy and contributors
* License GPL v3:
*
* This file is part of Kiwix.
*
* Kiwix is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Kiwix is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kiwix (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/
'use strict';
define(['zimArchive', 'jquery'],
function(zimArchive, jQuery) {
/**
* Create a ZIMArchive from DeviceStorage location
* @param {DeviceStorage} storage
* @param {String} path
* @param {callbackZIMArchive} callbackReady
* @param {callbackZIMArchive} callbackError
* @returns {ZIMArchive}
*/
function loadArchiveFromDeviceStorage(storage, path, callbackReady, callbackError) {
return new zimArchive.ZIMArchive(storage, path, callbackReady, callbackError);
};
/**
* Create a ZIMArchive from Files
* @param {Array.<File>} files
* @param {callbackZIMArchive} callbackReady
* @param {callbackZIMArchive} callbackError
* @returns {ZIMArchive}
*/
function loadArchiveFromFiles(files, callbackReady, callbackError) {
if (files.length >= 1) {
return new zimArchive.ZIMArchive(files, null, callbackReady, callbackError);
}
};
/**
* @callback callbackPathList
* @param {Array.<String>} directoryList List of directories
*/
/**
* Scans the DeviceStorage for archives
*
* @param {Array.<DeviceStorage>} storages List of DeviceStorage instances
* @param {callbackPathList} callbackFunction Function to call with the list of directories where archives are found
* @param {callbackPathList} callbackError Function to call in case of an error
*/
function scanForArchives(storages, callbackFunction, callbackError) {
var directories = [];
var promises = jQuery.map(storages, function(storage) {
return storage.scanForArchives()
.then(function(dirs) {
jQuery.merge(directories, dirs);
return true;
});
});
jQuery.when.apply(null, promises).then(function() {
callbackFunction(directories);
}).catch(function (error) {
callbackError("Error scanning your device storage : " + error
+ ". If you're using the Firefox OS Simulator, please put the archives in "
+ "a 'fake-sdcard' directory inside your Firefox profile "
+ "(ex : ~/.mozilla/firefox/xxxx.default/extensions/fxos_2_x_simulator@mozilla.org/"
+ "profile/fake-sdcard/wikipedia_en_ray_charles_2015-06.zim)", "Error reading Device Storage");
});
};
/**
* Functions and classes exposed by this module
*/
return {
loadArchiveFromDeviceStorage: loadArchiveFromDeviceStorage,
loadArchiveFromFiles: loadArchiveFromFiles,
scanForArchives: scanForArchives
};
});