mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-29 22:45:14 -04:00
Move filesize to Util.
This commit is contained in:
parent
943de9045d
commit
8d3723c0f1
2 changed files with 62 additions and 63 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue