diff --git a/README.md b/README.md index f71a4470..208825f8 100644 --- a/README.md +++ b/README.md @@ -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* diff --git a/src/_h5ai/css/inc/main.less b/src/_h5ai/css/inc/main.less index b1df578f..55ed8cd1 100644 --- a/src/_h5ai/css/inc/main.less +++ b/src/_h5ai/css/inc/main.less @@ -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); + } } } diff --git a/src/_h5ai/js/inc/ZippedDownload.js b/src/_h5ai/js/inc/ZippedDownload.js index b2388ab0..b9afae6d 100644 --- a/src/_h5ai/js/inc/ZippedDownload.js +++ b/src/_h5ai/js/inc/ZippedDownload.js @@ -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) { $("
  • downloaddownload
  • ") .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); diff --git a/src/_h5ai/php/api.php b/src/_h5ai/php/api.php index c3d12b30..72330882 100644 --- a/src/_h5ai/php/api.php +++ b/src/_h5ai/php/api.php @@ -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); diff --git a/src/_h5ai/php/inc/ZipIt.php b/src/_h5ai/php/inc/ZipIt.php index 5e2c9022..cb67a953 100644 --- a/src/_h5ai/php/inc/ZipIt.php +++ b/src/_h5ai/php/inc/ZipIt.php @@ -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; }