import db from xml file

This commit is contained in:
exromany 2016-03-01 07:29:20 +04:00
parent 75cdfc4e33
commit 6729754049
4 changed files with 91 additions and 12 deletions

View File

@ -258,6 +258,13 @@ var AppModel = Backbone.Model.extend({
this.addFile(newFile);
},
importFile: function(params, callback) {
var logger = new Logger('open', params.name);
logger.info('File import request');
logger.info('Open file from supplied content');
this.importFileWithData(params, callback, params.fileXml);
},
openFile: function(params, callback) {
var logger = new Logger('open', params.name);
logger.info('File open request');
@ -371,6 +378,22 @@ var AppModel = Backbone.Model.extend({
});
},
importFileWithData: function(params, callback, data) {
var file = new FileModel({
name: params.name,
storage: params.storage,
path: params.path
});
var that = this;
file.import(data, function(err) {
if (err) {
return callback(err);
}
that.addFile(file);
that.fileOpened(file);
});
},
addToLastOpenFiles: function(file, rev) {
this.appLogger.debug('Add last open file', file.get('cacheId'), file.get('name'), file.get('storage'), file.get('path'), rev);
var dt = new Date();

View File

@ -78,6 +78,30 @@ var FileModel = Backbone.Model.extend({
this.set({ open: true, created: true, name: name });
},
import: function(fileXml, callback) {
try {
var ts = logger.ts();
var password = kdbxweb.ProtectedValue.fromString('');
var credentials = new kdbxweb.Credentials(password);
kdbxweb.Kdbx.import(fileXml, credentials, (function(db, err) {
if (err) {
logger.error('Error importing file', err.code, err.message, err);
callback(err);
} else {
this.db = db;
this.readModel();
this.set({ open: true, created: true });
logger.info('Imported file ' + this.get('name') + ': ' + logger.ts(ts) + ', ' +
Math.round(fileXml.byteLength / 1024) + ' kB');
callback();
}
}).bind(this));
} catch (e) {
logger.error('Error importing file', e, e.code, e.message, e);
callback(e);
}
},
openDemo: function(callback) {
var password = kdbxweb.ProtectedValue.fromString('demo');
var credentials = new kdbxweb.Credentials(password);

View File

@ -18,6 +18,7 @@ var OpenView = Backbone.View.extend({
'click .open__icon-open': 'openFile',
'click .open__icon-new': 'createNew',
'click .open__icon-dropbox': 'openFromDropbox',
'click .open__icon-import': 'importFromXml',
'click .open__icon-demo': 'createDemo',
'click .open__pass-input[readonly]': 'openFile',
'input .open__pass-input': 'inputInput',
@ -123,18 +124,29 @@ var OpenView = Backbone.View.extend({
processFile: function(file, complete) {
var reader = new FileReader();
reader.onload = (function(e) {
if (this.reading === 'fileData') {
this.params.id = null;
this.params.fileData = e.target.result;
this.params.name = file.name.replace(/\.\w+$/i, '');
this.params.path = file.path || null;
this.params.storage = file.path ? 'file' : null;
this.params.rev = null;
this.displayOpenFile();
} else {
this.params.keyFileData = e.target.result;
this.params.keyFileName = file.name;
this.displayOpenKeyFile();
switch (this.reading) {
case 'fileData':
this.params.id = null;
this.params.fileData = e.target.result;
this.params.name = file.name.replace(/\.\w+$/i, '');
this.params.path = file.path || null;
this.params.storage = file.path ? 'file' : null;
this.params.rev = null;
this.displayOpenFile();
break;
case 'fileXml':
this.params.id = null;
this.params.fileXml = e.target.result;
this.params.name = file.name.replace(/\.\w+$/i, '');
this.params.path = file.path || null;
this.params.storage = file.path ? 'file' : null;
this.params.rev = null;
this.importDb();
break;
default:
this.params.keyFileData = e.target.result;
this.params.keyFileName = file.name;
this.displayOpenKeyFile();
}
if (complete) {
complete(true);
@ -179,6 +191,12 @@ var OpenView = Backbone.View.extend({
}
},
importFromXml: function() {
if (!this.busy) {
this.openAny('fileXml', 'xml');
}
},
openKeyFile: function(e) {
if ($(e.target).hasClass('open__settings-key-file-dropbox')) {
this.openKeyFileFromDropbox();
@ -443,6 +461,16 @@ var OpenView = Backbone.View.extend({
} else {
this.trigger('close');
}
},
importDb: function() {
if (this.busy || !this.params.name) {
return;
}
this.$el.toggleClass('open--opening', true);
this.inputEl.attr('disabled', 'disabled');
this.busy = true;
this.afterPaint(this.model.importFile.bind(this.model, this.params, this.openDbComplete.bind(this)));
}
});

View File

@ -13,6 +13,10 @@
<i class="fa fa-dropbox open__icon-i"></i>
<div class="open__icon-text">Dropbox</div>
</div>
<div class="open__icon open__icon-import">
<i class="fa fa-upload open__icon-i"></i>
<div class="open__icon-text">Import from XML</div>
</div>
<div class="open__icon open__icon-demo">
<i class="fa fa-magic open__icon-i"></i>
<div class="open__icon-text">{{res 'openDemo'}}</div>