From 1f6596a5ad5dce6119505a65fff3e4a8df925e96 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 24 Apr 2015 16:39:12 -0300 Subject: [PATCH] file api working. Migration script missing --- cordova/build.sh | 3 + cordova/config.xml | 3 + src/js/controllers/index.js | 18 +++++ src/js/routes.js | 7 +- src/js/services/configService.js | 13 ++-- src/js/services/fileStorage.js | 117 ++++++++++++++++++++++++++++++ src/js/services/localStorage.js | 38 +++------- src/js/services/profileService.js | 6 +- src/js/services/storageService.js | 25 ++++--- 9 files changed, 180 insertions(+), 50 deletions(-) create mode 100644 src/js/services/fileStorage.js diff --git a/cordova/build.sh b/cordova/build.sh index 4215be899..9858c897f 100755 --- a/cordova/build.sh +++ b/cordova/build.sh @@ -126,6 +126,9 @@ if [ ! -d $PROJECT ]; then cordova plugin add hu.dpal.phonegap.plugins.uniquedeviceid checkOK + cordova plugin add org.apache.cordova.file + checkOK + fi if $DBGJS diff --git a/cordova/config.xml b/cordova/config.xml index aabee9e98..07d568350 100644 --- a/cordova/config.xml +++ b/cordova/config.xml @@ -12,6 +12,9 @@ + + + diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index 9ca748ad2..0971cf64e 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -426,6 +426,19 @@ angular.module('copayApp.controllers').controller('indexController', function($r } }; + self.deviceError = function (err) { + if (isCordova) { + navigator.notification.confirm( + err, + function() {}, + 'Device Error', ['OK'] + ); + } else { + alert(err); + } + }; + + self.recreate = function(cb) { var fc = profileService.focusedClient; self.setOngoingProcess('recreating', true); @@ -563,6 +576,11 @@ angular.module('copayApp.controllers').controller('indexController', function($r $rootScope.$apply(); }); + $rootScope.$on('Local/DeviceError', function(event, err) { + self.deviceError(err); + $rootScope.$apply(); + }); + $rootScope.$on('Local/ClientError', function(event, err) { if (err.code && err.code === 'NOTAUTHORIZED') { // Show not error, just redirect to home (where the recreate option is shown) diff --git a/src/js/routes.js b/src/js/routes.js index d68255875..357e3f2f6 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -434,18 +434,21 @@ angular if (!profileService.profile && toState.needProfile) { + // Give us time to open / create the profile + event.preventDefault(); + // Try to open local profile profileService.loadAndBindProfile(function(err) { if (err) { if (err.message.match('NOPROFILE')) { $log.debug('No profile... redirecting'); $state.transitionTo('splash'); - event.preventDefault(); } else { throw new Error(err); // TODO } } else { - // Profile was loaded + console.log('Profile loaded ... resuming transition'); + $state.transitionTo('walletHome'); } }); } diff --git a/src/js/services/configService.js b/src/js/services/configService.js index 0e3ae3dad..baffdf8d4 100644 --- a/src/js/services/configService.js +++ b/src/js/services/configService.js @@ -1,8 +1,10 @@ 'use strict'; -angular.module('copayApp.services').factory('configService', function(localStorageService, lodash, bwcService) { +angular.module('copayApp.services').factory('configService', function(localStorageService, fileStorageService, isCordova, lodash, bwcService) { var root = {}; + var storage = isCordova ? fileStorageService : localStorageService; + var defaultConfig = { // wallet limits limits: { @@ -66,8 +68,7 @@ angular.module('copayApp.services').factory('configService', function(localStora }; root.get = function(cb) { - localStorageService.get('config', function(err, localConfig) { - + storage.get('config', function(err, localConfig) { if (localConfig) { configCache = JSON.parse(localConfig); @@ -89,7 +90,7 @@ angular.module('copayApp.services').factory('configService', function(localStora root.set = function(newOpts, cb) { var config = defaultConfig; - localStorageService.get('config', function(err, oldOpts) { + storage.get('config', function(err, oldOpts) { if (lodash.isString(oldOpts)) { oldOpts = JSON.parse(oldOpts); } @@ -102,12 +103,12 @@ angular.module('copayApp.services').factory('configService', function(localStora lodash.merge(config, oldOpts, newOpts); configCache = config; - localStorageService.set('config', JSON.stringify(config), cb); + storage.set('config', JSON.stringify(config), cb); }); }; root.reset = function(cb) { - localStorageService.remove('config', cb); + storage.remove('config', cb); }; root.getDefaults = function() { diff --git a/src/js/services/fileStorage.js b/src/js/services/fileStorage.js new file mode 100644 index 000000000..d745acbfe --- /dev/null +++ b/src/js/services/fileStorage.js @@ -0,0 +1,117 @@ +'use strict'; + +angular.module('copayApp.services') + .factory('fileStorageService', function(lodash, $log) { + var root = {}, fs; + + + root.init = function(cb) { + if (fs) return cb(null, fs); + + function onFileSystemSuccess(fileSystem) { + console.log('File system started: ', fileSystem.name, fileSystem.root.name); + fs = fileSystem; + return cb(null, fs); + } + + function fail(evt) { + var msg = 'Could not init file system: ' + evt.target.error.code; + console.log(msg); + return cb(msg); + }; + + window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail); + }; + + + root.get = function(k, cb) { + root.init(function(err, fs) { + if (err) return cb(err); + fs.root.getFile(k, { + create: false, + }, function(fileEntry) { + if (!fileEntry) return cb(); + fileEntry.file(function(file) { + var reader = new FileReader(); + + reader.onloadend = function(e) { + console.log("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); + }); + }) + }; + + root.set = function(k, v, cb) { + root.init(function(err, fs) { + if (err) return cb(err); + fs.root.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.onerror = function(e) { + console.log('Write failed: ' + e.toString()); + return cb('Fail to write:', e.toString()); + }; + + if (lodash.isObject(v)) + v = JSON.stringify(v); + + var blob = new Blob([v], { + type: "application/json" + }); + + fileWriter.write(blob); + + }, cb); + }); + + }); + }; + + root.remove = function(k, cb) { + root.init(function(err, fs) { + if (err) return cb(err); + fs.root.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); + }); + }); + }; + + /** + * Same as setItem, but fails if an item already exists + */ + root.create = function(name, value, callback) { + root.get(name, + function(err, data) { + if (data) { + return callback('EEXISTS'); + } else { + return root.set(name, value, callback); + } + }); + }; + + + return root; + }); diff --git a/src/js/services/localStorage.js b/src/js/services/localStorage.js index 462627df2..e4574351f 100644 --- a/src/js/services/localStorage.js +++ b/src/js/services/localStorage.js @@ -1,12 +1,9 @@ 'use strict'; angular.module('copayApp.services') - .factory('localStorageService', function() { - - var isChromeApp = typeof window !== "undefined" && window.chrome && chrome.runtime && chrome.runtime.id; + .factory('localStorageService', function(isChromeApp, $timeout) { var root = {}; - - var ls = ((typeof localStorage !== "undefined") ? localStorage : null); + var ls = ((typeof window.localStorage !== "undefined") ? window.localStorage : null); if (isChromeApp && !ls) { ls = localStorage = chrome.storage.local; @@ -14,9 +11,7 @@ angular.module('copayApp.services') } if (!ls) - throw new Error('localstorage not available, cannot run plugin'); - - root.init = function() {}; + throw new Error('localstorage not available'); root.get = function(k, cb) { if (isChromeApp) { @@ -26,7 +21,13 @@ angular.module('copayApp.services') return cb(null, data[k]); }); } else { +console.log('[localStorage.js.24]',k); //TODO +console.log('[localStorage.js.25:TODO:]'); //TODO +console.log('[localStorage.js.26:TODO:]'); //TODO +console.log('[localStorage.js.27:TODO:]'); //TODO + $timeout(function() { return cb(null, ls.getItem(k)); + }, 1000); } }; @@ -67,26 +68,5 @@ angular.module('copayApp.services') }; - root.clear = function(cb) { - // NOP - return cb(); - }; - - root.list = function(cb) { - if (isChromeApp) { - chrome.storage.local.get(null, function(items) { - return cb(null, lodash.keys(items)); - }); - } else { - var ret = []; - var l = ls.length; - - for (var i = 0; i < l; i++) - ret.push(ls.key(i)); - - return cb(null, ret); - } - }; - return root; }); diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index 769274428..88225bfcb 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -111,6 +111,8 @@ angular.module('copayApp.services') root.bindProfile = function(profile, cb) { + +console.log('[profileService.js.114] Bind profile', profile); //TODO root.profile = profile; configService.get(function(err) { @@ -127,12 +129,14 @@ angular.module('copayApp.services') root.loadAndBindProfile = function(cb) { storageService.getProfile(function(err, profile) { +console.log('[profileService.js.129:err:]',err); //TODO if (err) { - notification.error('CRITICAL ERROR: ' + err); + $rootScope.$emit('Local/DeviceError', err); return cb(err); } if (!profile) return cb(new Error('NOPROFILE: No profile')); +console.log('[profileService.js.135] BIND'); //TODO return root.bindProfile(profile, cb); }); }; diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index 7422a3650..671525ee1 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -1,8 +1,9 @@ 'use strict'; angular.module('copayApp.services') - .factory('storageService', function(localStorageService, sjcl, $log, lodash) { + .factory('storageService', function(fileStorageService, localStorageService, sjcl, $log, lodash, isCordova) { var root = {}; + var storage = isCordova ? fileStorageService : localStorageService; var getUUID = function(cb) { // TO SIMULATE MOBILE @@ -48,18 +49,18 @@ angular.module('copayApp.services') root.storeNewProfile = function(profile, cb) { encryptOnMobile(profile.toObj(), function(err, x) { - localStorageService.create('profile', x, cb); + storage.create('profile', x, cb); }); }; root.storeProfile = function(profile, cb) { encryptOnMobile(profile.toObj(), function(err, x) { - localStorageService.set('profile', x, cb); + storage.set('profile', x, cb); }); }; root.getProfile = function(cb) { - localStorageService.get('profile', function(err, str) { + storage.get('profile', function(err, str) { if (err || !str) return cb(err); decryptOnMobile(str, function(err, str) { @@ -76,35 +77,35 @@ angular.module('copayApp.services') }; root.deleteProfile = function(cb) { - localStorageService.remove('profile', cb); + storage.remove('profile', cb); }; root.storeFocusedWalletId = function(id, cb) { - localStorageService.set('focusedWalletId', id, cb); + storage.set('focusedWalletId', id, cb); }; root.getFocusedWalletId = function(cb) { - localStorageService.get('focusedWalletId', cb); + storage.get('focusedWalletId', cb); }; root.getLastAddress = function(walletId, cb) { - localStorageService.get('lastAddress-' + walletId, cb); + storage.get('lastAddress-' + walletId, cb); }; root.storeLastAddress = function(walletId, address, cb) { - localStorageService.set('lastAddress-' + walletId, address, cb); + storage.set('lastAddress-' + walletId, address, cb); }; root.clearLastAddress = function(walletId, cb) { - localStorageService.remove('lastAddress-' + walletId, cb); + storage.remove('lastAddress-' + walletId, cb); }; root.setBackupFlag = function(walletId, cb) { - localStorageService.set('backup-' + walletId, Date.now(), cb); + storage.set('backup-' + walletId, Date.now(), cb); }; root.getBackupFlag = function(walletId, cb) { - localStorageService.get('backup-' + walletId, cb); + storage.get('backup-' + walletId, cb); }; return root;