Implement image previewing
I have tested this with png and jpeg/jpg which both work but most other image types should work as well.  Co-authored-by: Jan Klattenhoff <j.klattenhoff@neusta.de> Reviewed-on: sites/file-explorer#12 Co-authored-by: jank1619 <jan@kjan.email> Co-committed-by: jank1619 <jan@kjan.email>
This commit is contained in:
28
src/Controller/ServeFileController.php
Normal file
28
src/Controller/ServeFileController.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Service\FileSystemService;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class ServeFileController extends AbstractController
|
||||
{
|
||||
public function __construct(private FileSystemService $fileSystemService)
|
||||
{
|
||||
}
|
||||
|
||||
#[Route("/serve/{filePath}", name: "serve_file")]
|
||||
public function __invoke(string $filePath): BinaryFileResponse
|
||||
{
|
||||
$file = $this->fileSystemService->getFile($filePath);
|
||||
$path = $file->getPath() . '/' . $file->getName();
|
||||
|
||||
$response = new BinaryFileResponse($path);
|
||||
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $file->getName());
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user