fix storing files at dataDirectory

This commit is contained in:
Matias Alejo Garcia 2015-04-25 20:53:31 -03:00
parent 648d754556
commit 55d8c00658
2 changed files with 65 additions and 50 deletions

View File

@ -29,11 +29,17 @@ angular
$delegate[level] = function() { $delegate[level] = function() {
var args = [].slice.call(arguments); var args = [].slice.call(arguments);
args = args.map(function(v) { args = args.map(function(v) {
if (typeof v == 'undefined') v = 'undefined'; try {
if (typeof v == 'object') { if (typeof v == 'undefined') v = 'undefined';
v = JSON.stringify(v); if (typeof v == 'object') {
if (v.length > 200) v = JSON.stringify(v);
}
v = v.toString();
if (v.length > 200)
v = v.substr(0, 197) + '...'; v = v.substr(0, 197) + '...';
} catch (e) {
console.log('Error at log decorator:', e);
v = 'undefined';
} }
return v; return v;
}); });

View File

@ -2,7 +2,8 @@
angular.module('copayApp.services') angular.module('copayApp.services')
.factory('fileStorageService', function(lodash, $log) { .factory('fileStorageService', function(lodash, $log) {
var root = {}, fs; var root = {},
fs;
root.init = function(cb) { root.init = function(cb) {
@ -27,24 +28,28 @@ angular.module('copayApp.services')
root.get = function(k, cb) { root.get = function(k, cb) {
root.init(function(err, fs) { root.init(function(err, fs) {
if (err) return cb(err); if (err) return cb(err);
fs.root.getFile(k, { window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir) {
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) {
console.log("Read: " + this.result); if (this.result)
return cb(null, this.result) $log.debug("Read: ", 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);
}); });
}) })
}; };
@ -52,48 +57,52 @@ angular.module('copayApp.services')
root.set = function(k, v, cb) { root.set = function(k, v, cb) {
root.init(function(err, fs) { root.init(function(err, fs) {
if (err) return cb(err); if (err) return cb(err);
fs.root.getFile(k, { window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir) {
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()); console.log('Write failed: ' + e.toString());
return cb('Fail to write:', e.toString()); return cb('Fail to write:', e.toString());
}; };
if (lodash.isObject(v)) if (lodash.isObject(v))
v = JSON.stringify(v); v = JSON.stringify(v);
var blob = new Blob([v], { $log.debug('Writing:', k, v);
type: "application/json" var blob = new Blob([v], {
}); type: "text/plain"
});
fileWriter.write(blob);
fileWriter.write(blob); }, cb);
});
}, cb);
}); });
}); });
}; };
root.remove = function(k, cb) { root.remove = function(k, cb) {
root.init(function(err, fs) { root.init(function(err, fs) {
if (err) return cb(err); if (err) return cb(err);
fs.root.getFile(k, { window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir) {
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);
});
}); });
}); });
}; };