From 344c0a8005b5d6dbe65ca6f313d118385ab5afd2 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Sun, 18 May 2014 22:04:06 +0200 Subject: [PATCH] More refactorings. --- src/_h5ai/conf/options.json | 8 +++- src/_h5ai/server/php/inc/class-api.php | 28 +++--------- src/_h5ai/server/php/inc/class-app.php | 8 +--- src/_h5ai/server/php/inc/class-item.php | 54 +++++++++++++++++------- src/_h5ai/server/php/inc/class-thumb.php | 20 +-------- 5 files changed, 55 insertions(+), 63 deletions(-) diff --git a/src/_h5ai/conf/options.json b/src/_h5ai/conf/options.json index 477c219c..a31a5987 100644 --- a/src/_h5ai/conf/options.json +++ b/src/_h5ai/conf/options.json @@ -135,9 +135,15 @@ Options /* Calc the size of folders. + This operation is real slow. The calculated sizes differ slightly for both + calculation types since "php" only adds the file size, while "shell-du" + also adds the sizes for the actual folder files. + + - type: "php" (sloooow) or "shell-du" (sloow) */ "foldersize": { - "enabled": true + "enabled": true, + "type": "php" }, /* diff --git a/src/_h5ai/server/php/inc/class-api.php b/src/_h5ai/server/php/inc/class-api.php index 914ff0a4..16e26611 100644 --- a/src/_h5ai/server/php/inc/class-api.php +++ b/src/_h5ai/server/php/inc/class-api.php @@ -15,7 +15,6 @@ class Api { public function apply() { $options = $this->app->get_options(); - $action = use_request_param("action"); if ($action === "get") { @@ -80,14 +79,8 @@ class Api { else if ($action === "getThumbHref") { - if (!$options["thumbnails"]["enabled"]) { - json_fail(1, "thumbnails disabled"); - } - - normalized_require_once("class-thumb"); - if (!Thumb::is_supported()) { - json_fail(2, "thumbnails not supported"); - } + json_fail(1, "thumbnails disabled", !$options["thumbnails"]["enabled"]); + json_fail(2, "thumbnails not supported", !HAS_PHP_JPG); $type = use_request_param("type"); $src_url = use_request_param("href"); @@ -97,9 +90,7 @@ class Api { $thumb = new Thumb($this->app); $thumb_url = $thumb->thumb($type, $src_url, $mode, $width, $height); - if ($thumb_url === null) { - json_fail(3, "thumbnail creation failed"); - } + json_fail(3, "thumbnail creation failed", $thumb_url === null); json_exit(array("absHref" => $thumb_url)); } @@ -113,7 +104,6 @@ class Api { $type = use_request_param("type"); $hrefs = use_request_param("hrefs"); - normalized_require_once("class-archive"); $archive = new Archive($this->app); $hrefs = explode("|:|", trim($hrefs)); @@ -124,9 +114,7 @@ class Api { header("Connection: close"); $rc = $archive->output($type, $hrefs); - if ($rc !== 0) { - json_fail("packaging failed"); - } + json_fail("packaging failed", $rc !== 0); exit; } @@ -152,7 +140,6 @@ class Api { json_fail(6, "already exists", file_exists($dest)); json_fail(7, "can't move uploaded file", !move_uploaded_file($userfile["tmp_name"], $dest)); - json_exit(); } @@ -183,11 +170,8 @@ class Api { } } - if (count($errors)) { - json_fail(2, "deletion failed for some"); - } else { - json_exit(); - } + json_fail(2, "deletion failed for some", count($errors) > 0); + json_exit(); } diff --git a/src/_h5ai/server/php/inc/class-app.php b/src/_h5ai/server/php/inc/class-app.php index 3d02ce31..74ee756e 100644 --- a/src/_h5ai/server/php/inc/class-app.php +++ b/src/_h5ai/server/php/inc/class-app.php @@ -113,12 +113,6 @@ class App { } - public function get_generic_json() { - - return json_encode(array("items" => $this->get_items(CURRENT_URL, 1))) . "\n"; - } - - public function get_items($url, $what) { $code = $this->get_http_code($url); @@ -157,7 +151,9 @@ class App { $cache = array(); $folder = Item::get($this, CURRENT_PATH, $cache); + time_log("f2"); $items = $folder->get_content($cache); + time_log("f3"); uasort($items, array("Item", "cmp")); $html = ""; diff --git a/src/_h5ai/server/php/inc/class-item.php b/src/_h5ai/server/php/inc/class-item.php index 94d437da..dd14a8a6 100644 --- a/src/_h5ai/server/php/inc/class-item.php +++ b/src/_h5ai/server/php/inc/class-item.php @@ -14,10 +14,46 @@ class Item { return strcasecmp($item1->path, $item2->path); } + + private static $size_cache = array(); + + + private static function filesize($app, $path) { + + if (array_key_exists($path, Item::$size_cache)) { + return Item::$size_cache[$path]; + } + + $size = 0; + + if (is_file($path)) { + + $size = @filesize($path); + + } else if (is_dir($path)) { + + $options = $app->get_options(); + if ($options["foldersize"]["enabled"]) { + if (HAS_CMD_DU && $options["foldersize"]["type"] === "shell-du") { + $cmdv = array("du", "-sk", $path); + $size = intval(preg_replace("#\s.*$#", "", exec_cmdv($cmdv)), 10) * 1024; + } else { + foreach ($app->read_dir($path) as $name) { + $size += Item::filesize($app, $path . "/" . $name); + } + } + } + } + + Item::$size_cache[$path] = $size; + return $size; + } + + public static function get($app, $path, &$cache) { if (!starts_with($path, ROOT_PATH)) { - error_log("ILLEGAL REQUEST: " . $path . ", " . ROOT_PATH); + err_log("ILLEGAL REQUEST: " . $path . ", " . ROOT_PATH); return null; } @@ -47,21 +83,9 @@ class Item { $this->path = normalize_path($path, false); $this->is_folder = is_dir($this->path); - $this->url = $this->app->to_url($path, $this->is_folder); - + $this->url = $app->to_url($this->path, $this->is_folder); $this->date = @filemtime($this->path); - - if ($this->is_folder) { - $this->size = null; - $options = $app->get_options(); - if ($options["foldersize"]["enabled"]) { - $cmdv = array("du", "-sk", $this->path); - $this->size = intval(preg_replace("#\s.*$#", "", exec_cmdv($cmdv)), 10) * 1024; - } - } else { - $this->size = @filesize($this->path); - } - + $this->size = Item::filesize($app, $this->path); $this->is_content_fetched = false; } diff --git a/src/_h5ai/server/php/inc/class-thumb.php b/src/_h5ai/server/php/inc/class-thumb.php index 2412046f..49cc9eaf 100644 --- a/src/_h5ai/server/php/inc/class-thumb.php +++ b/src/_h5ai/server/php/inc/class-thumb.php @@ -6,13 +6,6 @@ class Thumb { private static $AVCONV_CMD = "avconv -ss 0:01:00 -i [SOURCE] -an -vframes 1 [TARGET]"; private static $CONVERT_CMD = "convert -strip [SOURCE][0] [TARGET]"; private static $THUMB_CACHE = "thumbs"; - private static $CAPTURE_CACHE = "captures"; - - - public static final function is_supported() { - - return Image::is_supported(); - } private $app, $thumbs_path, $thumbs_href; @@ -65,7 +58,7 @@ class Thumb { $et = false; $opts = $this->app->get_options(); - if ($opts["thumbnails"]["exif"] === true && function_exists("exif_thumbnail")) { + if (HAS_PHP_EXIF && $opts["thumbnails"]["exif"] === true) { $et = @exif_thumbnail($source_path); } if($et !== false) { @@ -116,17 +109,6 @@ class Image { private $source_file, $source, $width, $height, $type, $dest; - public static final function is_supported() { - - if (!function_exists("gd_info")) { - return false; - } - - $gdinfo = gd_info(); - return array_key_exists("JPG Support", $gdinfo) && $gdinfo["JPG Support"] || array_key_exists("JPEG Support", $gdinfo) && $gdinfo["JPEG Support"]; - } - - public function __construct($filename = null) { $this->source_file = null;