diff --git a/Controller/UploadController.php b/Controller/UploadController.php index 7aeaebcbd45e46492a414e292d6637645ce9e584..dd81da810b2321b6976ee717d126ea80be066dea 100644 --- a/Controller/UploadController.php +++ b/Controller/UploadController.php @@ -16,8 +16,11 @@ use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\ResponseHeaderBag; +use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\HttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Routing\RouterInterface; /** @@ -146,6 +149,41 @@ class UploadController extends Controller ); } + /** + * @Route("/{id}", name="file_upload_get") + * @Method("GET") + * @param Request $request + * @param UploadedFile $file + */ + public function getAction(Request $request, UploadedFile $file) + { + $this->validateToken($request); + + if(!$file->isValid()) { + throw new NotFoundHttpException(); + } + + $response = StreamedResponse::create(function() use($file) { echo $file->getContent(); }) + ->setPrivate() + ->setLastModified($file->getLastModified()) + ->setEtag($file->getChecksum()); + + $response->headers->add( + [ + 'Content-Type' => $file->getMimeType(), + 'Content-Length' => $file->getSize(), + 'Content-Disposition' => $response->headers->makeDisposition( + ResponseHeaderBag::DISPOSITION_ATTACHMENT, + $file->getOriginalFilename() + ), + ] + ); + + $response->isNotModified($request); + + return $response; + } + /** * @Route("/{id}", name="file_upload_delete") * @Method("DELETE") @@ -157,6 +195,7 @@ class UploadController extends Controller $this->validateToken($request); $this->fileManager->delete($file); + return $this->createResponse(); }