mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-28 14:04:45 -04:00
Improves zipped download.
This commit is contained in:
parent
f51a0b80a7
commit
bc6e9fb150
5 changed files with 74 additions and 15 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue