Improves zipped download.

This commit is contained in:
Lars Jung 2012-02-17 23:21:13 +01:00
parent f51a0b80a7
commit bc6e9fb150
5 changed files with 74 additions and 15 deletions

View file

@ -38,6 +38,8 @@ h5ai is provided under the terms of the [MIT License](http://github.com/lrsjng/h
* fixes path problems on servers running on Windows in PHP version
* fixes broken links in custom headers/footers while zipped download enabled
* fixes problems with thumbnails for files with single or double quotes in filename
* improves zipped download
### v0.17 - *2011-11-28*

View file

@ -94,6 +94,14 @@ body > nav {
display: none;
float: right;
border-left: 1px solid rgb(231,231,231);
.transition(all 0.2s ease-in-out);
&.zipping {
}
&.failed {
background-color: rgba(255,0,0,0.5);
}
}
}

View file

@ -7,21 +7,22 @@
y = 0,
$document = $(document),
$selectionRect = $("#selection-rect"),
selectedHrefsStr = "",
updateDownloadBtn = function () {
var $selected = $("#extended a.selected"),
$downloadBtn = $("#download"),
query, href;
$downloadBtn = $("#download");
if ($selected.size() > 0) {
selectedHrefsStr = "";
if ($selected.length) {
$selected.each(function () {
href = $(this).attr("href");
query = query ? query + ":" + href : href;
var href = $(this).attr("href");
selectedHrefsStr = selectedHrefsStr ? selectedHrefsStr + ":" + href : href;
});
query = H5AI.core.api() + "?action=zip&hrefs=" + query;
$downloadBtn.show().find("a").attr("href", query);
$downloadBtn.show();
} else {
$downloadBtn.hide().find("a").attr("href", "#");
$downloadBtn.hide();
}
},
selectionUpdate = function (event) {
@ -94,6 +95,39 @@
if (H5AI.core.settings.zippedDownload) {
$("<li id='download'><a href='#'><img alt='download' /><span class='l10n-download'>download</span></a></li>")
.find("img").attr("src", H5AI.core.image("download")).end()
.find("a").click(function () {
$('#download').addClass('zipping');
$('#download img').attr('src', H5AI.core.image("loading"));
$.ajax({
url: H5AI.core.api(),
data: {
action: 'zip',
hrefs: selectedHrefsStr
},
type: 'POST',
dataType: 'json',
success: function (response) {
$('#download img').attr('src', H5AI.core.image("download"));
$('#download').removeClass('zipping');
if (response.status === 'ok') {
console.log("download worked!", response);
window.location = H5AI.core.api() + '?action=getzip&id=' + response.id;
} else {
console.log("download failed!", response);
$('#download').addClass('failed');
setTimeout(function () {
$('#download').removeClass('failed');
}, 1000);
}
},
failed: function () {
$('#download img').attr('src', H5AI.core.image("download"));
$('#download').removeClass('zipping');
}
});
}).end()
.appendTo($("#navbar"));
$("body>nav,body>footer,#tree").on("mousedown", noSelection);

View file

@ -118,12 +118,26 @@ else if ($action === "zip") {
$hrefs = explode(":", trim($hrefs));
$zipFile = $zipit->zip($hrefs);
if ($zipFile === false) {
fail(2, "something went wrong while building the zip");
if ($zipFile) {
$response = array('status' => 'ok', 'id' => basename($zipFile), 'size' => filesize($zipFile));
} else {
$response = array('status' => 'failed', 'msg' => 'none');
}
echo json_encode($response);
}
else if ($action === "getzip") {
list($id) = checkKeys(array("id"));
fail(1, "zipped file not found: " . $id, !preg_match("/^h5ai-zip-/", $id));
$zipFile = str_replace("\\", "/", sys_get_temp_dir()) . "/" . $id;
fail(2, "zipped file not found: " . $id, !file_exists($zipFile));
header("Content-Disposition: attachment; filename=\"h5ai-selection.zip\"");
header("Content-Type: application/force-download");
// header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($zipFile));
header("Connection: close");
readfile($zipFile);

View file

@ -13,17 +13,18 @@ class ZipIt {
public function zip($hrefs) {
$zipFile = tempnam("/tmp", "h5ai-download");
$zipFile = tempnam(sys_get_temp_dir(), "h5ai-zip-");
$zip = new ZipArchive();
if (!$zip->open($zipFile, ZIPARCHIVE::CREATE)) {
return false;
return null;
}
$zip->addEmptyDir("/");
foreach ($hrefs as $href) {
$d = safe_dirname($href, true);
$n = basename($href);
if ($this->h5ai->getHttpCode($this->h5ai->getAbsHref($d)) === "h5ai" && !$this->h5ai->ignoreThisFile($n)) {
if ($this->h5ai->getHttpCode($d) === "h5ai" && !$this->h5ai->ignoreThisFile($n)) {
$localFile = $this->h5ai->getAbsPath($href);
$file = preg_replace("!^" . $this->h5ai->getRootAbsPath() . "!", "", $localFile);
if (is_dir($localFile)) {
@ -35,7 +36,7 @@ class ZipIt {
}
$zip->close();
return $zipFile;
return filesize($zipFile) ? $zipFile : null;
}