Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically generate file props from path #360

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be use the mime type guesser from Symfony here instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any hint what that looks like?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could instantiate it yourself via (new MimeTypes())->guessMimeType($path). However, the symfony/framework-bundle also registers it as the mime_types service, which allows the system to add additional guessers via the MimeTypeGuesserInterface via the AddMimeTypeGuesserPass compiler pass. This would have to be done via a helper service outside this static function.

}

if (null === $size) {
$size = (int) filesize($path);
}

return new self(fopen($path, 'r'), $name, $mimeType, $size);
}

Expand Down
Loading