better form & mime type check

This commit is contained in:
Olivier PARPAILLON
2024-11-21 13:47:47 +01:00
parent 3d4fe031f6
commit 2a594a8d44
12 changed files with 85 additions and 55 deletions

View File

@@ -9,6 +9,7 @@ use App\Form\RegistrationFormType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Routing\Attribute\Route;
use App\Repository\ParticipantRepository;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
@@ -51,6 +52,26 @@ class ProfileController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$imageFile = $form->get('image')->getData();
if ($imageFile) {
if ($imageFile->getSize() > 1024 * 1024) { // 1MB
$this->addFlash('error', 'Votre image est trop lourde');
return $this->render('profile/edit.html.twig', [
'profile' => $userConnect,
'formProfile' => $form,
]);
}
$mimeTypes = new MimeTypes();
$validMimeTypes = ['image/png', 'image/jpeg'];
$fileMimeType = $mimeTypes->guessMimeType($imageFile->getRealPath());
if (!in_array($fileMimeType, $validMimeTypes, true)) {
$this->addFlash('error', "Veuillez insérer un type d'image valide (.jpg ou .png)");
return $this->render('profile/edit.html.twig', [
'profile' => $userConnect,
'formProfile' => $form,
]);
}
}
if (($form->has('deleteImage') && $form['deleteImage']->getData()) || $imageFile) {
$this->fileUploader->delete($profile->getFileName(), '/upload/image/profile');
if ($imageFile) {