Implement scanning of sdcard content, in order to find the archives on

it
This commit is contained in:
mossroy 2013-06-01 20:45:26 +02:00
parent 203735b931
commit 9eb9e9c01a
3 changed files with 56 additions and 7 deletions

View File

@ -55,8 +55,8 @@ License:
<br /> <br />
<ul> <ul>
<li>On desktops, it works on recent Firefox and Chrome, and maybe on other browsers</li> <li>On desktops, it works on recent Firefox and Chrome, and maybe on other browsers</li>
<li>On the Firefos OS simulator, you have (for now) to put the French dump files in a "fake-sdcard/evopedia" folder of your firefox profile (ex : ~/.mozilla/firefox/xxxx.default/extensions/r2d2b2g@mozilla.org/profile/fake-sdcard). It looks for evopedia/wikipedia_fr_2013-02-16/titles.idx in it. You also need to install the application from the dashboard of the simulator instead of accessing via the browser (due to security restrictions in Firefox OS : only certified webapps can access the sdcard)</li> <li>On the Firefos OS simulator, you have to put the archive files in a "fake-sdcard/evopedia" folder of your firefox profile (ex : ~/.mozilla/firefox/xxxx.default/extensions/r2d2b2g@mozilla.org/profile/fake-sdcard). It looks for evopedia/*/titles.idx in it. You also need to install the application from the dashboard of the simulator instead of accessing via the browser (due to security restrictions in Firefox OS : only certified webapps can access the sdcard)</li>
<li>On a real Firefox OS device, you also have (for now) to put the French dump files in an "evopedia" directory at the root of your sdcard, so that it finds a file /evopedia/wikipedia_fr_2013-02-16/titles.idx on it</li> <li>On a real Firefox OS device, you also have to put the archive files in an "evopedia" directory at the root of your sdcard, so that it finds a file /evopedia/*/titles.idx on it</li>
</ul> </ul>
<br /> <br />
It's only a proof of concept so far : there are many many ways this could be enhanced (suggestions and patches are welcome : the source code is on <a href="https://github.com/mossroy/evopedia-html5">github</a>). In particular : It's only a proof of concept so far : there are many many ways this could be enhanced (suggestions and patches are welcome : the source code is on <a href="https://github.com/mossroy/evopedia-html5">github</a>). In particular :

View File

@ -49,13 +49,12 @@ define(function(require) {
} }
if (storage != null) { if (storage != null) {
//var directory = 'evopedia/wikipedia_small_2010-08-14'; // If DeviceStorage is available, we look for archives in it
var directory = 'evopedia/wikipedia_fr_2013-02-16'; var directory = 'evopedia';
localArchive = new evopedia.LocalArchive(); evopedia.LocalArchive.scanForArchives(storage,directory,selectFirstArchive);
localArchive.readTitleFile(storage, directory);
localArchive.readDataFiles(storage, directory, 0);
} }
else { else {
// If DeviceStorage is not available, we display the file select components
displayFileSelect(); displayFileSelect();
setLocalArchiveFromFileSelect(); setLocalArchiveFromFileSelect();
} }
@ -65,6 +64,15 @@ define(function(require) {
var titleName = event.state.titleName; var titleName = event.state.titleName;
goToArticle(titleName); goToArticle(titleName);
}; };
/**
* Selects the first archive found on device storage
*/
function selectFirstArchive(archiveDirectories) {
localArchive = new evopedia.LocalArchive();
localArchive.readTitleFile(storage, archiveDirectories[0]);
localArchive.readDataFiles(storage, archiveDirectories[0], 0);
}
/** /**
* Displays the zone to select files from the dump * Displays the zone to select files from the dump

View File

@ -419,6 +419,47 @@ define(function(require) {
reader.readAsArrayBuffer(blob); reader.readAsArrayBuffer(blob);
}; };
/**
*  Scans the DeviceStorage for archives
*
* @param storage DeviceStorage instance
* @param rootDirectory Directory from which to start scanning
* @param callbackFunction Function to call with the list of directories where archives are found
*/
LocalArchive.scanForArchives = function(storage, rootDirectory, callbackFunction) {
var directories = [];
var cursor = storage.enumerate();
cursor.onerror = function() {
alert("Error scanning directory " + rootDirectory + " : " + cursor.error);
}
cursor.onsuccess = function() {
if (cursor.result) {
var file = cursor.result;
var fileName = file.name;
// We look for files "titles.idx"
if (!endsWith(fileName,"titles.idx")) {
cursor.continue();
return;
}
// We want to return the directory where titles.idx is stored
var directory = fileName.substring(0, fileName.lastIndexOf('/'));
console.log(directory);
directories.push(directory);
cursor.continue();
}
else {
callbackFunction(directories);
}
};
};
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
/** /**
* Title class : defines the title of an article and some methods to manipulate it * Title class : defines the title of an article and some methods to manipulate it