lib/boab/cms-bundle/src/Controller/FileManagementController.php line 38

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\Controller;
  3. use App\Exception\AppException;
  4. use Boab\CmsBundle\Event\DocumentRemovalEvent;
  5. use Boab\CmsBundle\Filesystem\FilesystemManager;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. class FileManagementController extends BaseController
  11. {
  12.     private FilesystemManager $filesystemManager;
  13.     public function __construct(FilesystemManager $filesystemManager)
  14.     {
  15.         $this->filesystemManager $filesystemManager;
  16.     }
  17.     public function streamMedia(Request $requeststring $fileName)
  18.     {
  19.         $size $request->query->get('size''');
  20.         try{
  21.             $response $this->filesystemManager->getStreamResponse($fileName$size);
  22.             if(!$response){
  23.                 // If the file is not found in any filesystem, throw a NotFoundHttpException
  24.                 throw new NotFoundHttpException(sprintf('File not found. %s'$request->getUri()));
  25.             }  
  26.             return $response;          
  27.         }catch(NotFoundHttpException $e){
  28.             $this->logger->error($e->getMessage(), ['exception'=>$e]);
  29.             throw new AppException(400$e->getMessage());
  30.         }catch(\Exception $e){
  31.             $this->logger->error($e->getMessage(), ['exception'=>$e]);
  32.             throw new AppException(400$e->getMessage());
  33.         }
  34.     }
  35.     public function deleteFile(Request $requeststring $modulestring $uuid)
  36.     {
  37.         $this->entityManager->beginTransaction();
  38.         try{
  39.             $event = new DocumentRemovalEvent($request$module$uuid$request->query->all());
  40.             $this->eventDispatcher->dispatch($eventDocumentRemovalEvent::REMOVE_FILE);
  41.             $this->entityManager->flush();
  42.             $this->entityManager->commit();
  43.             return new JsonResponse([
  44.                 'status' => 'success',
  45.                 'message' => $event->getMessage()
  46.             ]);
  47.         }catch(\Exception $e){
  48.             $this->logger->error($e->getMessage(), ['exception'=>$e]);
  49.             $this->entityManager->rollback();
  50.             throw new AppException('error''Something went wrong! Unable to complete the deletion');
  51.         }
  52.     }
  53. }