<?php
namespace Boab\CmsBundle\Controller;
use App\Exception\AppException;
use Boab\CmsBundle\Event\DocumentRemovalEvent;
use Boab\CmsBundle\Filesystem\FilesystemManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class FileManagementController extends BaseController
{
private FilesystemManager $filesystemManager;
public function __construct(FilesystemManager $filesystemManager)
{
$this->filesystemManager = $filesystemManager;
}
public function streamMedia(Request $request, string $fileName)
{
$size = $request->query->get('size', '');
try{
$response = $this->filesystemManager->getStreamResponse($fileName, $size);
if(!$response){
// If the file is not found in any filesystem, throw a NotFoundHttpException
throw new NotFoundHttpException(sprintf('File not found. %s', $request->getUri()));
}
return $response;
}catch(NotFoundHttpException $e){
$this->logger->error($e->getMessage(), ['exception'=>$e]);
throw new AppException(400, $e->getMessage());
}catch(\Exception $e){
$this->logger->error($e->getMessage(), ['exception'=>$e]);
throw new AppException(400, $e->getMessage());
}
}
public function deleteFile(Request $request, string $module, string $uuid)
{
$this->entityManager->beginTransaction();
try{
$event = new DocumentRemovalEvent($request, $module, $uuid, $request->query->all());
$this->eventDispatcher->dispatch($event, DocumentRemovalEvent::REMOVE_FILE);
$this->entityManager->flush();
$this->entityManager->commit();
return new JsonResponse([
'status' => 'success',
'message' => $event->getMessage()
]);
}catch(\Exception $e){
$this->logger->error($e->getMessage(), ['exception'=>$e]);
$this->entityManager->rollback();
throw new AppException('error', 'Something went wrong! Unable to complete the deletion');
}
}
}