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 ) {
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.
* @method saveDocument
......@@ -49,12 +75,12 @@ var newDAVStorage = function ( spec, my ) {
priv.username+':'+priv.password)},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
that.done();
that.success();
},
error: function (type) {
type.message = 'Cannot save "' + command.getPath() +
'" into DAVStorage.';
that.fail(type);
that.retry(type);
}
} );
//// end saving on dav
......@@ -80,19 +106,20 @@ var newDAVStorage = function ( spec, my ) {
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function (content) {
doc.content = content;
that.done(doc);
that.success(doc);
},
error: function (type) {
if (type.status === 404) {
type.message = 'Document "' +
command.getPath() +
'" not found in localStorage.';
that.error(type);
} else {
type.message =
'Cannot load "' + command.getPath() +
'" from DAVStorage.';
that.retry(type);
}
that.fail(type);
}
} );
};
......@@ -124,13 +151,17 @@ var newDAVStorage = function ( spec, my ) {
if (!command.getOption('metadata_only')) {
getContent();
} else {
that.done(doc);
that.success(doc);
}
},
error: function (type) {
type.message = 'Cannot load "' + command.getPath() +
'" 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 ) {
* @method getDocumentList
*/
that.getDocumentList = function (command) {
var document_array = [], file = {}, path_array = [];
var document_array = [], file = {}, path_array = [],
am = priv.newAsyncModule(), o = {};
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/',
async: true,
type: 'PROPFIND',
dataType: 'xml',
headers: {'Authorization': 'Basic '+Base64.encode(
priv.username + ':' + priv.password ), Depth: '1'},
success: function (xmlData) {
var wait_for_me = 0;
var getContent = function (file) {
wait_for_me ++;
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/' +
file.name,
type: "GET",
async: true,
dataType: 'text', // TODO : is it necessary ?
headers: {'Authorization':'Basic '+
Base64.encode(priv.username +':'+
priv.password)},
success: function (content) {
file.content = content;
// WARNING : files can be disordered because
// of asynchronous action
document_array.push (file);
wait_for_me --;
},
error: function (type) {
type.message = 'Cannot get a document '+
'content from DAVStorage.';
that.fail(type);
wait_for_me --;
o.getContent = function (file) {
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/' +
file.name,
type: "GET",
async: true,
dataType: 'text', // TODO : is it necessary ?
headers: {'Authorization':'Basic '+
Base64.encode(priv.username +':'+
priv.password)},
success: function (content) {
file.content = content;
// WARNING : files can be disordered because
// of asynchronous action
document_array.push (file);
am.call(o,'success');
},
error: function (type) {
type.message = 'Cannot get a document '+
'content from DAVStorage.';
am.call(o,'error',[type]);
}
});
};
o.getDocumentList = function () {
$.ajax ( {
url: priv.url + '/dav/' +
priv.username + '/' +
priv.applicationname + '/',
async: true,
type: 'PROPFIND',
dataType: 'xml',
headers: {'Authorization': 'Basic '+Base64.encode(
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(
'D\\:response, 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')) {
getContent(file);
} else {
document_array.push (file);
}
},
error: function (type) {
type.message =
'Cannot get a document list from DAVStorage.';
if (type.status === 404) {
am.call(o,'error',[type]);
} else {
am.call(o,'retry',[type]);
}
});
// wait until all getContent are ended, only if needed
var tmpfun = function () {
setTimeout(function() {
if (wait_for_me) {
tmpfun();
} else {
that.done(document_array);
}
},200);
};
tmpfun();
},
error: function (type) {
type.message =
'Cannot get a document list from DAVStorage.';
that.fail(type);
}
} );
}
} );
};
o.retry = function (error) {
am.neverCall(o,'retry');
am.neverCall(o,'success');
am.neverCall(o,'error');
that.retry(error);
};
o.error = function (error) {
am.neverCall(o,'retry');
am.neverCall(o,'success');
am.neverCall(o,'error');
that.error(error);
};
o.success = function () {
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 ) {
priv.username + ':' + priv.password )},
// xhrFields: {withCredentials: 'true'}, // cross domain
success: function () {
that.done();
that.success();
},
error: function (type) {
if (type.status === 404) {
that.done();
that.success();
} else {
type.message = 'Cannot remove "' + that.getFileName() +
'" 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