mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-25 20:44:53 -04:00
Move filesize to Util.
This commit is contained in:
parent
943de9045d
commit
8d3723c0f1
2 changed files with 62 additions and 63 deletions
|
@ -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) {
|
public static function get($app, $path, &$cache) {
|
||||||
|
|
||||||
if (!Util::starts_with($path, ROOT_PATH)) {
|
if (!Util::starts_with($path, ROOT_PATH)) {
|
||||||
|
@ -111,7 +49,7 @@ class Item {
|
||||||
$this->is_folder = is_dir($this->path);
|
$this->is_folder = is_dir($this->path);
|
||||||
$this->url = $app->to_url($this->path, $this->is_folder);
|
$this->url = $app->to_url($this->path, $this->is_folder);
|
||||||
$this->date = @filemtime($this->path);
|
$this->date = @filemtime($this->path);
|
||||||
$this->size = Item::filesize($app, $this->path);
|
$this->size = Util::filesize($app, $this->path);
|
||||||
$this->is_content_fetched = false;
|
$this->is_content_fetched = false;
|
||||||
|
|
||||||
// $options = $app->get_options();
|
// $options = $app->get_options();
|
||||||
|
|
|
@ -114,4 +114,65 @@ class Util {
|
||||||
} catch (Exception $e) {}
|
} catch (Exception $e) {}
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue