From 9ec43e8d0cd1dded7f3554091ff39fb7483b0bb4 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Mon, 9 Sep 2024 21:07:42 +0200 Subject: [PATCH] Automatically generate file props from path --- src/BulkyItem/FileItem.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/BulkyItem/FileItem.php b/src/BulkyItem/FileItem.php index dac914d..38958c7 100644 --- a/src/BulkyItem/FileItem.php +++ b/src/BulkyItem/FileItem.php @@ -53,12 +53,24 @@ public static function restore($contents, array $meta): BulkyItemInterface return new self($contents, $meta['name'], $meta['type'], $meta['size']); } - public static function fromPath(string $path, string $name, string $mimeType, int $size): self + public static function fromPath(string $path, string|null $name = null, string|null $mimeType = null, int|null $size = null): self { if (!(new Filesystem())->exists($path)) { throw new \InvalidArgumentException(\sprintf('The file "%s" does not exist.', $path)); } + if (null === $name) { + $name = basename($path); + } + + if (null === $mimeType) { + $mimeType = mime_content_type($path); + } + + if (null === $size) { + $size = (int) filesize($path); + } + return new self(fopen($path, 'r'), $name, $mimeType, $size); }