From df57a6f303472e1b83ddcb74b3cef120efe00fa6 Mon Sep 17 00:00:00 2001 From: thisfro Date: Wed, 14 Jul 2021 13:47:42 +0200 Subject: [PATCH] Refactor uploded file handling --- composer.json | 1 + composer.lock | 16 ++++++------ src/Controller/OfferController.php | 34 +++++++----------------- src/Service/OfferPhotoHelper.php | 42 ++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 33 deletions(-) create mode 100644 src/Service/OfferPhotoHelper.php diff --git a/composer.json b/composer.json index 53a1d84..addd661 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "symfony/asset": "5.2.*", "symfony/console": "5.2.*", "symfony/dotenv": "5.2.*", + "symfony/filesystem": "5.2.*", "symfony/flex": "^1.3.1", "symfony/form": "5.2.*", "symfony/framework-bundle": "5.2.*", diff --git a/composer.lock b/composer.lock index a06744c..837452f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "87576858dc205e57e5b2a58d94317391", + "content-hash": "d1148d1eedb4a09cd4b48bb22d51d878", "packages": [ { "name": "composer/package-versions-deprecated", @@ -3369,16 +3369,16 @@ }, { "name": "symfony/filesystem", - "version": "v5.2.10", + "version": "v5.2.11", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "9aa15870b021a34de200a15cff38844db4a930fa" + "reference": "8318cbf1066f44aea550230d8e19ed4ddec3997b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/9aa15870b021a34de200a15cff38844db4a930fa", - "reference": "9aa15870b021a34de200a15cff38844db4a930fa", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/8318cbf1066f44aea550230d8e19ed4ddec3997b", + "reference": "8318cbf1066f44aea550230d8e19ed4ddec3997b", "shasum": "" }, "require": { @@ -3411,7 +3411,7 @@ "description": "Provides basic utilities for the filesystem", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/filesystem/tree/v5.2.10" + "source": "https://github.com/symfony/filesystem/tree/v5.2.11" }, "funding": [ { @@ -3427,7 +3427,7 @@ "type": "tidelift" } ], - "time": "2021-05-26T17:33:56+00:00" + "time": "2021-06-30T07:26:44+00:00" }, { "name": "symfony/finder", @@ -7995,5 +7995,5 @@ "ext-iconv": "*" }, "platform-dev": [], - "plugin-api-version": "2.0.0" + "plugin-api-version": "2.1.0" } diff --git a/src/Controller/OfferController.php b/src/Controller/OfferController.php index deb8641..345a13a 100644 --- a/src/Controller/OfferController.php +++ b/src/Controller/OfferController.php @@ -10,6 +10,7 @@ use App\Repository\WishRepository; use App\Service\PlzToCoordinate; use App\Service\DistanceCalculator; +use App\Service\OfferPhotoHelper; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -37,7 +38,7 @@ class OfferController extends AbstractController } #[Route('/new', name: 'new_offer')] - public function newOffer(Request $request, string $photoDir): Response + public function newOffer(Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response { $offer = new Offering(); $form = $this->createForm(OfferingFormType::class, $offer); @@ -50,15 +51,7 @@ class OfferController extends AbstractController $offer->setCreatedAt(new \DateTime()); if ($photo = $form['photo']->getData()) { - $filename = uniqid().'.'.$photo->guessExtension(); - try { - $photo->move($photoDir, $filename); - } catch (FileException $e) { - // unable to upload the photo, give up - $this->addFlash("error", "There was an error uploading the photo: ".$e); - return $this->redirectToRoute('new_offer'); - } - $offer->setPhotoFilename($filename); + $offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer); } $this->entityManager->persist($offer); @@ -100,7 +93,7 @@ class OfferController extends AbstractController } #[Route('/offer/edit/{id}', name: 'edit_offer')] - public function editOffer(Offering $offer, OfferingRepository $offerRepository, Request $request, string $photoDir): Response + public function editOffer(Offering $offer, OfferingRepository $offerRepository, Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response { $form = $this->createForm(OfferingFormType::class, $offer); $user = $this->getUser(); @@ -113,15 +106,9 @@ class OfferController extends AbstractController $offer->setCreatedAt(new \DateTime()); if ($photo = $form['photo']->getData()) { - $filename = uniqid(random_bytes(6)).'.'.$photo->guessExtension(); - try { - $photo->move($photoDir, $filename); - } catch (FileException $e) { - // unable to upload the photo, give up - $this->addFlash("error", "There was an error uploading the photo: ".$e); - return $this->redirectToRoute('new_offer'); - } - $offer->setPhotoFilename($filename); + $oldFilename = $offer->getPhotoFilename(); + $offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer); + $offerPhotoHelper->deleteOfferPhoto($photoDir, $oldFilename); } $this->entityManager->persist($offer); @@ -139,16 +126,13 @@ class OfferController extends AbstractController } #[Route('/offer/delete/{id}', name: 'delete_offer')] - public function deleteOffer(Offering $offer, string $photoDir): Response + public function deleteOffer(Offering $offer, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response { $user = $this->getUser(); if ($offer->getByUser() === $user) { - if ($offer->getPhotoFilename()) - { - unlink($photoDir . '/' . $offer->getPhotoFilename()); - } + $offerPhotoHelper->deleteOfferPhoto($photoDir, $offer->getPhotoFilename()); $this->entityManager->remove($offer); $this->entityManager->flush(); $this->addFlash( diff --git a/src/Service/OfferPhotoHelper.php b/src/Service/OfferPhotoHelper.php new file mode 100644 index 0000000..9646288 --- /dev/null +++ b/src/Service/OfferPhotoHelper.php @@ -0,0 +1,42 @@ +filesystem = new Filesystem(); + } + + public function uploadOfferPhoto(string $photoDir, UploadedFile $photo, Offering $offer) + { + $filename = uniqid().'.'.$photo->guessExtension(); + try { + $photo->move($photoDir, $filename); + } catch (FileException $e) { + // unable to upload the photo, give up + $this->addFlash("error", "There was an error uploading the photo: ".$e); + return $this->redirectToRoute('new_offer'); + } + $offer->setPhotoFilename($filename); + } + + public function deleteOfferPhoto(string $photoDir, string $filename) + { + $file = $photoDir . '/' . $filename; + if($this->filesystem->exists($file)) { + $this->filesystem->remove($file); + } + + } +} \ No newline at end of file