mirror of
https://github.com/kiwix/kiwix-js-pwa.git
synced 2025-09-11 13:18:21 -04:00

Former-commit-id: c68596bc74e187de64dc2e39e43eadcf1f9ea32f [formerly fa761722a44f952ce2dd17b8c9944b2b4f83adbf] [formerly f3cc4ecfb825af85e764ab89d1b350cdaca4b5e2] [formerly 2ce10066e6d7b53aa1390d7e75189f18c7b59b8a [formerly e7fe66c76d904f511bfa246c059dade2d12911b6 [formerly 40d3b58c2cf6821c94cb138f86d45c498698beed]]] Former-commit-id: 375cfb19d2c3ca7fcda82adff21837dcaecc2e82 [formerly a9650d1a4d00e1bc0ad8c82cb360c7c79cae2173 [formerly 7d6ea7d5e4332e09c8aa04e00d50c402a6ae0d16]] Former-commit-id: cba19c61686b992d81f8736997ed56ad669ea3cd [formerly 34f89f43b796b52dc5ff580d4f481a7d1b5ef7fc] Former-commit-id: a769646441e4eb51a6ee4485f594d46a98d1ce93
108 lines
3.5 KiB
JavaScript
108 lines
3.5 KiB
JavaScript
/**
|
|
* abstractFilesystemAccess.js: Abstraction layer for file access.
|
|
* This is currently only implemented for FirefoxOS, but could be extended to
|
|
* Cordova, Electron or other ways to directly browse and read files from the
|
|
* filesystem.
|
|
* It is unfortunately not possible to do that inside a standard browser
|
|
* (even inside an extension).
|
|
*
|
|
* Copyright 2014 Kiwix developers
|
|
* 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([], function() {
|
|
|
|
/**
|
|
* Storage implemented by Firefox OS
|
|
*
|
|
* @typedef StorageFirefoxOS
|
|
* @property {DeviceStorage} _storage DeviceStorage
|
|
* @property {String} storageName Name of the storage
|
|
*/
|
|
|
|
/**
|
|
* Creates an abstraction layer around the FirefoxOS storage.
|
|
* @param storage FirefoxOS DeviceStorage object
|
|
*/
|
|
function StorageFirefoxOS(storage) {
|
|
this._storage = storage;
|
|
this.storageName = storage.storageName;
|
|
};
|
|
/**
|
|
* Access the given file.
|
|
* @param {String} path absolute path to the file
|
|
* @return {Promise} Promise which is resolved with a HTML5 file object and
|
|
* rejected with an error message.
|
|
*/
|
|
StorageFirefoxOS.prototype.get = function(path) {
|
|
var that = this;
|
|
return new Promise(function (resolve, reject){
|
|
var request = that._storage.get(path);
|
|
request.onsuccess = function() { resolve(this.result); };
|
|
request.onerror = function() { reject(this.error.name); };
|
|
});
|
|
};
|
|
|
|
// We try to match both a standalone ZIM file (.zim) or
|
|
// the first file of a split ZIM files collection (.zimaa)
|
|
var regexpZIMFileName = /\.zim(aa)?$/i;
|
|
|
|
/**
|
|
* Searches for archive files or directories.
|
|
* @return {Promise} Promise which is resolved with an array of
|
|
* paths and rejected with an error message.
|
|
*/
|
|
StorageFirefoxOS.prototype.scanForArchives = function() {
|
|
var that = this;
|
|
return new Promise(function (resolve, reject){
|
|
var directories = [];
|
|
var cursor = that._storage.enumerate();
|
|
cursor.onerror = function() {
|
|
reject(cursor.error);
|
|
};
|
|
cursor.onsuccess = function() {
|
|
if (!cursor.result) {
|
|
resolve(directories);
|
|
return;
|
|
}
|
|
var file = cursor.result;
|
|
|
|
if (regexpZIMFileName.test(file.name)) {
|
|
directories.push(file.name);
|
|
}
|
|
|
|
cursor.continue();
|
|
};
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Browse a path through DeviceStorage API
|
|
* @param path Path where to look for files
|
|
* @return {DOMCursor} Cursor of files found in given path
|
|
*/
|
|
StorageFirefoxOS.prototype.enumerate = function(path) {
|
|
return this._storage.enumerate();
|
|
};
|
|
|
|
|
|
return {
|
|
StorageFirefoxOS: StorageFirefoxOS
|
|
};
|
|
});
|