kiwix-js-pwa/www/js/lib/abstractFilesystemAccess.js
Jaifroid cb15c17438 Adopt changes from Kiwix JS for MIME type map
Former-commit-id: f8f1f27226d73ebd6ad158d8ce99235e73fe11f1 [formerly 8f8c60e1f9aefada72a7fb81522f8599aef7d18d]
Former-commit-id: 8c1d6f14aa2d8cfb5dea16ee98a5274dc05a7353
2019-05-17 18:37:00 +01:00

106 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(['q', 'jquery'], function(q, jQuery) {
/**
* 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 deferred = q.defer();
var request = this._storage.get(path);
request.onsuccess = function() { deferred.resolve(this.result); };
request.onerror = function() { deferred.reject(this.error.name); };
return deferred.promise;
};
// 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 deferred = jQuery.Deferred();
var directories = [];
var cursor = this._storage.enumerate();
cursor.onerror = function() {
deferred.reject(cursor.error);
};
cursor.onsuccess = function() {
if (!cursor.result) {
deferred.resolve(directories);
return;
}
var file = cursor.result;
if (regexpZIMFileName.test(file.name)) {
directories.push(file.name);
}
cursor.continue();
};
return deferred.promise();
};
/**
* 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
};
});