Commit fd87000b authored by Tristan Cavelier's avatar Tristan Cavelier

grunt jio and jio.storages + remove console.log and tests code

parent add5497c
This diff is collapsed.
This diff is collapsed.
/*! JIO Storage - v0.1.0 - 2012-08-20 /*! JIO Storage - v0.1.0 - 2012-08-21
* Copyright (c) 2012 Nexedi; Licensed */ * Copyright (c) 2012 Nexedi; Licensed */
(function(LocalOrCookieStorage, $, Base64, sjcl, hex_sha256, Jio) { (function(LocalOrCookieStorage, $, Base64, sjcl, hex_sha256, Jio) {
var newLocalStorage = function ( spec, my ) { var newLocalStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'base' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
priv.secureDocId = function (string) { priv.secureDocId = function (string) {
var split = string.split('/'), i; var split = string.split('/'), i;
...@@ -279,7 +280,8 @@ var newLocalStorage = function ( spec, my ) { ...@@ -279,7 +280,8 @@ var newLocalStorage = function ( spec, my ) {
Jio.addStorageType('local', newLocalStorage); Jio.addStorageType('local', newLocalStorage);
var newDAVStorage = function ( spec, my ) { var newDAVStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'base' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
priv.secureDocId = function (string) { priv.secureDocId = function (string) {
var split = string.split('/'), i; var split = string.split('/'), i;
...@@ -355,24 +357,16 @@ var newDAVStorage = function ( spec, my ) { ...@@ -355,24 +357,16 @@ var newDAVStorage = function ( spec, my ) {
return async; return async;
}; };
that.post = function (command) { priv.putOrPost = function (command,type) {
that.put(command);
};
/**
* Saves a document in the distant dav storage.
* @method put
*/
that.put = function (command) {
var secured_docid = priv.secureDocId(command.getDocId()); var secured_docid = priv.secureDocId(command.getDocId());
$.ajax ( { $.ajax ( {
url: priv.url + '/' + url: priv.url + '/' +
priv.secured_username + '/' + priv.secured_username + '/' +
priv.secured_applicationname + '/' + priv.secured_applicationname + '/' +
secured_docid, secured_docid + '?_=' + Date.now(), // to make url unique!
type: 'PUT', // and avoid chrome PUT on cache !
type: type,
data: command.getDocContent(), data: command.getDocContent(),
async: true, async: true,
dataType: 'text', // TODO is it necessary ? dataType: 'text', // TODO is it necessary ?
...@@ -390,6 +384,18 @@ var newDAVStorage = function ( spec, my ) { ...@@ -390,6 +384,18 @@ var newDAVStorage = function ( spec, my ) {
that.retry(type); that.retry(type);
} }
} ); } );
};
that.post = function (command) {
priv.putOrPost (command,'POST');
};
/**
* Saves a document in the distant dav storage.
* @method put
*/
that.put = function (command) {
priv.putOrPost (command,'PUT');
}; // end put }; // end put
/** /**
...@@ -403,7 +409,7 @@ var newDAVStorage = function ( spec, my ) { ...@@ -403,7 +409,7 @@ var newDAVStorage = function ( spec, my ) {
url: priv.url + '/' + url: priv.url + '/' +
priv.secured_username + '/' + priv.secured_username + '/' +
priv.secured_applicationname + '/' + priv.secured_applicationname + '/' +
secured_docid, secured_docid + '?_=' + Date.now(),
type: "GET", type: "GET",
async: true, async: true,
dataType: 'text', // TODO is it necessary ? dataType: 'text', // TODO is it necessary ?
...@@ -439,7 +445,7 @@ var newDAVStorage = function ( spec, my ) { ...@@ -439,7 +445,7 @@ var newDAVStorage = function ( spec, my ) {
url: priv.url + '/' + url: priv.url + '/' +
priv.secured_username + '/' + priv.secured_username + '/' +
priv.secured_applicationname + '/' + priv.secured_applicationname + '/' +
secured_docid, secured_docid + '?_=' + Date.now(),
type: "PROPFIND", type: "PROPFIND",
async: true, async: true,
dataType: 'xml', dataType: 'xml',
...@@ -494,7 +500,7 @@ var newDAVStorage = function ( spec, my ) { ...@@ -494,7 +500,7 @@ var newDAVStorage = function ( spec, my ) {
url: priv.url + '/' + url: priv.url + '/' +
priv.secured_username + '/' + priv.secured_username + '/' +
priv.secured_applicationname + '/' + priv.secured_applicationname + '/' +
priv.secureDocId(file.id), priv.secureDocId(file.id) + '?_=' + Date.now(),
type: "GET", type: "GET",
async: true, async: true,
dataType: 'text', // TODO : is it necessary ? dataType: 'text', // TODO : is it necessary ?
...@@ -521,12 +527,13 @@ var newDAVStorage = function ( spec, my ) { ...@@ -521,12 +527,13 @@ var newDAVStorage = function ( spec, my ) {
$.ajax ( { $.ajax ( {
url: priv.url + '/' + url: priv.url + '/' +
priv.secured_username + '/' + priv.secured_username + '/' +
priv.secured_applicationname + '/', priv.secured_applicationname + '/' + '?_=' + Date.now(),
async: true, async: true,
type: 'PROPFIND', type: 'PROPFIND',
dataType: 'xml', dataType: 'xml',
headers: {'Authorization': 'Basic '+Base64.encode( headers: {'Authorization': 'Basic '+Base64.encode(
priv.username + ':' + priv.password ), Depth: '1'}, priv.username + ':' + priv.password ), Depth: '1'},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function (xmlData) { success: function (xmlData) {
var response = $(xmlData).find( var response = $(xmlData).find(
'D\\:response, response' 'D\\:response, response'
...@@ -613,12 +620,11 @@ var newDAVStorage = function ( spec, my ) { ...@@ -613,12 +620,11 @@ var newDAVStorage = function ( spec, my ) {
that.remove = function (command) { that.remove = function (command) {
var secured_docid = priv.secureDocId(command.getDocId()); var secured_docid = priv.secureDocId(command.getDocId());
$.ajax ( { $.ajax ( {
url: priv.url + '/' + url: priv.url + '/' +
priv.secured_username + '/' + priv.secured_username + '/' +
priv.secured_applicationname + '/' + priv.secured_applicationname + '/' +
secured_docid, secured_docid + '?_=' + Date.now(),
type: "DELETE", type: "DELETE",
async: true, async: true,
headers: {'Authorization':'Basic '+Base64.encode( headers: {'Authorization':'Basic '+Base64.encode(
...@@ -648,7 +654,8 @@ var newDAVStorage = function ( spec, my ) { ...@@ -648,7 +654,8 @@ var newDAVStorage = function ( spec, my ) {
Jio.addStorageType('dav', newDAVStorage); Jio.addStorageType('dav', newDAVStorage);
var newReplicateStorage = function ( spec, my ) { var newReplicateStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
priv.return_value_array = []; priv.return_value_array = [];
priv.storagelist = spec.storagelist || []; priv.storagelist = spec.storagelist || [];
...@@ -753,7 +760,8 @@ var newReplicateStorage = function ( spec, my ) { ...@@ -753,7 +760,8 @@ var newReplicateStorage = function ( spec, my ) {
Jio.addStorageType('replicate', newReplicateStorage); Jio.addStorageType('replicate', newReplicateStorage);
var newIndexStorage = function ( spec, my ) { var newIndexStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
var validatestate_secondstorage = spec.storage || false; var validatestate_secondstorage = spec.storage || false;
priv.secondstorage_spec = spec.storage || {type:'base'}; priv.secondstorage_spec = spec.storage || {type:'base'};
...@@ -961,7 +969,8 @@ var newIndexStorage = function ( spec, my ) { ...@@ -961,7 +969,8 @@ var newIndexStorage = function ( spec, my ) {
Jio.addStorageType ('indexed', newIndexStorage); Jio.addStorageType ('indexed', newIndexStorage);
var newCryptedStorage = function ( spec, my ) { var newCryptedStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {}; spec = spec || {};
var that = my.basicStorage( spec, my ), priv = {};
var is_valid_storage = (spec.storage?true:false); var is_valid_storage = (spec.storage?true:false);
...@@ -1223,9 +1232,8 @@ var newCryptedStorage = function ( spec, my ) { ...@@ -1223,9 +1232,8 @@ var newCryptedStorage = function ( spec, my ) {
Jio.addStorageType('crypt', newCryptedStorage); Jio.addStorageType('crypt', newCryptedStorage);
var newConflictManagerStorage = function ( spec, my ) { var newConflictManagerStorage = function ( spec, my ) {
var that = Jio.storage( spec, my, 'handler' ), priv = {};
spec = spec || {}; spec = spec || {};
my = my || {}; var that = my.basicStorage( spec, my ), priv = {};
var storage_exists = (spec.storage?true:false); var storage_exists = (spec.storage?true:false);
priv.secondstorage_spec = spec.storage || {type:'base'}; priv.secondstorage_spec = spec.storage || {type:'base'};
...@@ -1253,20 +1261,18 @@ var newConflictManagerStorage = function ( spec, my ) { ...@@ -1253,20 +1261,18 @@ var newConflictManagerStorage = function ( spec, my ) {
priv.getDistantMetadata = function (command,path,success,error) { priv.getDistantMetadata = function (command,path,success,error) {
var cloned_option = command.cloneOption (); var cloned_option = command.cloneOption ();
cloned_option.metadata_only = false; cloned_option.metadata_only = false;
cloned_option.max_retry = command.getOption('max_retry') || 3;
that.addJob ('get',priv.secondstorage_spec,path,cloned_option, that.addJob ('get',priv.secondstorage_spec,path,cloned_option,
success, error); success, error);
}; };
priv.saveMetadataToDistant = function (command,path,content,success,error) { priv.saveMetadataToDistant = function (command,path,content,success,error) {
// max_retry:0 // inf
that.addJob ('put',priv.secondstorage_spec, that.addJob ('put',priv.secondstorage_spec,
{_id:path,content:JSON.stringify (content)}, {_id:path,content:JSON.stringify (content)},
command.cloneOption(),success,error); command.cloneOption(),success,error);
}; };
priv.saveNewRevision = function (command,path,content,success,error) { priv.saveNewRevision = function (command,path,content,success,error) {
that.addJob ('put',priv.secondstorage_spec,{_id:path,content:content}, that.addJob ('post',priv.secondstorage_spec,{_id:path,content:content},
command.cloneOption(),success,error); command.cloneOption(),success,error);
}; };
...@@ -1277,7 +1283,6 @@ var newConflictManagerStorage = function ( spec, my ) { ...@@ -1277,7 +1283,6 @@ var newConflictManagerStorage = function ( spec, my ) {
priv.deleteAFile = function (command,path,success,error) { priv.deleteAFile = function (command,path,success,error) {
var cloned_option = command.cloneOption(); var cloned_option = command.cloneOption();
cloned_option.max_retry = 0; // inf
that.addJob ('remove',priv.secondstorage_spec,{_id:path}, that.addJob ('remove',priv.secondstorage_spec,{_id:path},
command.cloneOption(), success, error); command.cloneOption(), success, error);
}; };
...@@ -1296,6 +1301,7 @@ var newConflictManagerStorage = function ( spec, my ) { ...@@ -1296,6 +1301,7 @@ var newConflictManagerStorage = function ( spec, my ) {
}; };
priv._revs = function (metadata,revision) { priv._revs = function (metadata,revision) {
if (!(metadata && revision)) { return null; }
if (metadata[revision]) { if (metadata[revision]) {
return {start:metadata[revision]._revisions.length, return {start:metadata[revision]._revisions.length,
ids:metadata[revision]._revisions}; ids:metadata[revision]._revisions};
...@@ -1305,6 +1311,7 @@ var newConflictManagerStorage = function ( spec, my ) { ...@@ -1305,6 +1311,7 @@ var newConflictManagerStorage = function ( spec, my ) {
}; };
priv._revs_info = function (metadata) { priv._revs_info = function (metadata) {
if (!metadata) { return null; }
var k, l = []; var k, l = [];
for (k in metadata) { for (k in metadata) {
l.push({ l.push({
......
This diff is collapsed.
...@@ -180,7 +180,7 @@ var newLocalStorage = function ( spec, my ) { ...@@ -180,7 +180,7 @@ var newLocalStorage = function ( spec, my ) {
} }
LocalOrCookieStorage.setItem(path, doc); LocalOrCookieStorage.setItem(path, doc);
that.success ({ok:true,id:command.getDocId()}); that.success ({ok:true,id:command.getDocId()});
},5000); });
}; // end put }; // end put
/** /**
...@@ -214,7 +214,7 @@ var newLocalStorage = function ( spec, my ) { ...@@ -214,7 +214,7 @@ var newLocalStorage = function ( spec, my ) {
} }
that.success (doc); that.success (doc);
} }
},5000); });
}; // end get }; // end get
/** /**
...@@ -250,7 +250,7 @@ var newLocalStorage = function ( spec, my ) { ...@@ -250,7 +250,7 @@ var newLocalStorage = function ( spec, my ) {
} }
} }
that.success ({total_rows:new_array.length,rows:new_array}); that.success ({total_rows:new_array.length,rows:new_array});
},5000); });
}; // end allDocs }; // end allDocs
/** /**
...@@ -271,7 +271,7 @@ var newLocalStorage = function ( spec, my ) { ...@@ -271,7 +271,7 @@ var newLocalStorage = function ( spec, my ) {
LocalOrCookieStorage.deleteItem(path); LocalOrCookieStorage.deleteItem(path);
priv.removeFileName(secured_docid); priv.removeFileName(secured_docid);
that.success ({ok:true,id:command.getDocId()}); that.success ({ok:true,id:command.getDocId()});
},5000); });
}; // end remove }; // end remove
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