mirror of
https://github.com/kiwix/kiwix-js.git
synced 2025-09-24 04:54:51 -04:00
if getDeviceStorages() is available (FxOS >=1.1), browse all the device storages for archives, instead of the default one (given by getDeviceStorage())
Fixes #62
This commit is contained in:
parent
1dbfdaf5e1
commit
a98090ffa9
@ -232,15 +232,46 @@ define(function(require) {
|
||||
|
||||
/**
|
||||
* Initialize the localArchive from given directory, using DeviceStorage
|
||||
* @param {type} storage
|
||||
* @param {type} storages List of DeviceStorages available
|
||||
* @param {type} archiveDirectory
|
||||
*/
|
||||
LocalArchive.prototype.initializeFromDeviceStorage = function(storage, archiveDirectory) {
|
||||
this.readTitleFilesFromStorage(storage, archiveDirectory);
|
||||
this.readDataFilesFromStorage(storage, archiveDirectory, 0);
|
||||
this.readMathFilesFromStorage(storage, archiveDirectory);
|
||||
this.readMetadataFileFromStorage(storage, archiveDirectory);
|
||||
this.readCoordinateFilesFromStorage(storage, archiveDirectory, 0);
|
||||
LocalArchive.prototype.initializeFromDeviceStorage = function(storages, archiveDirectory) {
|
||||
// First, we have to find which DeviceStorage has been selected by the user
|
||||
// It is the prefix of the archive directory
|
||||
var storageNameRegex = /^\/([^\/]+)\//;
|
||||
var regexResults = storageNameRegex.exec(archiveDirectory);
|
||||
var selectedStorage = null;
|
||||
if (regexResults && regexResults.length>0) {
|
||||
var selectedStorageName = regexResults[1];
|
||||
for (var i=0; i<storages.length; i++) {
|
||||
var storage = storages[i];
|
||||
if (selectedStorageName === storage.storageName) {
|
||||
// We found the selected storage
|
||||
selectedStorage = storage;
|
||||
}
|
||||
}
|
||||
if (selectedStorage === null) {
|
||||
alert("Unable to find which device storage corresponds to directory " + archiveDirectory);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// This happens with FxOS 1.0
|
||||
// In this case, we use the first storage of the list
|
||||
// (there should be only one)
|
||||
if (storages.length === 1) {
|
||||
selectedStorage = storages[0];
|
||||
}
|
||||
else {
|
||||
alert("Something weird happened with the DeviceStorage API : found a directory without prefix : "
|
||||
+ archiveDirectory + ", but there were " + storages.length
|
||||
+ " storages found with getDeviceStorages instead of 1");
|
||||
}
|
||||
}
|
||||
this.readTitleFilesFromStorage(selectedStorage, archiveDirectory);
|
||||
this.readDataFilesFromStorage(selectedStorage, archiveDirectory, 0);
|
||||
this.readMathFilesFromStorage(selectedStorage, archiveDirectory);
|
||||
this.readMetadataFileFromStorage(selectedStorage, archiveDirectory);
|
||||
this.readCoordinateFilesFromStorage(selectedStorage, archiveDirectory, 0);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -684,12 +715,12 @@ define(function(require) {
|
||||
/**
|
||||
* Scans the DeviceStorage for archives
|
||||
*
|
||||
* @param storage DeviceStorage instance
|
||||
* @param storages List of DeviceStorage instances
|
||||
* @param callbackFunction Function to call with the list of directories where archives are found
|
||||
*/
|
||||
LocalArchive.scanForArchives = function(storage, callbackFunction) {
|
||||
LocalArchive.scanForArchives = function(storages, callbackFunction) {
|
||||
var directories = [];
|
||||
var cursor = storage.enumerate();
|
||||
var cursor = util.enumerateAll(storages);
|
||||
cursor.onerror = function() {
|
||||
alert("Error scanning your SD card : " + cursor.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/r2d2b2g@mozilla.org/profile/fake-sdcard/wikipedia_small_2010-08-14)");
|
||||
|
@ -95,7 +95,72 @@ define(function(require) {
|
||||
|
||||
return (r > 0 ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function emulates a "composite" storage enumeration
|
||||
* (i.e. returns files from all the storage areas)
|
||||
* This is needed since the removal of composite storage :
|
||||
* see https://bugzilla.mozilla.org/show_bug.cgi?id=885753
|
||||
*
|
||||
* This code was copied (with only slight modifications) from Gaia source code :
|
||||
* https://bug893282.bugzilla.mozilla.org/attachment.cgi?id=785076
|
||||
*
|
||||
* @param {type} storages List of DeviceStorage instances
|
||||
* @returns {_L22.enumerateAll.cursor} Cursor of files found in device storages
|
||||
*/
|
||||
function enumerateAll(storages) {
|
||||
var storageIndex = 0;
|
||||
var ds_cursor = null;
|
||||
|
||||
var cursor = {
|
||||
continue: function cursor_continue() {
|
||||
ds_cursor.continue();
|
||||
}
|
||||
};
|
||||
|
||||
function enumerateNextStorage() {
|
||||
ds_cursor = storages[storageIndex].enumerate();
|
||||
ds_cursor.onsuccess = onsuccess;
|
||||
ds_cursor.onerror = onerror;
|
||||
};
|
||||
|
||||
function onsuccess(e) {
|
||||
cursor.result = e.target.result;
|
||||
if (!cursor.result) {
|
||||
storageIndex++;
|
||||
if (storageIndex < storages.length) {
|
||||
enumerateNextStorage();
|
||||
return;
|
||||
}
|
||||
// If we've run out of storages, then we fall through and call
|
||||
// onsuccess with the null result.
|
||||
}
|
||||
if (cursor.onsuccess) {
|
||||
try {
|
||||
cursor.onsuccess(e);
|
||||
} catch (err) {
|
||||
console.warn('enumerateAll onsuccess threw', err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function onerror(e) {
|
||||
cursor.error = e.target.error;
|
||||
if (cursor.onerror) {
|
||||
try {
|
||||
cursor.onerror(e);
|
||||
} catch (err) {
|
||||
console.warn('enumerateAll onerror threw', err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
enumerateNextStorage();
|
||||
return cursor;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Functions and classes exposed by this module
|
||||
@ -105,6 +170,7 @@ define(function(require) {
|
||||
readIntegerFrom4Bytes: readIntegerFrom4Bytes,
|
||||
readIntegerFrom2Bytes : readIntegerFrom2Bytes,
|
||||
uint8ArrayToHex : uint8ArrayToHex,
|
||||
uint8ArrayToBase64 : uint8ArrayToBase64
|
||||
uint8ArrayToBase64 : uint8ArrayToBase64,
|
||||
enumerateAll : enumerateAll
|
||||
};
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user