Commit a0b4d3f0 authored by Tristan Cavelier's avatar Tristan Cavelier

Updating Crypt Storage

parent c40c23d7
...@@ -64,7 +64,7 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -64,7 +64,7 @@ var newCryptedStorage = function ( spec, my ) {
message:'Unable to decrypt.'}); message:'Unable to decrypt.'});
return; return;
} }
callback(tmp); callback(undefined,tmp);
}; };
priv.newAsyncModule = function () { priv.newAsyncModule = function () {
...@@ -113,9 +113,8 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -113,9 +113,8 @@ var newCryptedStorage = function ( spec, my ) {
}; };
o.save = function () { o.save = function () {
var settings = command.cloneOption(), newcommand; var settings = command.cloneOption(), newcommand;
settings.onResponse = function (){}; settings.success = function () { that.success(); };
settings.onDone = function () { that.done(); }; settings.error = function (r) { that.error(r); };
settings.onFail = function (r) { that.fail(r); };
newcommand = that.newCommand( newcommand = that.newCommand(
'saveDocument', 'saveDocument',
{path:new_file_name,content:new_file_content,option:settings}); {path:new_file_name,content:new_file_content,option:settings});
...@@ -142,39 +141,37 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -142,39 +141,37 @@ var newCryptedStorage = function ( spec, my ) {
}; };
o.loadDocument = function () { o.loadDocument = function () {
var settings = command.cloneOption(), newcommand; var settings = command.cloneOption(), newcommand;
settings.onResponse = function(){}; settings.error = o.loadOnError;
settings.onFail = o.loadOnFail; settings.success = o.loadOnSuccess;
settings.onDone = o.loadOnDone;
newcommand = that.newCommand ( newcommand = that.newCommand (
'loadDocument', 'loadDocument',
{path:new_file_name,option:settings}); {path:new_file_name,option:settings});
that.addJob ( that.addJob (
that.newStorage ( priv.secondstorage_spec ), newcommand ); that.newStorage ( priv.secondstorage_spec ), newcommand );
}; };
o.loadOnDone = function (result) { o.loadOnSuccess = function (result) {
result.name = command.getPath(); result.name = command.getPath();
if (command.getOption('metadata_only')) { if (command.getOption('metadata_only')) {
that.done(result); that.success(result);
} else { } else {
priv.decrypt (result.content, function(res){ priv.decrypt (result.content, function(err,res){
if (typeof res === 'object') { if (err) {
that.fail({status:0,statusText:'Decrypt Fail', that.error(err);
message:'Unable to decrypt'});
} else { } else {
result.content = res; result.content = res;
// content only: the second storage should // content only: the second storage should
// manage content_only option, so it is not // manage content_only option, so it is not
// necessary to manage it. // necessary to manage it.
that.done(result); that.success(result);
} }
}); });
} }
}; };
o.loadOnFail = function (error) { o.loadOnError = function (error) {
// NOTE : we can re create an error object instead of // NOTE : we can re create an error object instead of
// keep the old ex:status=404,message="document 1y59gyl8g // keep the old ex:status=404,message="document 1y59gyl8g
// not found in localStorage"... // not found in localStorage"...
that.fail(error); that.error(error);
}; };
am.call(o,'encryptFilePath'); am.call(o,'encryptFilePath');
}; // end loadDocument }; // end loadDocument
...@@ -186,51 +183,57 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -186,51 +183,57 @@ var newCryptedStorage = function ( spec, my ) {
that.getDocumentList = function (command) { that.getDocumentList = function (command) {
var result_array = [], am = priv.newAsyncModule(), o = {}; var result_array = [], am = priv.newAsyncModule(), o = {};
o.getDocumentList = function () { o.getDocumentList = function () {
var newcommand = command.clone(); var settings = command.cloneOption();
newcommand.onResponseDo (function(){}); settings.success = o.getListOnSuccess;
newcommand.onDoneDo (o.getListOnDone); settings.error = o.getListOnError;
newcommand.onFailDo (o.getListOnFail);
that.addJob ( that.addJob (
that.newStorage ( priv.secondstorage_spec ), newcommand ); that.newStorage ( priv.secondstorage_spec ),
that.newCommand ( 'getDocumentList', {path:command.getPath(),
option:settings}) );
}; };
o.getListOnDone = function (result) { o.getListOnSuccess = function (result) {
result_array = result; result_array = result;
var i, fun = function () { var i, decrypt = function (c) {
var c = i; priv.decrypt (result_array[c].name,function (err,res) {
priv.decrypt (result[c].name,function (res) { if (err) {
am.call(o,'error',[err]);
} else {
am.call(o,'pushResult',[res,c,'name']); am.call(o,'pushResult',[res,c,'name']);
}
}); });
if (!command.getOption('metadata_only')) { if (!command.getOption('metadata_only')) {
priv.decrypt (result[c].content,function (res) { priv.decrypt (result_array[c].content,function (err,res) {
if (err) {
am.call(o,'error',[err]);
} else {
am.call(o,'pushResult',[res,c,'content']); am.call(o,'pushResult',[res,c,'content']);
}
}); });
} }
}; };
if (command.getOption('metadata_only')) { if (command.getOption('metadata_only')) {
am.wait(o,'done',result.length-1); am.wait(o,'success',result.length-1);
} else { } else {
am.wait(o,'done',result.length*2-1); am.wait(o,'success',result.length*2-1);
}
for (i = 0; i < result_array.length; i+= 1) {
decrypt(i);
} }
for (i = 0; i < result.length; i+= 1) { fun(); }
}; };
o.getListOnFail = function (error) { o.getListOnError = function (error) {
am.call(o,'fail',[error]); am.call(o,'error',[error]);
}; };
o.pushResult = function (result,index,key) { o.pushResult = function (result,index,key) {
if (typeof result === 'object') {
return am.call(o,'fail',[{status:0,statusText:'Decrypt Fail',
message:'Unable to decrypt.'}]);
}
result_array[index][key] = result; result_array[index][key] = result;
am.call(o,'done'); am.call(o,'success');
}; };
o.fail = function (error) { o.error = function (error) {
am.end(); am.end();
that.fail(error); that.error (error);
}; };
o.done = function () { o.success = function () {
am.end(); am.end();
that.done(result_array); that.success (result_array);
}; };
am.call(o,'getDocumentList'); am.call(o,'getDocumentList');
}; // end getDocumentList }; // end getDocumentList
...@@ -240,32 +243,30 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -240,32 +243,30 @@ var newCryptedStorage = function ( spec, my ) {
* @method removeDocument * @method removeDocument
*/ */
that.removeDocument = function (command) { that.removeDocument = function (command) {
var new_file_name, am = priv.newAsyncModule(), o = {}; var new_file_name, o = {};
o.encryptFilePath = function () { o.encryptFilePath = function () {
priv.encrypt(command.getPath(),function(res) { priv.encrypt(command.getPath(),function(res) {
new_file_name = res; new_file_name = res;
am.call(o,'removeDocument'); o.removeDocument();
}); });
}; };
o.removeDocument = function () { o.removeDocument = function () {
var cloned_option = command.cloneOption(); var cloned_option = command.cloneOption();
cloned_option.onResponse = o.removeOnResponse; cloned_option.error = o.removeOnError;
cloned_option.onFail = function () {}; cloned_option.success = o.removeOnSuccess;
cloned_option.onDone = function () {};
that.addJob(that.newStorage(priv.secondstorage_spec), that.addJob(that.newStorage(priv.secondstorage_spec),
that.newCommand( that.newCommand(
'removeDocument', 'removeDocument',
{path:new_file_name, {path:new_file_name,
option:cloned_option})); option:cloned_option}));
}; };
o.removeOnResponse = function (result) { o.removeOnSuccess = function (result) {
if (result.status.isDone()) { that.success();
that.done();
} else {
that.fail(result.error);
}
}; };
am.call(o,'encryptFilePath'); o.removeOnError = function (error) {
that.error (error);
};
o.encryptFilePath();
}; };
return that; return that;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment