Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #11325 from dhylands/dhylands-893282-mediadb
Browse files Browse the repository at this point in the history
Bug 893282 - Fix mediadb to scan all device storage areas. r=djf
  • Loading branch information
dhylands committed Aug 2, 2013
2 parents 0918120 + d88da08 commit 9326784
Showing 1 changed file with 67 additions and 11 deletions.
78 changes: 67 additions & 11 deletions shared/js/mediadb.js
Expand Up @@ -621,11 +621,6 @@ var MediaDB = (function() {
function initDeviceStorage() {
var details = media.details;

// Get the composite storage object that agregates all files of the
// specified media type. We use this object when scanning and when
// retrieving files using their fully-qualifed names.
media.storage = navigator.getDeviceStorage(mediaType);

// Get the individual device storage objects, so that we can listen
// for events on the different volumes separately.
details.storages = navigator.getDeviceStorages(mediaType);
Expand Down Expand Up @@ -923,13 +918,16 @@ var MediaDB = (function() {
throw Error('MediaDB is not ready. State: ' + this.state);

var media = this;
// Refetch the default storage area, since the user can change it
// in the settings app.
var storage = navigator.getDeviceStorage(media.mediaType);

// Delete any existing file by this name, then save the file.
var deletereq = media.storage.delete(filename);
var deletereq = storage.delete(filename);
deletereq.onsuccess = deletereq.onerror = save;

function save() {
var request = media.storage.addNamed(file, filename);
var request = storage.addNamed(file, filename);
request.onerror = function() {
console.error('MediaDB: Failed to store', filename,
'in DeviceStorage:', request.error);
Expand Down Expand Up @@ -1241,6 +1239,60 @@ var MediaDB = (function() {
return (path[0] === '.' || path.indexOf('/.') !== -1);
}

// With the removal of composite storage, this function emulates
// the composite storage enumeration (i.e. return files from
// all of the storage areas).
function enumerateAll(storages, dir, options) {
var storageIndex = 0;
var ds_cursor = null;

var cursor = {
continue: function cursor_continue() {
ds_cursor.continue();
}
};

function enumerateNextStorage() {
ds_cursor = storages[storageIndex].enumerate(dir, options);
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;
}

// Tell the db to start a manual scan. I think we don't do
// this automatically from the constructor, but most apps will start
// a scan right after calling the constructor and then will proceed to
Expand Down Expand Up @@ -1279,7 +1331,7 @@ var MediaDB = (function() {
var cursor;
if (timestamp > 0) {
media.details.firstscan = false;
cursor = media.storage.enumerate('', {
cursor = enumerateAll(media.details.storages, '', {
// add 1 so we don't find the same newest file again
since: new Date(timestamp + 1)
});
Expand All @@ -1290,7 +1342,7 @@ var MediaDB = (function() {
// allows important optimizations during the scanning process
media.details.firstscan = true;
media.details.records = [];
cursor = media.storage.enumerate('');
cursor = enumerateAll(media.details.storages, '');
}

cursor.onsuccess = function() {
Expand Down Expand Up @@ -1346,7 +1398,7 @@ var MediaDB = (function() {
// were found during the quick scan. So we'll start off by
// enumerating all files in device storage
var dsfiles = [];
var cursor = media.storage.enumerate('');
var cursor = enumerateAll(media.details.storages, '');
cursor.onsuccess = function() {
if (!media.scanning) // Abort if scanning has been cancelled
return;
Expand Down Expand Up @@ -1598,7 +1650,11 @@ var MediaDB = (function() {

// If we got a filename, look up the file in device storage
if (typeof f === 'string') {
var getreq = media.storage.get(f);
// Note: Even though we're using the default storage area, if the
// filename is fully qualified, it will get redirected to the
// appropriate storage area.
var storage = navigator.getDeviceStorage(media.mediaType);
var getreq = storage.get(f);
getreq.onerror = function() {
console.warn('MediaDB: Unknown file in insertRecord:',
f, getreq.error);
Expand Down

0 comments on commit 9326784

Please sign in to comment.