Permalink
Browse files

Update uploader

  • Loading branch information...
1 parent c913927 commit 80f6cea1aa1f2f6c26e16f12fc3c826e6eef488a @cdujeu cdujeu committed Sep 19, 2016
@@ -91,22 +91,32 @@ class PydioApi{
* @param onProgress
* @returns XHR Handle to abort transfer
*/
- uploadFile(file, fileParameterName, queryStringParams='', onComplete=function(){}, onError=function(){}, onProgress=function(){}){
+ uploadFile(file, fileParameterName, queryStringParams='', onComplete=function(){}, onError=function(){}, onProgress=function(){}, uploadUrl='', xhrSettings={}){
+
+ if(!uploadUrl){
+ uploadUrl = pydio.Parameters.get('ajxpServerAccess');
+ }
+ if(queryStringParams){
+ uploadUrl += (uploadUrl.indexOf('?') === -1 ? '?' : '&') + queryStringParams;
+ }
if(window.Connexion){
+
var c = new Connexion();
- return c.uploadFile(file, fileParameterName, queryStringParams, onComplete, onError, onProgress);
+ return c.uploadFile(file, fileParameterName, uploadUrl, onComplete, onError, onProgress, xhrSettings);
+
}else if(window.jQuery){
+
var formData = new FormData();
formData.append(fileParameterName, file);
- queryStringParams += '&secure_token' + this._secureToken;
- return jQuery.ajax(this._baseUrl + '&' + queryStringParams, {
+ return jQuery.ajax(uploadUrl, {
method:'POST',
data:formData,
complete:onComplete,
error:onError,
progress:onProgress
});
+
}
}
@@ -217,13 +217,12 @@ Class.create("Connexion", {
document.fire("ajaxplorer:server_answer", this);
},
- uploadFile: function(file, fileParameterName, queryStringParams, onComplete, onError, onProgress){
+ uploadFile: function(file, fileParameterName, uploadUrl, onComplete, onError, onProgress, xhrSettings={}){
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);
+ var xhr = this.initializeXHRForUpload(uploadUrl, onComplete, onError, onProgress, xhrSettings);
if(window.FormData){
this.sendFileUsingFormData(xhr, file, fileParameterName);
}else if(window.FileReader){
@@ -236,12 +235,15 @@ Class.create("Connexion", {
this.xhrSendAsBinary(xhr, file.name, file.getAsBinary(), fileParameterName)
}
return xhr;
-
+
},
- initializeXHRForUpload : function(url, onComplete, onError, onProgress){
+ initializeXHRForUpload : function(url, onComplete, onError, onProgress, xhrSettings={}){
var xhr = new XMLHttpRequest();
var upload = xhr.upload;
+ if(xhrSettings.withCredentials){
+ xhr.withCredentials = true;
+ }
upload.addEventListener("progress", function(e){
if (!e.lengthComputable) return;
onProgress(e);
Oops, something went wrong.
@@ -34,6 +34,7 @@
}
class UploadItem extends StatusItem{
+
constructor(file, targetNode, relativePath = null){
super('file');
this._file = file;
@@ -43,6 +44,9 @@
this._repositoryId = global.pydio.user.activeRepository;
this._relativePath = relativePath;
}
+ getMqConfigs(){
+ return global.pydio.getPluginConfigs('mq');
+ }
getFile(){
return this._file;
}
@@ -130,14 +134,20 @@
return;
}
- this.xhr = PydioApi.getClient().uploadFile(
- this._file,
- 'userfile_0',
- queryString,
- complete,
- error,
- progress
- );
+ // Checks applied.
+ if(this.getMqConfigs().get('UPLOAD_ACTIVE')){
+
+ this.tryAlternativeUpload(complete, progress, function(){
+ // Failed, switch back to normal upload.
+ this.xhr = PydioApi.getClient().uploadFile(this._file,'userfile_0',queryString,complete,error,progress);
+ });
+
+ }else{
+
+ this.xhr = PydioApi.getClient().uploadFile(this._file,'userfile_0',queryString,complete,error,progress);
+
+ }
+
}
_doAbort(completeCallback){
if(this.xhr){
@@ -146,6 +156,36 @@
}catch(e){}
}
}
+
+ tryAlternativeUpload(completeCallback, progressCallback, errorCallback){
+ let configs = this.getMqConfigs();
+ var host = configs.get("BOOSTER_MAIN_HOST");
+ if(configs.get("BOOSTER_UPLOAD_ADVANCED") && configs.get("BOOSTER_UPLOAD_ADVANCED")['booster_ws_advanced'] === 'custom' && configs.get("BOOSTER_UPLOAD_ADVANCED")['WS_HOST']){
+ host = configs.get("BOOSTER_UPLOAD_ADVANCED")['WS_HOST'];
+ }
+ var port = configs.get("BOOSTER_MAIN_PORT");
+ if(configs.get("BOOSTER_UPLOAD_ADVANCED") && configs.get("BOOSTER_UPLOAD_ADVANCED")['booster_upload_advanced'] === 'custom' && configs.get("BOOSTER_UPLOAD_ADVANCED")['WS_PORT']){
+ port = configs.get("BOOSTER_UPLOAD_ADVANCED")['WS_PORT'];
+ }
+ let fullPath = this._targetNode.getPath();
+ if(this._relativePath) {
+ fullPath += PathUtils.getDirname(this._relativePath);
+ }
+ fullPath += '/' + PathUtils.getBasename(this._file.name);
+
+ let url = "http"+(configs.get("UPLOAD_SECURE")?"s":"")+"://"+host+":"+port+"/"+configs.get("UPLOAD_PATH")+"/"+this._repositoryId + fullPath;
+ let queryString = '';
+ let overwriteStatus = UploaderConfigs.getInstance().getOption("DEFAULT_EXISTING", "upload_existing");
+ if(overwriteStatus === 'rename') {
+ queryString += 'auto_rename=true';
+ }
+ try{
+ console.log(url);
+ this.xhr = PydioApi.getClient().uploadFile(this._file,'userfile_0',queryString,completeCallback,errorCallback,progressCallback, url, {withCredentials:true});
+ }catch(e){
+ errorCallback();
+ }
+ }
}
class FolderItem extends StatusItem{

0 comments on commit 80f6cea

Please sign in to comment.