Adds workaround to use direct POST-request downloads.

This commit is contained in:
Lars Jung 2013-07-18 23:25:53 +02:00
parent adce6f19c4
commit ab45b3072c
3 changed files with 12 additions and 116 deletions

View file

@ -27,34 +27,6 @@ modulejs.define('ext/download', ['_', '$', 'core/settings', 'core/resource', 'co
}, 1000);
},
handleResponse = function (json) {
$download.removeClass('current');
$img.attr('src', resource.image('download'));
if (json && json.code === 0) {
setTimeout(function () { // wait here so the img above can be updated in time
window.location = '?action=getArchive&id=' + json.id + '&as=' + (settings.packageName || location.getItem().label) + '.' + settings.format;
}, 200);
} else {
failed();
}
},
requestArchive = function (hrefsStr) {
$download.addClass('current');
$img.attr('src', resource.image('loading.gif', true));
server.request({
action: 'createArchive',
type: settings.type,
format: settings.format,
hrefs: hrefsStr
}, handleResponse);
},
onSelection = function (items) {
selectedHrefsStr = '';
@ -72,15 +44,19 @@ modulejs.define('ext/download', ['_', '$', 'core/settings', 'core/resource', 'co
onClick = function (event) {
var type = settings.type,
extension = type === 'shell-zip' ? 'zip' : 'tar',
query = '?action=passArchive' +
'&as=' + encodeURIComponent((settings.packageName || location.getItem().label) + '.' + extension) +
'&type=' + type +
'&hrefs=' + encodeURIComponent(selectedHrefsStr),
$iframe = $('<iframe src="' + query + '" style="display: none;" />');
extension = (type === 'shell-zip') ? 'zip' : 'tar',
query = {
action: 'passArchive',
as: (settings.packageName || location.getItem().label) + '.' + extension,
type: type,
hrefs: selectedHrefsStr
},
$form = $('<form action="#" method="post" style="display:none;" />');
$iframe.appendTo('body');
setTimeout(function () { $iframe.remove(); }, 1000);
for (var key in query) {
$form.append('<input type="hidden" name="' + key + '" value="' + query[key] + '" />')
}
$form.appendTo('body').submit().remove();
},
init = function () {

View file

@ -100,57 +100,6 @@ class Api {
}
else if ($action === "createArchive") {
json_fail(1, "downloads disabled", !$options["download"]["enabled"]);
list($format, $hrefs) = use_request_params(array("format", "hrefs"));
normalized_require_once("/server/php/inc/Archive.php");
$archive = new Archive($this->app);
$hrefs = explode("|:|", trim($hrefs));
$target = $archive->create($format, $hrefs);
if (!is_string($target)) {
json_fail($target, "package creation failed");
}
json_exit(array("id" => basename($target), "size" => filesize($target)));
}
else if ($action === "getArchive") {
json_fail(1, "downloads disabled", !$options["download"]["enabled"]);
list($id, $as) = use_request_params(array("id", "as"));
json_fail(2, "file not found", !preg_match("/^package-/", $id));
$target = $this->app->get_cache_abs_path() . "/" . $id;
json_fail(3, "file not found", !file_exists($target));
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($target));
header("Content-Disposition: attachment; filename=\"$as\"");
header("Connection: close");
register_shutdown_function("delete_tempfile", $target);
// readfile($target);
// Send data in small segments of 16MB to not hit PHP's memory limit (default: 128M)
if ($fd = fopen($target, 'rb')) {
set_time_limit(0);
while (!feof($fd)) {
print fread($fd, 1024 * 1024 * 16);
ob_flush();
flush();
}
fclose($fd);
}
}
else if ($action === "passArchive") {
json_fail(1, "downloads disabled", !$options["download"]["enabled"]);

View file

@ -41,35 +41,6 @@ class Archive {
}
// public function create($format, $hrefs) {
// $this->dirs = array();
// $this->files = array();
// $this->add_hrefs($hrefs);
// if (count($this->dirs) === 0 && count($this->files) === 0) {
// return 404;
// }
// $target = $this->app->get_cache_abs_path() . "/package-" . sha1(microtime(true) . rand()) . "." . $format;
// try {
// $archive = new PharData($target);
// foreach ($this->dirs as $archived_dir) {
// $archive->addEmptyDir($archived_dir);
// }
// foreach ($this->files as $real_file => $archived_file) {
// $archive->addFile($real_file, $archived_file); // very, very slow :/
// }
// } catch (Exeption $err) {
// return 500;
// }
// return @filesize($target) ? $target : null;
// }
private function shell_cmd($cmd) {
$cmd = str_replace("[ROOTDIR]", "\"" . $this->app->get_abs_path() . "\"", $cmd);