Skip to content

Commit

Permalink
Harden upload accept
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Dec 4, 2024
1 parent 52a38e2 commit 90aa1a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/Controller/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ public function upload(Request $request, BookFileSystemManager $fileSystemManage
'label' => 'Book',
'required' => false,
'multiple' => true,
'attr' => [
'accept' => '.epub,.pdf,.mobi,.cbr,.cbz',
],
])
->add('submit', SubmitType::class, [
'label' => 'Upload',
Expand Down
10 changes: 8 additions & 2 deletions src/Service/BookFileSystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function __construct(
private readonly string $bookFolderNamingFormat,
private readonly string $bookFileNamingFormat,
private readonly SluggerInterface $slugger,
private readonly LoggerInterface $logger)
{
private readonly LoggerInterface $logger
) {
if ($this->bookFolderNamingFormat === '') {
throw new \RuntimeException('Could not get filename format');
}
Expand Down Expand Up @@ -797,6 +797,12 @@ public function uploadFilesToConsumeDirectory(array $files): void
{
$destination = $this->getBooksDirectory().'/consume';
foreach ($files as $file) {
$originalName = $file->getClientOriginalName();
$explode = explode('.', $originalName);
$ext = end($explode);
if (!in_array("*.".$ext, self::ALLOWED_FILE_EXTENSIONS, true)) {
throw new \InvalidArgumentException('File type not allowed');
}
$file->move($destination, $file->getClientOriginalName());
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/components/UploadBookPicture.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% if isEditing and is_granted('EDIT', book) %}
<div class="btn-group mt-2">

<input type="file" name="cover" class="form-control"/>
<input type="file" name="cover" accept="image/png, image/jpeg, image/webp" class="form-control"/>
<button data-action="live#action" class="btn btn-sm btn-outline-primary" data-live-action-param="files|uploadFiles">
{{ 'upload'|trans }}
</button>
Expand Down

0 comments on commit 90aa1a4

Please sign in to comment.