first commit
This commit is contained in:
commit
788cf3e917
47 changed files with 2007 additions and 0 deletions
33
app/FileViewer.php
Normal file
33
app/FileViewer.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace MingTsay\Akanyan;
|
||||
|
||||
abstract class FileViewer
|
||||
{
|
||||
protected abstract static function directory(): string;
|
||||
|
||||
protected abstract static function whitelist(): ?array;
|
||||
|
||||
public static function list(): array
|
||||
{
|
||||
$directory = static::directory();
|
||||
$whitelist = static::whitelist();
|
||||
|
||||
return array_values(array_filter(
|
||||
scandir($directory),
|
||||
fn($file) => ($whitelist === null || in_array($file, $whitelist)) && is_file("$directory/$file")
|
||||
));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue