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

[QA - Sentry] Les fichiers de format ne sont pas pris en charge #3612

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
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
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();
hmeneuvrier marked this conversation as resolved.
Show resolved Hide resolved
$this->logger->error($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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

J'enlèverais bien aussi les logs sur sentry pour des formats non supportés... non ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Je suis ok, mais y'avait peut-être une raison derrière ça ?

$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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

idem

$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
17 changes: 17 additions & 0 deletions src/Service/UploadHandlerService.php
Original file line number Diff line number Diff line change
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
Loading