Commit 0c0dbc06 authored by Tristan Cavelier's avatar Tristan Cavelier

Updating Dav Storage

Fix getDocumentList setTimeout bug.
parent 7996dfca
...@@ -28,6 +28,32 @@ var newDAVStorage = function ( spec, my ) { ...@@ -28,6 +28,32 @@ var newDAVStorage = function ( spec, my ) {
return 'Need at least 2 parameters: "username" and "url".'; return 'Need at least 2 parameters: "username" and "url".';
}; };
priv.newAsyncModule = function () {
var async = {};
async.call = function (obj,function_name,arglist) {
obj._wait = obj._wait || {};
if (obj._wait[function_name]) {
obj._wait[function_name]--;
return function () {};
}
// ok if undef or 0
arglist = arglist || [];
return obj[function_name].apply(obj[function_name],arglist);
};
async.neverCall = function (obj,function_name) {
obj._wait = obj._wait || {};
obj._wait[function_name] = -1;
};
async.wait = function (obj,function_name,times) {
obj._wait = obj._wait || {};
obj._wait[function_name] = times;
};
async.end = function () {
async.call = function(){};
};
return async;
};
/** /**
* Saves a document in the distant dav storage. * Saves a document in the distant dav storage.
* @method saveDocument * @method saveDocument
...@@ -49,12 +75,12 @@ var newDAVStorage = function ( spec, my ) { ...@@ -49,12 +75,12 @@ var newDAVStorage = function ( spec, my ) {
priv.username+':'+priv.password)}, priv.username+':'+priv.password)},
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function () { success: function () {
that.done(); that.success();
}, },
error: function (type) { error: function (type) {
type.message = 'Cannot save "' + command.getPath() + type.message = 'Cannot save "' + command.getPath() +
'" into DAVStorage.'; '" into DAVStorage.';
that.fail(type); that.retry(type);
} }
} ); } );
//// end saving on dav //// end saving on dav
...@@ -80,19 +106,20 @@ var newDAVStorage = function ( spec, my ) { ...@@ -80,19 +106,20 @@ var newDAVStorage = function ( spec, my ) {
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function (content) { success: function (content) {
doc.content = content; doc.content = content;
that.done(doc); that.success(doc);
}, },
error: function (type) { error: function (type) {
if (type.status === 404) { if (type.status === 404) {
type.message = 'Document "' + type.message = 'Document "' +
command.getPath() + command.getPath() +
'" not found in localStorage.'; '" not found in localStorage.';
that.error(type);
} else { } else {
type.message = type.message =
'Cannot load "' + command.getPath() + 'Cannot load "' + command.getPath() +
'" from DAVStorage.'; '" from DAVStorage.';
that.retry(type);
} }
that.fail(type);
} }
} ); } );
}; };
...@@ -124,13 +151,17 @@ var newDAVStorage = function ( spec, my ) { ...@@ -124,13 +151,17 @@ var newDAVStorage = function ( spec, my ) {
if (!command.getOption('metadata_only')) { if (!command.getOption('metadata_only')) {
getContent(); getContent();
} else { } else {
that.done(doc); that.success(doc);
} }
}, },
error: function (type) { error: function (type) {
type.message = 'Cannot load "' + command.getPath() + type.message = 'Cannot load "' + command.getPath() +
'" informations from DAVStorage.'; '" informations from DAVStorage.';
that.fail(type); if (type.status === 404) {
that.error(type);
} else {
that.retry(type);
}
} }
} ); } );
}; };
...@@ -140,96 +171,112 @@ var newDAVStorage = function ( spec, my ) { ...@@ -140,96 +171,112 @@ var newDAVStorage = function ( spec, my ) {
* @method getDocumentList * @method getDocumentList
*/ */
that.getDocumentList = function (command) { that.getDocumentList = function (command) {
var document_array = [], file = {}, path_array = []; var document_array = [], file = {}, path_array = [],
am = priv.newAsyncModule(), o = {};
$.ajax ( { o.getContent = function (file) {
url: priv.url + '/dav/' + $.ajax ( {
priv.username + '/' + url: priv.url + '/dav/' +
priv.applicationname + '/', priv.username + '/' +
async: true, priv.applicationname + '/' +
type: 'PROPFIND', file.name,
dataType: 'xml', type: "GET",
headers: {'Authorization': 'Basic '+Base64.encode( async: true,
priv.username + ':' + priv.password ), Depth: '1'}, dataType: 'text', // TODO : is it necessary ?
success: function (xmlData) { headers: {'Authorization':'Basic '+
var wait_for_me = 0; Base64.encode(priv.username +':'+
var getContent = function (file) { priv.password)},
wait_for_me ++; success: function (content) {
$.ajax ( { file.content = content;
url: priv.url + '/dav/' + // WARNING : files can be disordered because
priv.username + '/' + // of asynchronous action
priv.applicationname + '/' + document_array.push (file);
file.name, am.call(o,'success');
type: "GET", },
async: true, error: function (type) {
dataType: 'text', // TODO : is it necessary ? type.message = 'Cannot get a document '+
headers: {'Authorization':'Basic '+ 'content from DAVStorage.';
Base64.encode(priv.username +':'+ am.call(o,'error',[type]);
priv.password)}, }
success: function (content) { });
file.content = content; };
// WARNING : files can be disordered because o.getDocumentList = function () {
// of asynchronous action $.ajax ( {
document_array.push (file); url: priv.url + '/dav/' +
wait_for_me --; priv.username + '/' +
}, priv.applicationname + '/',
error: function (type) { async: true,
type.message = 'Cannot get a document '+ type: 'PROPFIND',
'content from DAVStorage.'; dataType: 'xml',
that.fail(type); headers: {'Authorization': 'Basic '+Base64.encode(
wait_for_me --; priv.username + ':' + priv.password ), Depth: '1'},
success: function (xmlData) {
var response = $(xmlData).find(
'D\\:response, response'
);
var len = response.length;
am.wait(o,'success',len-2);
response.each( function(i,data){
if(i>0) { // exclude parent folder
file = {};
$(data).find('D\\:href, href').each(function(){
path_array = $(this).text().split('/');
file.name =
(path_array[path_array.length-1] ?
path_array[path_array.length-1] :
path_array[path_array.length-2]+'/');
});
if (file.name === '.htaccess' ||
file.name === '.htpasswd') { return; }
$(data).find(
'lp1\\:getlastmodified, getlastmodified'
).each(function () {
file.last_modified = $(this).text();
});
$(data).find(
'lp1\\:creationdate, creationdate'
).each(function () {
file.creation_date = $(this).text();
});
if (!command.getOption ('metadata_only')) {
am.call(o,'getContent',[file]);
} else {
document_array.push (file);
am.call(o,'success');
}
} }
}); });
}; },
$(xmlData).find( error: function (type) {
'D\\:response, response' type.message =
).each( function(i,data){ 'Cannot get a document list from DAVStorage.';
if(i>0) { // exclude parent folder if (type.status === 404) {
file = {}; am.call(o,'error',[type]);
$(data).find('D\\:href, href').each(function(){ } else {
path_array = $(this).text().split('/'); am.call(o,'retry',[type]);
file.name =
(path_array[path_array.length-1] ?
path_array[path_array.length-1] :
path_array[path_array.length-2]+'/');
});
if (file.name === '.htaccess' ||
file.name === '.htpasswd') { return; }
$(data).find(
'lp1\\:getlastmodified, getlastmodified'
).each(function () {
file.last_modified = $(this).text();
});
$(data).find(
'lp1\\:creationdate, creationdate'
).each(function () {
file.creation_date = $(this).text();
});
if (!command.getOption ('metadata_only')) {
getContent(file);
} else {
document_array.push (file);
}
} }
}); }
// wait until all getContent are ended, only if needed } );
var tmpfun = function () { };
setTimeout(function() { o.retry = function (error) {
if (wait_for_me) { am.neverCall(o,'retry');
tmpfun(); am.neverCall(o,'success');
} else { am.neverCall(o,'error');
that.done(document_array); that.retry(error);
} };
},200); o.error = function (error) {
}; am.neverCall(o,'retry');
tmpfun(); am.neverCall(o,'success');
}, am.neverCall(o,'error');
error: function (type) { that.error(error);
type.message = };
'Cannot get a document list from DAVStorage.'; o.success = function () {
that.fail(type); am.neverCall(o,'retry');
} am.neverCall(o,'success');
} ); am.neverCall(o,'error');
that.success(document_array);
};
am.call (o,'getDocumentList');
}; };
/** /**
...@@ -249,15 +296,15 @@ var newDAVStorage = function ( spec, my ) { ...@@ -249,15 +296,15 @@ var newDAVStorage = function ( spec, my ) {
priv.username + ':' + priv.password )}, priv.username + ':' + priv.password )},
// xhrFields: {withCredentials: 'true'}, // cross domain // xhrFields: {withCredentials: 'true'}, // cross domain
success: function () { success: function () {
that.done(); that.success();
}, },
error: function (type) { error: function (type) {
if (type.status === 404) { if (type.status === 404) {
that.done(); that.success();
} else { } else {
type.message = 'Cannot remove "' + that.getFileName() + type.message = 'Cannot remove "' + that.getFileName() +
'" from DAVStorage.'; '" from DAVStorage.';
that.fail(type); that.retry(type);
} }
} }
} ); } );
......
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