Merge pull request #2644 from matiu/bug/android-file

write file not as blobs
This commit is contained in:
Matias Alejo Garcia 2015-04-27 15:35:54 -03:00
commit 0479ef0f34
2 changed files with 79 additions and 78 deletions

View file

@ -246,13 +246,13 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}; };
self.handleError = function(err) { self.handleError = function(err) {
$log.debug('ERROR:', err); $log.warn('Client ERROR:', err);
if (err.code === 'NOTAUTHORIZED') { if (err.code === 'NOTAUTHORIZED') {
$scope.$emit('Local/NotAuthorized'); $scope.$emit('Local/NotAuthorized');
} else if (err.code === 'NOTFOUND') { } else if (err.code === 'NOTFOUND') {
$scope.$emit('Local/BWSNotFound'); $scope.$emit('Local/BWSNotFound');
} else { } else {
$scope.$emit('Local/ClientError', err); $scope.$emit('Local/ClientError', (err.error ? err.error : err));
} }
}; };
self.openWallet = function() { self.openWallet = function() {
@ -448,7 +448,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.setOngoingProcess('recreating', false); self.setOngoingProcess('recreating', false);
if (err) { if (err) {
self.clientError('Could not recreate wallet:' + err); self.handleError(err);
$rootScope.$apply(); $rootScope.$apply();
return; return;
} }
@ -485,7 +485,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
if (err) { if (err) {
if (self.walletId == walletId) if (self.walletId == walletId)
self.setOngoingProcess('scanning', false); self.setOngoingProcess('scanning', false);
self.clientError('Could not scan wallet:' + err); self.handleError(err);
$rootScope.$apply(); $rootScope.$apply();
} }
}); });
@ -533,6 +533,9 @@ angular.module('copayApp.controllers').controller('indexController', function($r
// UX event handlers // UX event handlers
$rootScope.$on('Local/ColorUpdated', function(event) { $rootScope.$on('Local/ColorUpdated', function(event) {
self.updateColor(); self.updateColor();
$timeout(function() {
$rootScope.$apply();
});
}); });
$rootScope.$on('Local/UnitSettingUpdated', function(event) { $rootScope.$on('Local/UnitSettingUpdated', function(event) {
@ -569,7 +572,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) {

View file

@ -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,70 +28,63 @@ 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) { dir.getFile(k, {
if (err) return cb(err); create: true,
$log.debug(".set: Got main dir:", dir.nativeURL); }, function(fileEntry) {
dir.getFile(k, { // Create a FileWriter object for our FileEntry (log.txt).
create: true, fileEntry.createWriter(function(fileWriter) {
}, function(fileEntry) {
// 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)){
var blob = new Blob([v], { v = v.toString();
type: "text/plain" }
});
fileWriter.write(blob);
}, cb); $log.debug('Writing:', k, v);
}); fileWriter.write(v);
}, cb);
}); });
}); });
}; };
@ -105,23 +102,23 @@ angular.module('copayApp.services')
// url = 'ms-appdata:///local/'; // url = 'ms-appdata:///local/';
window.resolveLocalFileSystemURL(url, function(dir) { window.resolveLocalFileSystemURL(url, function(dir) {
return cb(null, dir); return cb(null, dir);
}, function(err) {
$log.warn(err);
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 +137,5 @@ angular.module('copayApp.services')
}); });
}; };
return root; return root;
}); });