Skip to content

Commit

Permalink
Automatically generate file props from path
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Sep 9, 2024
1 parent 00795e0 commit 9ec43e8
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/BulkyItem/FileItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 9ec43e8

Please sign in to comment.