add file preview

Reviewed-on: sites/file-explorer#6
This commit is contained in:
2024-12-06 13:04:56 +00:00
parent a574c3a89a
commit 12730b8bcd
10 changed files with 94 additions and 19 deletions

View File

@ -6,22 +6,25 @@ namespace App\Objects;
use SplFileInfo;
class DirContent
readonly class DirContent
{
private function __construct(
private readonly string $name,
private readonly int $size,
private readonly string $type,
)
{
private string $name,
private int $size,
private string $type,
private string $path,
private string $content,
) {
}
public static function make(SplFileInfo $fileInfo): DirContent
public static function make(SplFileInfo $fileInfo, string $content = ''): DirContent
{
return new self(
$fileInfo->getBasename(),
$fileInfo->getSize() ?? 0,
$fileInfo->getType() ?? 'N/A',
$fileInfo->getPath(),
$content
);
}
@ -44,10 +47,20 @@ class DirContent
return $this->type;
}
private function getHumanReadableSize()
public function getPath(): string
{
return $this->path;
}
public function getContent(): string
{
return $this->content;
}
private function getHumanReadableSize(): string
{
$bytes = $this->size;
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
$factor = floor((strlen((string) $bytes) - 1) / 3);
return sprintf("%.1f %s", $bytes / (1024 ** $factor), $size[$factor]);