migration from localStorage to fileStorege

This commit is contained in:
Matias Alejo Garcia 2015-04-25 14:42:17 -03:00
commit cf557fe018
4 changed files with 65 additions and 16 deletions

View file

@ -27,9 +27,14 @@ angular
['debug', 'info', 'warn', 'error', 'log'].forEach(function(level) { ['debug', 'info', 'warn', 'error', 'log'].forEach(function(level) {
var orig = $delegate[level]; var orig = $delegate[level];
$delegate[level] = function() { $delegate[level] = function() {
var args = [].slice.call(arguments).map(function(v){ var args = [].slice.call(arguments);
if (typeof v == 'undefined') return 'undefined'; args = args.map(function(v) {
if (typeof v == 'object') return JSON.stringify(v).substr(0,200)+'...'; if (typeof v == 'undefined') v = 'undefined';
if (typeof v == 'object') {
v = JSON.stringify(v);
if (v.length > 200)
v = v.substr(0, 197) + '...';
}
return v; return v;
}); });
historicLog.add(level, args.join(' ')); historicLog.add(level, args.join(' '));

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.services').factory('configService', function(storageService, lodash) { angular.module('copayApp.services').factory('configService', function(storageService, lodash, $log) {
var root = {}; var root = {};
var defaultConfig = { var defaultConfig = {
@ -48,6 +48,7 @@ angular.module('copayApp.services').factory('configService', function(storageSer
}; };
root.get = function(cb) { root.get = function(cb) {
storageService.getConfig(function(err, localConfig) { storageService.getConfig(function(err, localConfig) {
if (localConfig) { if (localConfig) {
configCache = JSON.parse(localConfig); configCache = JSON.parse(localConfig);
@ -61,9 +62,9 @@ angular.module('copayApp.services').factory('configService', function(storageSer
} }
} else { } else {
configCache = defaultConfig; configCache = lodash.clone(defaultConfig);
}; };
$log.debug('Preferences read:', configCache)
return cb(err, configCache); return cb(err, configCache);
}); });
}; };

View file

@ -139,10 +139,21 @@ angular.module('copayApp.services')
$rootScope.$emit('Local/DeviceError', err); $rootScope.$emit('Local/DeviceError', err);
return cb(err); return cb(err);
} }
if (!profile) return cb(new Error('NOPROFILE: No profile')); if (!profile) {
$log.debug('Profile read'); // Migration??
storageService.tryToMigrate(function(err, migratedProfile) {
if (err) return cb(err);
if (!migratedProfile)
return cb(new Error('NOPROFILE: No profile'));
profile = migratedProfile;
return root.bindProfile(profile, cb);
})
} else {
$log.debug('Profile read');
return root.bindProfile(profile, cb);
}
return root.bindProfile(profile, cb);
}); });
}; };
@ -268,11 +279,17 @@ angular.module('copayApp.services')
root.create = function(cb) { root.create = function(cb) {
root._createNewProfile(function(err, p) { $log.info('Creating profile');
if (err) return cb(err); configService.get(function(err) {
root.bindProfile(p, function(err) { root.applyConfig();
storageService.storeNewProfile(p, function(err) { root._createNewProfile(function(err, p) {
return cb(err); if (err) return cb(err);
console.log('[profileService.js.287]'); //TODO
root.bindProfile(p, function(err) {
storageService.storeNewProfile(p, function(err) {
return cb(err);
});
}); });
}); });
}); });

View file

@ -47,6 +47,32 @@ angular.module('copayApp.services')
}); });
}; };
root.tryToMigrate = function(cb) {
if (!isCordova) return cb();
localStorageService.get('profile', function(err, p) {
if (err) cb(err);
if (!p) return cb();
$log.info('Starting Migration profile to File storage...')
fileStorageService.create('profile', p, function(err) {
if (err) cb(err);
$log.info('Profile Migrated successfully');
localStorageService.get('config', function(err, c) {
if (err) cb(err);
if (!c) return cb(null, p);
fileStorageService.create('config', c, function(err) {
if (err) cb(err);
$log.info('Config Migrated successfully');
return cb(null, p)
});
});
});
});
};
root.storeNewProfile = function(profile, cb) { root.storeNewProfile = function(profile, cb) {
encryptOnMobile(profile.toObj(), function(err, x) { encryptOnMobile(profile.toObj(), function(err, x) {
storage.create('profile', x, cb); storage.create('profile', x, cb);
@ -63,7 +89,7 @@ angular.module('copayApp.services')
storage.get('profile', function(err, str) { storage.get('profile', function(err, str) {
if (err || !str) if (err || !str)
// Migrate ? // Migrate ?
return cb(err); return cb(err);
decryptOnMobile(str, function(err, str) { decryptOnMobile(str, function(err, str) {