Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Connexion: switch all requests to post by default.
Browse files Browse the repository at this point in the history
Api: put support for file upload / download directly inside api, implement upload inside Connexion
  • Loading branch information
cdujeu committed Jun 2, 2015
1 parent 032e965 commit 3bd78b8
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 4 deletions.
48 changes: 47 additions & 1 deletion core/src/plugins/gui.ajax/res/js/es6/http/PydioApi.es6
Expand Up @@ -15,8 +15,8 @@ class PydioApi{
}

request(parameters, onComplete=null, onError=null, settings={}){
parameters['secure_token'] = this._secureToken;
if(window.Connexion){
// Connexion already handles secure_token
var c = new Connexion();
c.setParameters($H(parameters));
if(settings.method){
Expand All @@ -29,6 +29,7 @@ class PydioApi{
c.sendAsync();
}
}else if(window.jQuery){
parameters['secure_token'] = this._secureToken;
jQuery.ajax(this._baseUrl,{
method:settings.method ||'post',
data: parameters,
Expand All @@ -39,6 +40,51 @@ class PydioApi{
}
}

loadFile(filePath, onComplete=null, onError=null){
if(window.Connexion){
var c = new Connexion(filePath);
c.setMethod('GET');
c.onComplete = onComplete;
c.sendAsync();
}else if(window.jQuery){
jQuery.ajax(filePath, {
method:'GET',
async:true,
complete:onComplete,
error:onError
});
}
}

uploadFile(file, fileParameterName, queryStringParams='', onComplete=function(){}, onError=function(){}, onProgress=function(){}){

if(window.Connexion){
var c = new Connexion();
c.uploadFile(file, fileParameterName, queryStringParams, onComplete, onError, onProgress);
}else if(window.jQuery){
var formData = new FormData();
formData.append(fileParameterName, file);
queryStringParams += '&secure_token' + this._secureToken;
jQuery.ajax(this._baseUrl + '&' + queryStringParams, {
method:'POST',
data:formData,
complete:onComplete,
error:onError,
progress:onProgress
});
}

}

static supportsUpload(){
if(window.Connexion){
return (window.FormData || window.FileReader);
}else if(window.jQuery){
return window.FormData;
}
return false;
}

static getClient(){
if(PydioApi._PydioClient) return PydioApi._PydioClient;
var client = new PydioApi();
Expand Down
Expand Up @@ -35,7 +35,7 @@ Class.create("Connexion", {
if(baseUrl) this._baseUrl = baseUrl;
this._libUrl = window.ajxpResourcesFolder+'/js';
this._parameters = new Hash();
this._method = 'get';
this._method = 'post';
},

/**
Expand Down Expand Up @@ -76,7 +76,15 @@ Class.create("Connexion", {
addSecureToken : function(){
if(Connexion.SECURE_TOKEN && this._baseUrl.indexOf('secure_token') == -1 && !this._parameters.get('secure_token')){
this.addParameter('secure_token', Connexion.SECURE_TOKEN);
}
}else if(this._baseUrl.indexOf('secure_token=') !== -1){
// Remove from baseUrl and set inside params
var parts = this._baseUrl.split('secure_token=');
var toks = parts[1].split('&');
var token = toks.shift();
var rest = toks.join('&');
this._baseUrl = parts[0] + (rest ? '&' + rest : '');
this._parameters.set('secure_token', token);
}
},

/**
Expand Down Expand Up @@ -208,7 +216,70 @@ Class.create("Connexion", {
}
document.fire("ajaxplorer:server_answer", this);
},


uploadFile: function(file, fileParameterName, queryStringParams, onComplete, onError, onProgress){

if(!onComplete) onComplete = function(){};
if(!onError) onError = function(){};
if(!onProgress) onProgress = function(){};
var url = pydio.Parameters.get('ajxpServerAccess') + '&' + queryStringParams;
var xhr = this.initializeXHRForUpload(url, onComplete, onError, onProgress);
if(window.FormData){
this.sendFileUsingFormData(xhr, file, fileParameterName);
}else if(window.FileReader){
var fileReader = new FileReader();
fileReader.onload = function(e){
this.xhrSendAsBinary(xhr, file.name, e.target.result, fileParameterName);
}.bind(this);
fileReader.readAsBinaryString(file);
}else if(file.getAsBinary){
this.xhrSendAsBinary(xhr, file.name, file.getAsBinary(), fileParameterName)
}

},

initializeXHRForUpload : function(url, onComplete, onError, onProgress){
var xhr = new XMLHttpRequest();
var upload = xhr.upload;
upload.addEventListener("progress", function(e){
if (!e.lengthComputable) return;
onProgress(e);
}, false);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status === 200) {
onComplete(xhr);
} else {
onError(xhr);
}
}
}.bind(this);
upload.onerror = function(){
onError(xhr);
};
xhr.open("POST", url, true);
return xhr;
},

sendFileUsingFormData : function(xhr, file, fileParameterName){
var formData = new FormData();
formData.append(fileParameterName, file);
xhr.send(formData);
},

xhrSendAsBinary : function(xhr, fileName, fileData, fileParameterName){
var boundary = '----MultiPartFormBoundary' + (new Date()).getTime();
xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary);

var body = "--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name='"+fileParameterName+"'; filename='" + unescape(encodeURIComponent(fileName)) + "'\r\n";
body += "Content-Type: application/octet-stream\r\n\r\n";
body += fileData + "\r\n";
body += "--" + boundary + "--\r\n";

xhr.sendAsBinary(body);
},

/**
* Load a javascript library
* @param fileName String
Expand Down

0 comments on commit 3bd78b8

Please sign in to comment.