Skip to content

Commit

Permalink
check size on uploadFile #3336
Browse files Browse the repository at this point in the history
  • Loading branch information
hmeneuvrier committed Jan 24, 2025
1 parent e9a8c93 commit 6c34f91
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 46 deletions.
105 changes: 60 additions & 45 deletions src/Service/Signalement/SignalementFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Entity\Intervention;
use App\Entity\Signalement;
use App\Entity\User;
use App\Exception\File\EmptyFileException;
use App\Exception\File\MaxUploadSizeExceededException;
use App\Factory\FileFactory;
use App\Service\Files\FilenameGenerator;
use App\Service\ImageManipulationHandler;
Expand Down Expand Up @@ -40,6 +42,7 @@ public function process(
): array {
$fileList = [];
foreach ($files[$inputName] as $key => $file) {
$fileSizeOk = false;
if ($file instanceof UploadedFile) {
try {
if (!$this->fileScanner->isClean($file->getPathname())) {
Expand All @@ -53,61 +56,73 @@ public function process(
$this->logger->error($exception->getMessage());
continue;
}
try {
$fileSizeOk = $this->uploadHandlerService->isFileSizeOk($file);
} catch (MaxUploadSizeExceededException|EmptyFileException $exception) {
$this->errors[] = $exception->getMessage();
}
} else {
$fileSizeOk = true;
}
$fileExtension = $file instanceof UploadedFile ? $file->getExtension() : null;
if (
$file instanceof UploadedFile
&& File::INPUT_NAME_DOCUMENTS === $inputName
&& !UploadHandlerService::isAcceptedDocumentFormat($file, $inputName)
) {
$acceptedExtensions = UploadHandlerService::getAcceptedExtensions('document');
$message = <<<ERROR


if ($fileSizeOk) {
$fileExtension = $file instanceof UploadedFile ? $file->getClientOriginalExtension() : null;

if (
$file instanceof UploadedFile
&& File::INPUT_NAME_DOCUMENTS === $inputName
&& !UploadHandlerService::isAcceptedDocumentFormat($file, $inputName)
) {
$acceptedExtensions = UploadHandlerService::getAcceptedExtensions('document');
$message = <<<ERROR
Les fichiers de format {$fileExtension} ne sont pas pris en charge,
merci de choisir un fichier au format {$acceptedExtensions}.
ERROR;
$fileInfo = '( Fichier : '.$file->__toString().' MimeType : '.$file->getMimeType().' )';
$this->logger->error($message.$fileInfo);
$this->errors[] = $message;
} elseif (
$file instanceof UploadedFile
&& File::INPUT_NAME_PHOTOS === $inputName
&& !ImageManipulationHandler::isAcceptedPhotoFormat($file, $inputName)
) {
$acceptedExtensions = UploadHandlerService::getAcceptedExtensions('photo');
$message = <<<ERROR
$fileInfo = ' ( Fichier : '.$file->__toString().' MimeType : '.$file->getMimeType().' )';
$this->logger->error($message.$fileInfo);
$this->errors[] = $message;
} elseif (
$file instanceof UploadedFile
&& File::INPUT_NAME_PHOTOS === $inputName
&& !ImageManipulationHandler::isAcceptedPhotoFormat($file, $inputName)
) {
$acceptedExtensions = UploadHandlerService::getAcceptedExtensions('photo');
$message = <<<ERROR
Les fichiers de format {$fileExtension} ne sont pas pris en charge,
merci de choisir un fichier au format {$acceptedExtensions}.
ERROR;
$fileInfo = '( Fichier : '.$file->__toString().' MimeType : '.$file->getMimeType().' )';
$this->logger->error($message.$fileInfo);
$this->errors[] = $message;
} else {
$inputTypeDetection = $inputName;
try {
if ($file instanceof UploadedFile) {
$filename = $this->uploadHandlerService->uploadFromFile(
$file,
$this->filenameGenerator->generate($file),
$inputTypeDetection
);
$title = $this->filenameGenerator->getTitle();

if (\in_array($file->getMimeType(), File::IMAGE_MIME_TYPES)) {
$this->imageManipulationHandler->setUseTmpDir(false)->resize($filename)->thumbnail($filename);
$fileInfo = ' ( Fichier : '.$file->__toString().' MimeType : '.$file->getMimeType().' )';
$this->logger->error($message.$fileInfo);
$this->errors[] = $message;
} else {
$inputTypeDetection = $inputName;
try {
if ($file instanceof UploadedFile) {
$filename = $this->uploadHandlerService->uploadFromFile(
$file,
$this->filenameGenerator->generate($file),
$inputTypeDetection
);
$title = $this->filenameGenerator->getTitle();

if (\in_array($file->getMimeType(), File::IMAGE_MIME_TYPES)) {
$this->imageManipulationHandler->setUseTmpDir(false)->resize($filename)->thumbnail($filename);
} else {
$inputTypeDetection = 'documents';
}
} else {
$inputTypeDetection = 'documents';
$filename = $this->uploadHandlerService->moveFromBucketTempFolder($file);
$title = $key;
}
} else {
$filename = $this->uploadHandlerService->moveFromBucketTempFolder($file);
$title = $key;
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage());
$this->errors[] = $exception->getMessage();
continue;
}
if (!empty($filename)) {
$fileList[] = $this->createFileItem($filename, $title, $inputTypeDetection, $documentType);
}
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage());
$this->errors[] = $exception->getMessage();
continue;
}
if (!empty($filename)) {
$fileList[] = $this->createFileItem($filename, $title, $inputTypeDetection, $documentType);
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/Service/UploadHandlerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function toTempFolder(
}
$newFilename = $this->filenameGenerator->generate($file);
$titre = $this->filenameGenerator->getTitle();

if ($this->isFileEmpty($file)) {
throw new EmptyFileException();
}
Expand Down Expand Up @@ -119,6 +119,23 @@ public static function getAcceptedExtensions(?string $type = 'document'): string
return $all;
}

/**
* @throws EmptyFileException
* @throws MaxUploadSizeExceededException
*/
public function isFileSizeOk(
UploadedFile $file,
): bool {
if ($this->isFileEmpty($file)) {
throw new EmptyFileException();
}
if ($file->getSize() > self::MAX_FILESIZE) {
throw new MaxUploadSizeExceededException(self::MAX_FILESIZE);
}

return true;
}

public function moveFilePath(string $filePath): ?string
{
try {
Expand Down

0 comments on commit 6c34f91

Please sign in to comment.