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

View file

@ -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,70 +28,63 @@ 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) {
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);
var blob = new Blob([v], {
type: "text/plain"
});
fileWriter.write(blob);
if (!lodash.isString(v)){
v = v.toString();
}
}, cb);
});
$log.debug('Writing:', k, v);
fileWriter.write(v);
}, cb);
});
});
};
@ -105,23 +102,23 @@ angular.module('copayApp.services')
// url = 'ms-appdata:///local/';
window.resolveLocalFileSystemURL(url, function(dir) {
return cb(null, dir);
}, function(err) {
$log.warn(err);
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 +137,5 @@ angular.module('copayApp.services')
});
};
return root;
});