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) {
|
$rootScope.$on('Local/BackupDone', function(event) {
|
||||||
self.needsBackup = false;
|
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) {
|
$rootScope.$on('Local/NotAuthorized', function(event) {
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,20 @@
|
||||||
angular.module('copayApp.services')
|
angular.module('copayApp.services')
|
||||||
.factory('fileStorageService', function(lodash, $log) {
|
.factory('fileStorageService', function(lodash, $log) {
|
||||||
var root = {},
|
var root = {},
|
||||||
fs;
|
_fs, _dir;
|
||||||
|
|
||||||
|
|
||||||
root.init = function(cb) {
|
root.init = function(cb) {
|
||||||
if (fs) return cb(null, fs);
|
if (_dir) return cb(null, _fs, _dir);
|
||||||
|
|
||||||
function onFileSystemSuccess(fileSystem) {
|
function onFileSystemSuccess(fileSystem) {
|
||||||
console.log('File system started: ', fileSystem.name, fileSystem.root.name);
|
console.log('File system started: ', fileSystem.name, fileSystem.root.name);
|
||||||
fs = fileSystem;
|
_fs = fileSystem;
|
||||||
return cb(null, fs);
|
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) {
|
function fail(evt) {
|
||||||
|
|
@ -24,67 +28,64 @@ angular.module('copayApp.services')
|
||||||
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
|
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
root.get = function(k, cb) {
|
root.get = function(k, cb) {
|
||||||
root.init(function(err, fs) {
|
root.init(function(err, fs, dir) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
root.getDir(function(err, dir) {
|
dir.getFile(k, {
|
||||||
if (err) return cb(err);
|
create: false,
|
||||||
$log.debug(".get: Got main dir:", dir.nativeURL);
|
}, function(fileEntry) {
|
||||||
dir.getFile(k, {
|
if (!fileEntry) return cb();
|
||||||
create: false,
|
fileEntry.file(function(file) {
|
||||||
}, function(fileEntry) {
|
var reader = new FileReader();
|
||||||
if (!fileEntry) return cb();
|
|
||||||
fileEntry.file(function(file) {
|
|
||||||
var reader = new FileReader();
|
|
||||||
|
|
||||||
reader.onloadend = function(e) {
|
reader.onloadend = function(e) {
|
||||||
if (this.result)
|
if (this.result)
|
||||||
$log.debug("Read: ", this.result);
|
$log.debug("Read: ", this.result);
|
||||||
return cb(null, this.result)
|
return cb(null, this.result)
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
});
|
|
||||||
}, function(err) {
|
|
||||||
// Not found
|
|
||||||
if (err.code == 1) return cb();
|
|
||||||
else return cb(err);
|
|
||||||
});
|
});
|
||||||
|
}, function(err) {
|
||||||
|
// Not found
|
||||||
|
if (err.code == 1) return cb();
|
||||||
|
else return cb(err);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
root.set = function(k, v, cb) {
|
root.set = function(k, v, cb) {
|
||||||
root.init(function(err, fs) {
|
root.init(function(err, fs, dir) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
root.getDir(function(err, dir) {
|
console.log('[fileStorage.js.58:dir:]',dir.nativeURL); //TODO
|
||||||
if (err) return cb(err);
|
dir.getFile(k, {
|
||||||
$log.debug(".set: Got main dir:", dir.nativeURL);
|
create: true,
|
||||||
dir.getFile(k, {
|
}, function(fileEntry) {
|
||||||
create: true,
|
// Create a FileWriter object for our FileEntry (log.txt).
|
||||||
}, function(fileEntry) {
|
fileEntry.createWriter(function(fileWriter) {
|
||||||
// Create a FileWriter object for our FileEntry (log.txt).
|
|
||||||
fileEntry.createWriter(function(fileWriter) {
|
|
||||||
|
|
||||||
fileWriter.onwriteend = function(e) {
|
fileWriter.onwriteend = function(e) {
|
||||||
console.log('Write completed.');
|
console.log('Write completed.');
|
||||||
return cb();
|
return cb();
|
||||||
};
|
};
|
||||||
|
|
||||||
fileWriter.onerror = function(e) {
|
fileWriter.onerror = function(e) {
|
||||||
console.log('Write failed: ' + e.toString());
|
var err = e.error ? e.error : JSON.stringify(e);
|
||||||
return cb('Fail to write:', e.toString());
|
console.log('Write failed: ' + err);
|
||||||
};
|
return cb('Fail to write:' + err);
|
||||||
|
};
|
||||||
|
|
||||||
if (lodash.isObject(v))
|
if (lodash.isObject(v))
|
||||||
v = JSON.stringify(v);
|
v = JSON.stringify(v);
|
||||||
|
|
||||||
$log.debug('Writing:', k, v);
|
if (!lodash.isString(v)){
|
||||||
fileWriter.write(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);
|
return cb(null, dir);
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
$log.warn(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.remove = function(k, cb) {
|
||||||
root.init(function(err, fs) {
|
root.init(function(err, fs, dir) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
root.getDir(function(err, dir) {
|
dir.getFile(k, {
|
||||||
if (err) return cb(err);
|
create: false,
|
||||||
dir.getFile(k, {
|
}, function(fileEntry) {
|
||||||
create: false,
|
// Create a FileWriter object for our FileEntry (log.txt).
|
||||||
}, function(fileEntry) {
|
fileEntry.remove(function() {
|
||||||
// Create a FileWriter object for our FileEntry (log.txt).
|
console.log('File removed.');
|
||||||
fileEntry.remove(function() {
|
return cb();
|
||||||
console.log('File removed.');
|
}, cb, cb);
|
||||||
return cb();
|
|
||||||
}, cb, cb);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
@ -140,6 +138,5 @@ angular.module('copayApp.services')
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue