Refactor uploded file handling

This commit is contained in:
Jannis Portmann 2021-07-14 13:47:42 +02:00
parent 7f485cee91
commit df57a6f303
4 changed files with 60 additions and 33 deletions

View file

@ -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(