Refactor uploded file handling
This commit is contained in:
parent
7f485cee91
commit
df57a6f303
4 changed files with 60 additions and 33 deletions
|
@ -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.*",
|
||||
|
|
16
composer.lock
generated
16
composer.lock
generated
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
42
src/Service/OfferPhotoHelper.php
Normal file
42
src/Service/OfferPhotoHelper.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Offering;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
class OfferPhotoHelper
|
||||
{
|
||||
private $logger;
|
||||
|
||||
public function __construct(LoggerInterface $logger)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in a new issue