open dir only once
This commit is contained in:
parent
28125773c8
commit
20b7a851d3
2 changed files with 71 additions and 72 deletions
|
|
@ -569,7 +569,9 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
|
||||
$rootScope.$on('Local/BackupDone', function(event) {
|
||||
self.needsBackup = false;
|
||||
storageService.setBackupFlag(self.walletId, function() {});
|
||||
storageService.setBackupFlag(self.walletId, function(err) {
|
||||
if (err) $rootScope.$emit('Local/DeviceError', err)
|
||||
});
|
||||
});
|
||||
|
||||
$rootScope.$on('Local/NotAuthorized', function(event) {
|
||||
|
|
|
|||
|
|
@ -3,16 +3,20 @@
|
|||
angular.module('copayApp.services')
|
||||
.factory('fileStorageService', function(lodash, $log) {
|
||||
var root = {},
|
||||
fs;
|
||||
|
||||
_fs, _dir;
|
||||
|
||||
root.init = function(cb) {
|
||||
if (fs) return cb(null, fs);
|
||||
if (_dir) return cb(null, _fs, _dir);
|
||||
|
||||
function onFileSystemSuccess(fileSystem) {
|
||||
console.log('File system started: ', fileSystem.name, fileSystem.root.name);
|
||||
fs = fileSystem;
|
||||
return cb(null, fs);
|
||||
_fs = fileSystem;
|
||||
root.getDir(function(err, newDir) {
|
||||
if (err || !newDir.nativeURL) return cb(err);
|
||||
_dir = newDir
|
||||
$log.debug("Got main dir:", _dir.nativeURL);
|
||||
return cb(null, _fs, _dir);
|
||||
});
|
||||
}
|
||||
|
||||
function fail(evt) {
|
||||
|
|
@ -24,67 +28,64 @@ angular.module('copayApp.services')
|
|||
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
|
||||
};
|
||||
|
||||
|
||||
root.get = function(k, cb) {
|
||||
root.init(function(err, fs) {
|
||||
root.init(function(err, fs, dir) {
|
||||
if (err) return cb(err);
|
||||
root.getDir(function(err, dir) {
|
||||
if (err) return cb(err);
|
||||
$log.debug(".get: Got main dir:", dir.nativeURL);
|
||||
dir.getFile(k, {
|
||||
create: false,
|
||||
}, function(fileEntry) {
|
||||
if (!fileEntry) return cb();
|
||||
fileEntry.file(function(file) {
|
||||
var reader = new FileReader();
|
||||
dir.getFile(k, {
|
||||
create: false,
|
||||
}, function(fileEntry) {
|
||||
if (!fileEntry) return cb();
|
||||
fileEntry.file(function(file) {
|
||||
var reader = new FileReader();
|
||||
|
||||
reader.onloadend = function(e) {
|
||||
if (this.result)
|
||||
$log.debug("Read: ", this.result);
|
||||
return cb(null, this.result)
|
||||
}
|
||||
reader.onloadend = function(e) {
|
||||
if (this.result)
|
||||
$log.debug("Read: ", this.result);
|
||||
return cb(null, this.result)
|
||||
}
|
||||
|
||||
reader.readAsText(file);
|
||||
});
|
||||
}, function(err) {
|
||||
// Not found
|
||||
if (err.code == 1) return cb();
|
||||
else return cb(err);
|
||||
reader.readAsText(file);
|
||||
});
|
||||
}, function(err) {
|
||||
// Not found
|
||||
if (err.code == 1) return cb();
|
||||
else return cb(err);
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
root.set = function(k, v, cb) {
|
||||
root.init(function(err, fs) {
|
||||
root.init(function(err, fs, dir) {
|
||||
if (err) return cb(err);
|
||||
root.getDir(function(err, dir) {
|
||||
if (err) return cb(err);
|
||||
$log.debug(".set: Got main dir:", dir.nativeURL);
|
||||
dir.getFile(k, {
|
||||
create: true,
|
||||
}, function(fileEntry) {
|
||||
// Create a FileWriter object for our FileEntry (log.txt).
|
||||
fileEntry.createWriter(function(fileWriter) {
|
||||
console.log('[fileStorage.js.58:dir:]',dir.nativeURL); //TODO
|
||||
dir.getFile(k, {
|
||||
create: true,
|
||||
}, function(fileEntry) {
|
||||
// Create a FileWriter object for our FileEntry (log.txt).
|
||||
fileEntry.createWriter(function(fileWriter) {
|
||||
|
||||
fileWriter.onwriteend = function(e) {
|
||||
console.log('Write completed.');
|
||||
return cb();
|
||||
};
|
||||
fileWriter.onwriteend = function(e) {
|
||||
console.log('Write completed.');
|
||||
return cb();
|
||||
};
|
||||
|
||||
fileWriter.onerror = function(e) {
|
||||
console.log('Write failed: ' + e.toString());
|
||||
return cb('Fail to write:', e.toString());
|
||||
};
|
||||
fileWriter.onerror = function(e) {
|
||||
var err = e.error ? e.error : JSON.stringify(e);
|
||||
console.log('Write failed: ' + err);
|
||||
return cb('Fail to write:' + err);
|
||||
};
|
||||
|
||||
if (lodash.isObject(v))
|
||||
v = JSON.stringify(v);
|
||||
if (lodash.isObject(v))
|
||||
v = JSON.stringify(v);
|
||||
|
||||
$log.debug('Writing:', k, v);
|
||||
fileWriter.write(v);
|
||||
if (!lodash.isString(v)){
|
||||
v = v.toString();
|
||||
}
|
||||
|
||||
}, cb);
|
||||
});
|
||||
$log.debug('Writing:', k, v);
|
||||
fileWriter.write(v);
|
||||
|
||||
}, cb);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
@ -104,24 +105,21 @@ angular.module('copayApp.services')
|
|||
return cb(null, dir);
|
||||
}, function(err) {
|
||||
$log.warn(err);
|
||||
return cb(err || 'Could not resolve filesystem:' + url);
|
||||
return cb(err || 'Could not resolve filesystem:' + url);
|
||||
});
|
||||
};
|
||||
|
||||
root.remove = function(k, cb) {
|
||||
root.init(function(err, fs) {
|
||||
root.init(function(err, fs, dir) {
|
||||
if (err) return cb(err);
|
||||
root.getDir(function(err, dir) {
|
||||
if (err) return cb(err);
|
||||
dir.getFile(k, {
|
||||
create: false,
|
||||
}, function(fileEntry) {
|
||||
// Create a FileWriter object for our FileEntry (log.txt).
|
||||
fileEntry.remove(function() {
|
||||
console.log('File removed.');
|
||||
return cb();
|
||||
}, cb, cb);
|
||||
});
|
||||
dir.getFile(k, {
|
||||
create: false,
|
||||
}, function(fileEntry) {
|
||||
// Create a FileWriter object for our FileEntry (log.txt).
|
||||
fileEntry.remove(function() {
|
||||
console.log('File removed.');
|
||||
return cb();
|
||||
}, cb, cb);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
@ -140,6 +138,5 @@ angular.module('copayApp.services')
|
|||
});
|
||||
};
|
||||
|
||||
|
||||
return root;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue