Skip to content

Commit

Permalink
avoid posting wrong file formats #783
Browse files Browse the repository at this point in the history
  • Loading branch information
emilschn committed Sep 20, 2024
1 parent 865a371 commit c0e721e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Service/Upload/UploadHandlerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
class UploadHandlerService
{
public const MAX_FILESIZE = 10 * 1024 * 1024;
public const UPLOAD_ACCEPTED_EXTENSIONS = ['jpg', 'jpeg', 'png'];
public const UPLOAD_ACCEPTED_MIME_TYPES = ['image/jpeg', 'image/png'];

private $file;

Expand Down Expand Up @@ -100,6 +102,16 @@ public function getFile(): ?array
return $this->file;
}

private function isAcceptedPhotoFormat(UploadedFile $file): bool
{
return \in_array($file->getMimeType(), self::UPLOAD_ACCEPTED_MIME_TYPES)
&& (
\in_array($file->getClientOriginalExtension(), self::UPLOAD_ACCEPTED_EXTENSIONS)
|| \in_array($file->getExtension(), self::UPLOAD_ACCEPTED_EXTENSIONS)
|| \in_array($file->guessExtension(), self::UPLOAD_ACCEPTED_EXTENSIONS)
);
}

public function handleUploadFilesRequest(
?array $filesPosted,
): array {
Expand All @@ -110,6 +122,12 @@ public function handleUploadFilesRequest(
if ($file->getError()) {
return [];
}

if (!$this->isAcceptedPhotoFormat($file)) {
$this->logger->error('Bad format : '.$file->getClientOriginalName());
continue;
}

$originalFilename = pathinfo($file->getClientOriginalName(), \PATHINFO_FILENAME);
$title = $originalFilename.'.'.$file->guessExtension();
$safeFilename = $this->slugger->slug($originalFilename);
Expand Down

0 comments on commit c0e721e

Please sign in to comment.