38 lines
1 KiB
PHP
38 lines
1 KiB
PHP
<?php
|
|
|
|
namespace MingTsay\Akanyan;
|
|
|
|
abstract class FileViewer
|
|
{
|
|
protected static bool $orderDesc = false;
|
|
|
|
protected abstract static function directory(): string;
|
|
|
|
protected abstract static function whitelist(): ?array;
|
|
|
|
public static function list(): array
|
|
{
|
|
$directory = static::directory();
|
|
$whitelist = static::whitelist();
|
|
|
|
$list = array_values(array_filter(
|
|
scandir($directory),
|
|
fn($file) => ($whitelist === null || in_array($file, $whitelist)) && is_file("$directory/$file")
|
|
));
|
|
|
|
if (static::$orderDesc) rsort($list);
|
|
return $list;
|
|
}
|
|
|
|
public static function read($file): ?string
|
|
{
|
|
$directory = static::directory();
|
|
$filename = "$directory/$file";
|
|
|
|
if (!in_array($file, self::list()) || !file_exists($filename) || !is_file($filename)) return null;
|
|
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
|
$content = file_get_contents($filename);
|
|
return $extension === 'gz' ? gzdecode($content) : $content;
|
|
}
|
|
}
|