Files
file-explorer/src/Objects/DirContent.php
jank1619 a67b93ac99 Implement image previewing
I have tested this with png and jpeg/jpg which both work but most other image types should work as well.
![image](/attachments/25318577-d48c-4902-93b3-9adbb8b954e7)

Co-authored-by: Jan Klattenhoff <j.klattenhoff@neusta.de>
Reviewed-on: sites/file-explorer#12
Co-authored-by: jank1619 <jan@kjan.email>
Co-committed-by: jank1619 <jan@kjan.email>
2024-12-21 18:12:14 +00:00

76 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Objects;
use SplFileInfo;
readonly class DirContent
{
private function __construct(
private string $name,
private int $size,
private string $type,
private string $path,
private string $content,
private string $mimeType,
) {
}
public static function make(SplFileInfo $fileInfo, string $content = ''): DirContent
{
return new self(
$fileInfo->getBasename(),
$fileInfo->getSize() ?? 0,
$fileInfo->getType() ?? 'N/A',
$fileInfo->getPath(),
$content,
mime_content_type($fileInfo->getPath() . '/' . $fileInfo->getFilename()),
);
}
public function getName(): string
{
return $this->name;
}
public function getSize(bool $humanReadable = true): int|string
{
if (!$humanReadable) {
return $this->size;
}
return $this->getHumanReadableSize();
}
public function getType(): string
{
return $this->type;
}
public function getPath(): string
{
return $this->path;
}
public function getContent(): string
{
return $this->content;
}
public function getMimeType(): string
{
return $this->mimeType;
}
private function getHumanReadableSize(): string
{
$bytes = $this->size;
$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]);
}
}