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

Commit 9fecbc6

Browse files
committed
Refactor the awful JS code for triggering download in a PydioApi.downloadSelection() method.
1 parent 8d912d5 commit 9fecbc6

File tree

4 files changed

+75
-69
lines changed

4 files changed

+75
-69
lines changed

core/src/plugins/access.fs/fsActions.xml

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -217,44 +217,15 @@
217217
<processing>
218218
<clientCallback prepareModal="true"><![CDATA[
219219
var userSelection = ajaxplorer.getUserSelection();
220-
if((userSelection.isUnique() && !userSelection.hasDir()) || multipleFilesDownloadEnabled)
220+
if(( userSelection.isUnique() && !userSelection.hasDir() ) || pydio.Parameters.get('multipleFilesDownloadEnabled'))
221221
{
222-
if(gaTrackEvent){
222+
if(window.gaTrackEvent){
223223
var fileNames = userSelection.getFileNames();
224224
for(var i=0; i<fileNames.length;i++){
225-
gaTrackEvent("Data", "Download", fileNames[i]);
225+
window.gaTrackEvent("Data", "Download", fileNames[i]);
226226
}
227227
}
228-
var agent = navigator.userAgent;
229-
if(agent && (agent.indexOf('iPhone')!=-1||agent.indexOf('iPod')!=-1||agent.indexOf('iPad')!=-1||agent.indexOf('iOs')!=-1||agent.indexOf('Safari')!=-1)){
230-
var downloadUrl = ajxpServerAccessPath+'&get_action=download';
231-
downloadUrl = userSelection.updateFormOrUrl(null,downloadUrl);
232-
document.location.href=downloadUrl;
233-
}else{
234-
var minisite_session = null;
235-
var parts = $A(window.ajxpServerAccessPath.replace('?', '&').split('&'));
236-
parts.each(function(p){
237-
var sub = p.split('=');
238-
if(sub.length == 2 && sub[0] == 'minisite_session'){
239-
minisite_session = sub[1];
240-
throw $break;
241-
}
242-
});
243-
$('download_form').action = window.ajxpServerAccessPath;
244-
$('download_form').secure_token.value = Connexion.SECURE_TOKEN;
245-
$('download_form').select("input").each(function(input){
246-
if(input.name!='get_action' && input.name!='secure_token') input.remove();
247-
});
248-
if(minisite_session){
249-
$('download_form').insert(new Element('input', {type:'hidden', name:'minisite_session', value:minisite_session}));
250-
}
251-
userSelection.updateFormOrUrl($('download_form'));
252-
try{
253-
$('download_form').submit();
254-
}catch(e){
255-
256-
}
257-
}
228+
PydioApi.getClient().downloadSelection(userSelection, $('download_form'), 'download');
258229
}
259230
else
260231
{
@@ -279,7 +250,7 @@
279250
<div id="multi_download_form" title="AJXP_MESSAGE[118]" box_width="345">
280251
<div class="dialogLegend">AJXP_MESSAGE[119]</div><br/>
281252
<div id="multiple_download_container"></div>
282-
<form style="display:inline;" action="index.php" method="GET" id="download_form" target="download_iframe">
253+
<form style="display:inline;" action="index.php" method="POST" id="download_form" target="download_iframe">
283254
<input type="hidden" name="get_action" value="download">
284255
<input type="hidden" name="secure_token" value="">
285256
</form>

core/src/plugins/action.powerfs/class.PowerFSController.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,10 @@ public function switchAction($action, $httpVars, $fileVars)
7070
if ($httpVars["on_end"] == "reload") {
7171
AJXP_XMLWriter::triggerBgAction("reload_node", array(), "powerfs.2", true, 2);
7272
} else {
73-
$archiveName = $httpVars["archive_name"];
73+
$archiveName = AJXP_Utils::sanitize($httpVars["archive_name"], AJXP_SANITIZE_FILENAME);
74+
$archiveName = str_replace("'", "\'", $archiveName);
7475
$jsCode = "
75-
var regex = new RegExp('.*?[&\\?]' + 'minisite_session' + '=(.*?)&.*');
76-
val = window.ajxpServerAccessPath.replace(regex, \"\$1\");
77-
var minisite_session = ( val == window.ajxpServerAccessPath ? false : val );
78-
79-
$('download_form').action = window.ajxpServerAccessPath;
80-
$('download_form').secure_token.value = window.Connexion.SECURE_TOKEN;
81-
$('download_form').select('input').each(function(input){
82-
if(input.name!='secure_token') input.remove();
83-
});
84-
$('download_form').insert(new Element('input', {type:'hidden', name:'ope_id', value:'".$httpVars["ope_id"]."'}));
85-
$('download_form').insert(new Element('input', {type:'hidden', name:'archive_name', value:'".$archiveName."'}));
86-
$('download_form').insert(new Element('input', {type:'hidden', name:'get_action', value:'postcompress_download'}));
87-
if(minisite_session) $('download_form').insert(new Element('input', {type:'hidden', name:'minisite_session', value:minisite_session}));
88-
$('download_form').submit();
89-
$('download_form').get_action.value = 'download';
76+
PydioApi.getClient().downloadSelection(null, $('download_form'), 'postcompress_download', {ope_id:'".$httpVars["ope_id"]."',archive_name:'".$archiveName."'});
9077
";
9178
AJXP_XMLWriter::triggerBgJsAction($jsCode, $mess["powerfs.3"], true);
9279
AJXP_XMLWriter::triggerBgAction("reload_node", array(), "powerfs.2", true, 2);

core/src/plugins/action.powerfs/manifest.xml

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
var userSelection = ajaxplorer.getUserSelection();
4444
if((userSelection.isUnique() && !userSelection.hasDir()) || multipleFilesDownloadEnabled)
4545
{
46-
if(gaTrackEvent){
47-
fileNames = userSelection.getFileNames();
46+
if(window.gaTrackEvent){
47+
var fileNames = userSelection.getFileNames();
4848
for(var i=0; i<fileNames.length;i++){
49-
gaTrackEvent("Data", "Download", fileNames[i]);
49+
window.gaTrackEvent("Data", "Download", fileNames[i]);
5050
}
5151
}
5252
var agent = navigator.userAgent;
@@ -57,7 +57,7 @@
5757
}else{
5858
if( !userSelection.isUnique() || userSelection.hasDir() ){
5959
60-
zipName = getBaseName(userSelection.getContextNode().getPath());
60+
var zipName = getBaseName(userSelection.getContextNode().getPath());
6161
if(zipName == "") zipName = "Archive";
6262
var index=1;
6363
var buff = zipName;
@@ -82,21 +82,9 @@
8282
conn.sendAsync();
8383
8484
}else{
85-
var regex = new RegExp('.*?[&\\?]' + 'minisite_session' + '=(.*?)&.*');
86-
val = window.ajxpServerAccessPath.replace(regex, "$1");
87-
var minisite_session = ( val == window.ajxpServerAccessPath ? false : val );
8885
89-
$('download_form').action = window.ajxpServerAccessPath;
90-
$('download_form').secure_token.value = Connexion.SECURE_TOKEN;
91-
$('download_form').select("input").each(function(input){
92-
if(input.name!='get_action' && input.name!='secure_token') input.remove();
93-
if(input.name=='get_action') input.value = 'download';
94-
});
95-
if(minisite_session){
96-
$('download_form').insert(new Element('input', {type:'hidden', name:'minisite_session', value:minisite_session}));
97-
}
98-
userSelection.updateFormOrUrl($('download_form'));
99-
$('download_form').submit();
86+
PydioApi.getClient().downloadSelection(userSelection, $('download_form'));
87+
10088
}
10189
}
10290
}
@@ -106,7 +94,7 @@
10694
var dObject = oForm.getElementsBySelector('div[id="multiple_download_container"]')[0];
10795
var downloader = new MultiDownloader(dObject, ajxpServerAccessPath+'&action=download&file=');
10896
downloader.triggerEnd = function(){hideLightBox()};
109-
fileNames = userSelection.getFileNames();
97+
var fileNames = userSelection.getFileNames();
11098
for(var i=0; i<fileNames.length;i++)
11199
{
112100
downloader.addListRow(fileNames[i]);

core/src/plugins/gui.ajax/res/js/es6/http/PydioApi.es6

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,66 @@ class PydioApi{
7979

8080
}
8181

82+
/**
83+
*
84+
* @param userSelection UserSelection A Pydio DataModel with selected files
85+
* @param prototypeHiddenForm Element A hidden form element: currently relying on PrototypeJS.
86+
* @param dlActionName String Action name to trigger, download by default.
87+
* @param additionalParameters Object Optional set of key/values to pass to the download.
88+
*/
89+
downloadSelection(userSelection, prototypeHiddenForm = null, dlActionName='download', additionalParameters = {}){
90+
91+
var ajxpServerAccess = this._pydioObject.Parameters.get("ajxpServerAccess");
92+
var agent = navigator.userAgent || '';
93+
var agentIsMobile = (agent.indexOf('iPhone')!=-1||agent.indexOf('iPod')!=-1||agent.indexOf('iPad')!=-1||agent.indexOf('iOs')!=-1);
94+
if(agentIsMobile || !prototypeHiddenForm){
95+
var downloadUrl = ajxpServerAccess + '&get_action=' + dlActionName;
96+
if(additionalParameters){
97+
for(var param in additionalParameters){
98+
if(additionalParameters.hasOwnProperty(param)) downloadUrl += "&" + param + "=" + additionalParameters[param];
99+
}
100+
}
101+
if(userSelection){
102+
downloadUrl = userSelection.updateFormOrUrl(null,downloadUrl);
103+
}
104+
document.location.href=downloadUrl;
105+
}else{
106+
var minisite_session = null;
107+
var parts = ajxpServerAccess.replace('?', '&').split('&');
108+
parts.map(function(p){
109+
var sub = p.split('=');
110+
if(sub.length == 2 && sub[0] == 'minisite_session'){
111+
minisite_session = sub[1];
112+
}
113+
});
114+
prototypeHiddenForm.action = window.ajxpServerAccessPath;
115+
prototypeHiddenForm.secure_token.value = this._secureToken;
116+
prototypeHiddenForm.get_action.value = dlActionName;
117+
prototypeHiddenForm.select("input").each(function(input){
118+
if(input.name!='get_action' && input.name!='secure_token') input.remove();
119+
});
120+
if(minisite_session){
121+
prototypeHiddenForm.insert(new Element('input', {type:'hidden', name:'minisite_session', value:minisite_session}));
122+
}
123+
if(additionalParameters){
124+
for(var parameter in additionalParameters){
125+
if(additionalParameters.hasOwnProperty(parameter)) {
126+
prototypeHiddenForm.insert(new Element('input', {type:'hidden', name:parameter, value:additionalParameters[parameter]}));
127+
}
128+
}
129+
}
130+
if(userSelection) {
131+
userSelection.updateFormOrUrl(prototypeHiddenForm);
132+
}
133+
try{
134+
prototypeHiddenForm.submit();
135+
}catch(e){
136+
if(window.console) window.console.error("Error while submitting hidden form for download", e);
137+
}
138+
}
139+
140+
}
141+
82142
static supportsUpload(){
83143
if(window.Connexion){
84144
return (window.FormData || window.FileReader);

0 commit comments

Comments
 (0)