Add proper download started feedback

* this code adds a cookie when a special get parameter is set
* the content of this get parameter is used as value for the cookie
* the cookie expires after 20 seconds
* the JS code checks every 500 milliseconds for the cookie
  -> if the cookie is set the request returned and the download is started
This commit is contained in:
Morris Jobke 2015-06-29 15:20:47 +02:00
parent 5a528214b8
commit e557fe0aab
3 changed files with 59 additions and 12 deletions

View File

@ -39,4 +39,15 @@ if (!is_array($files_list)) {
$files_list = array($files);
}
/**
* this sets a cookie to be able to recognize the start of the download
* the content must not be longer than 32 characters and must only contain
* alphanumeric characters
*/
if(isset($_GET['downloadStartSecret'])
&& !isset($_GET['downloadStartSecret'][32])
&& preg_match('!^[a-zA-Z0-9]+$!', $_GET['downloadStartSecret']) === 1) {
setcookie('ocDownloadStarted', $_GET['downloadStartSecret'], time() + 20, '/');
}
OC_Files::get($dir, $files_list, $_SERVER['REQUEST_METHOD'] == 'HEAD');

View File

@ -491,14 +491,31 @@
var sourceImage = icon.attr('src');
icon.attr('src', sourceImage.replace('actions/download.svg', 'loading-small.gif'));
// TODO proper detection of "download has started"
setTimeout(function(){
icon.attr('src', sourceImage);
downloadFileaction.removeClass('disabled');
}, 2000);
var randomString = Math.random().toString(36).substring(2);
var isCookieSet = function(name, value) {
var cookies = document.cookie.split(';');
for (var i=0; i < cookies.length; i++) {
var cookie = cookies[i].split('=');
if (cookie[0].trim() === name && cookie[1].trim() === value) {
return true;
}
}
return false;
};
var checkForDownloadCookie = function() {
if (!isCookieSet('ocDownloadStarted', randomString)){
setTimeout(checkForDownloadCookie, 500);
} else {
icon.attr('src', sourceImage);
downloadFileaction.removeClass('disabled');
}
};
if (url) {
OC.redirect(url);
OC.redirect(url + '&downloadStartSecret=' + randomString);
checkForDownloadCookie();
}
}, t('files', 'Download'));
}

View File

@ -431,13 +431,32 @@
var sourceImage = icon.attr('src');
icon.attr('src', sourceImage.replace('actions/download.svg', 'loading-small.gif'));
// TODO proper detection of "download has started"
setTimeout(function(){
icon.attr('src', sourceImage);
downloadFileaction.removeClass('disabled');
}, 2000);
var randomString = Math.random().toString(36).substring(2);
OC.redirect(this.getDownloadUrl(files, dir));
var isCookieSet = function(name, value) {
var cookies = document.cookie.split(';');
for (var i=0; i < cookies.length; i++) {
var cookie = cookies[i].split('=');
if (cookie[0].trim() === name && cookie[1].trim() === value) {
return true;
}
}
return false;
};
var checkForDownloadCookie = function() {
console.log('check');
if (!isCookieSet('ocDownloadStarted', randomString)){
setTimeout(checkForDownloadCookie, 500);
} else {
console.log('boom');
icon.attr('src', sourceImage);
downloadFileaction.removeClass('disabled');
}
};
OC.redirect(this.getDownloadUrl(files, dir) + '&downloadStartSecret=' + randomString);
checkForDownloadCookie();
return false;
},