diff --git a/src/_h5ai/server/php/inc/class-item.php b/src/_h5ai/server/php/inc/class-item.php index 69cbf9ea..8245d764 100644 --- a/src/_h5ai/server/php/inc/class-item.php +++ b/src/_h5ai/server/php/inc/class-item.php @@ -15,68 +15,6 @@ class Item { } - 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 = null; - - if (is_file($path)) { - - if (PHP_INT_SIZE < 8) { - $_handle = fopen($path, "r"); - - $_pos = 0; - $_size = 1073741824; - fseek($_handle, 0, SEEK_SET); - while ($_size > 1) { - fseek($_handle, $_size, SEEK_CUR); - - if (fgetc($_handle) === false) { - fseek($_handle, -$_size, SEEK_CUR); - $_size = (int)($_size / 2); - } else { - fseek($_handle, -1, SEEK_CUR); - $_pos += $_size; - } - } - - while (fgetc($_handle) !== false) { - $_pos++; - } - fclose($_handle); - - $size = $_pos; - } else { - $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.*$#", "", Util::exec_cmdv($cmdv)), 10) * 1024; - } else { - $size = 0; - 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 (!Util::starts_with($path, ROOT_PATH)) { @@ -111,7 +49,7 @@ class Item { $this->is_folder = is_dir($this->path); $this->url = $app->to_url($this->path, $this->is_folder); $this->date = @filemtime($this->path); - $this->size = Item::filesize($app, $this->path); + $this->size = Util::filesize($app, $this->path); $this->is_content_fetched = false; // $options = $app->get_options(); diff --git a/src/_h5ai/server/php/inc/class-util.php b/src/_h5ai/server/php/inc/class-util.php index adece248..8e523dfd 100644 --- a/src/_h5ai/server/php/inc/class-util.php +++ b/src/_h5ai/server/php/inc/class-util.php @@ -114,4 +114,65 @@ class Util { } catch (Exception $e) {} return false; } + + + private static $size_cache = array(); + + public static function filesize($app, $path) { + + if (array_key_exists($path, Util::$size_cache)) { + return Util::$size_cache[$path]; + } + + $size = null; + + if (is_file($path)) { + + if (PHP_INT_SIZE < 8) { + $_handle = fopen($path, "r"); + + $_pos = 0; + $_size = 1073741824; + fseek($_handle, 0, SEEK_SET); + while ($_size > 1) { + fseek($_handle, $_size, SEEK_CUR); + + if (fgetc($_handle) === false) { + fseek($_handle, -$_size, SEEK_CUR); + $_size = (int)($_size / 2); + } else { + fseek($_handle, -1, SEEK_CUR); + $_pos += $_size; + } + } + + while (fgetc($_handle) !== false) { + $_pos++; + } + fclose($_handle); + + $size = $_pos; + } else { + $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.*$#", "", Util::exec_cmdv($cmdv)), 10) * 1024; + } else { + $size = 0; + foreach ($app->read_dir($path) as $name) { + $size += Util::filesize($app, $path . "/" . $name); + } + } + } + } + + Util::$size_cache[$path] = $size; + return $size; + } }