Final modifications.

This commit is contained in:
Lars Jung 2012-04-19 00:57:43 +02:00
parent de92767e5a
commit ade6cf8e0b
24 changed files with 316 additions and 275 deletions

View file

@ -2,7 +2,11 @@
class Archive {
private $h5ai;
private static $TAR_CMD = "$(cd [ROOTDIR] && tar --no-recursion -cf [TARGET] [DIRS] [FILES])";
private static $ZIP_CMD = "$(cd [ROOTDIR] && zip [TARGET] [FILES])";
private $h5ai, $dirs, $files, $sc401;
public function __construct($h5ai) {
@ -11,66 +15,116 @@ class Archive {
}
public function create($format, $hrefs) {
public function create($execution, $format, $hrefs) {
$target = H5ai::normalize_path(sys_get_temp_dir(), true) . "h5ai-selection-" . microtime(true) . "-" . rand() . "." . $format;
$archive = new PharData($target);
$this->dirs = array();
$this->files = array();
$this->sc401 = false;
$this->addHrefs($hrefs);
if ($this->sc401) {
return 401;
} else if (count($this->dirs) === 0 && count($this->files) === 0) {
return 404;
}
$target = H5ai::normalize_path(sys_get_temp_dir(), true) . "h5ai-selection-" . microtime(true) . rand() . "." . $format;
try {
if ($execution === "shell") {
if ($format === "tar") {
$cmd = Archive::$TAR_CMD;
} else if ($format === "zip") {
$cmd = Archive::$ZIP_CMD;
} else {
return null;
}
$cmd = str_replace("[ROOTDIR]", "\"" . $this->h5ai->getRootAbsPath() . "\"", $cmd);
$cmd = str_replace("[TARGET]", "\"" . $target . "\"", $cmd);
$cmd = str_replace("[DIRS]", count($this->dirs) ? "\"" . implode("\" \"", array_values($this->dirs)) . "\"" : "", $cmd);
$cmd = str_replace("[FILES]", count($this->files) ? "\"" . implode("\" \"", array_values($this->files)) . "\"" : "", $cmd);
`$cmd`;
} else if ($execution === "php") {
$archive = new PharData($target);
foreach ($this->dirs as $archivedDir) {
$archive->addEmptyDir($archivedDir);
}
foreach ($this->files as $realFile => $archivedFile) {
$archive->addFile($realFile, $archivedFile); // very, very slow :/
}
}
} catch (Exeption $err) {
return 500;
}
return @filesize($target) ? $target : null;
}
private function addHrefs($hrefs) {
foreach ($hrefs as $href) {
$d = H5ai::normalize_path(dirname($href), true);
$n = basename($href);
$code = $this->h5ai->getHttpCode($d);
if ($code == 401) {
return $code;
$this->sc401 = true;
}
if ($code == "h5ai" && !$this->h5ai->ignoreThisFile($n)) {
$realFile = $this->h5ai->getAbsPath($href);
$archivedFile = preg_replace("!^" . $this->h5ai->getRootAbsPath() . "!", "", $realFile);
$archivedFile = preg_replace("!^" . H5ai::normalize_path($this->h5ai->getRootAbsPath(), true) . "!", "", $realFile);
if (is_dir($realFile)) {
$rcode = $this->addDir($archive, $realFile, $archivedFile);
if ($rcode == 401) {
return $rcode;
}
$this->addDir($realFile, $archivedFile);
} else {
$this->addFile($archive, $realFile, $archivedFile);
$this->addFile($realFile, $archivedFile);
}
}
}
return filesize($target) ? $target : null;
}
private function addFile($archive, $realFile, $archivedFile) {
private function addFile($realFile, $archivedFile) {
if (is_readable($realFile)) {
$archive->addFile($realFile, $archivedFile);
$this->files[$realFile] = $archivedFile;
}
}
private function addDir($archive, $realDir, $archivedDir) {
private function addDir($realDir, $archivedDir) {
$code = $this->h5ai->getHttpCode($this->h5ai->getAbsHref($realDir));
if ($code == 401) {
$this->sc401 = true;
}
if ($code == "h5ai") {
$archive->addEmptyDir($archivedDir);
$this->dirs[] = $archivedDir;
$files = $this->h5ai->readDir($realDir);
foreach ($files as $file) {
$realFile = $realDir . "/" . $file;
$archivedFile = $archivedDir . "/" . $file;
if (is_dir($realFile)) {
$rcode = $this->addDir($archive, $realFile, $archivedFile);
if ($rcode == 401) {
return $rcode;
}
$this->addDir($realFile, $archivedFile);
} else {
$this->addFile($archive, $realFile, $archivedFile);
$this->addFile($realFile, $archivedFile);
}
}
}
return $code;
}
}