From cf557fe0182cd90730f024df96d05a7ead9d7f2d Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 25 Apr 2015 14:42:17 -0300 Subject: [PATCH] migration from localStorage to fileStorege --- src/js/routes.js | 11 ++++++++--- src/js/services/configService.js | 7 ++++--- src/js/services/profileService.js | 33 +++++++++++++++++++++++-------- src/js/services/storageService.js | 30 ++++++++++++++++++++++++++-- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/js/routes.js b/src/js/routes.js index 6eefe1a6d..c0e7f3e9c 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -27,9 +27,14 @@ angular ['debug', 'info', 'warn', 'error', 'log'].forEach(function(level) { var orig = $delegate[level]; $delegate[level] = function() { - var args = [].slice.call(arguments).map(function(v){ - if (typeof v == 'undefined') return 'undefined'; - if (typeof v == 'object') return JSON.stringify(v).substr(0,200)+'...'; + var args = [].slice.call(arguments); + args = args.map(function(v) { + 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; }); historicLog.add(level, args.join(' ')); diff --git a/src/js/services/configService.js b/src/js/services/configService.js index 9174b5055..835e2aa8c 100644 --- a/src/js/services/configService.js +++ b/src/js/services/configService.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.services').factory('configService', function(storageService, lodash) { +angular.module('copayApp.services').factory('configService', function(storageService, lodash, $log) { var root = {}; var defaultConfig = { @@ -48,6 +48,7 @@ angular.module('copayApp.services').factory('configService', function(storageSer }; root.get = function(cb) { + storageService.getConfig(function(err, localConfig) { if (localConfig) { configCache = JSON.parse(localConfig); @@ -61,9 +62,9 @@ angular.module('copayApp.services').factory('configService', function(storageSer } } else { - configCache = defaultConfig; + configCache = lodash.clone(defaultConfig); }; - + $log.debug('Preferences read:', configCache) return cb(err, configCache); }); }; diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index 3305abaad..08e9ec4b8 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -139,10 +139,21 @@ angular.module('copayApp.services') $rootScope.$emit('Local/DeviceError', err); return cb(err); } - if (!profile) return cb(new Error('NOPROFILE: No profile')); - $log.debug('Profile read'); + if (!profile) { + // 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._createNewProfile(function(err, p) { - if (err) return cb(err); - root.bindProfile(p, function(err) { - storageService.storeNewProfile(p, function(err) { - return cb(err); + $log.info('Creating profile'); + configService.get(function(err) { + root.applyConfig(); + root._createNewProfile(function(err, p) { + if (err) return cb(err); + +console.log('[profileService.js.287]'); //TODO + root.bindProfile(p, function(err) { + storageService.storeNewProfile(p, function(err) { + return cb(err); + }); }); }); }); diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index aa138372b..db72bf7c8 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -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) { encryptOnMobile(profile.toObj(), function(err, x) { storage.create('profile', x, cb); @@ -62,8 +88,8 @@ angular.module('copayApp.services') root.getProfile = function(cb) { storage.get('profile', function(err, str) { - if (err || !str) - // Migrate ? + if (err || !str) + // Migrate ? return cb(err); decryptOnMobile(str, function(err, str) {