feat(share) : controllers status/browse/download du partage
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Share;
|
||||
|
||||
use App\Service\Share\Exception\InvalidPathException;
|
||||
use App\Service\Share\Exception\ShareConnectionException;
|
||||
use App\Service\Share\Exception\ShareNotConfiguredException;
|
||||
use App\Service\Share\FileSource;
|
||||
use App\Service\Share\SharePathResolver;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\HeaderUtils;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Mime\MimeTypes;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
use function is_resource;
|
||||
|
||||
class ShareDownloadController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly FileSource $fileSource,
|
||||
private readonly SharePathResolver $pathResolver,
|
||||
) {}
|
||||
|
||||
#[Route('/api/share/download', name: 'share_download', methods: ['GET'], priority: 1)]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function __invoke(Request $request): Response
|
||||
{
|
||||
$rawPath = (string) $request->query->get('path', '');
|
||||
|
||||
try {
|
||||
$path = $this->pathResolver->normalizeRelative($rawPath);
|
||||
} catch (InvalidPathException) {
|
||||
return new Response('Invalid path.', 400);
|
||||
}
|
||||
|
||||
if ('' === $path) {
|
||||
throw new NotFoundHttpException('No file requested.');
|
||||
}
|
||||
|
||||
try {
|
||||
$stream = $this->fileSource->read($path);
|
||||
} catch (ShareNotConfiguredException) {
|
||||
return new Response('Share not configured.', 409);
|
||||
} catch (ShareConnectionException $e) {
|
||||
throw new NotFoundHttpException($e->getMessage());
|
||||
}
|
||||
|
||||
$name = basename($path);
|
||||
$extension = pathinfo($name, PATHINFO_EXTENSION);
|
||||
$mime = MimeTypes::getDefault()->getMimeTypes($extension)[0] ?? 'application/octet-stream';
|
||||
|
||||
// SVG toujours en attachment (anti-XSS) ; sinon respecte le paramètre demandé.
|
||||
$requested = 'attachment' === $request->query->get('disposition') ? 'attachment' : 'inline';
|
||||
$disposition = 'image/svg+xml' === $mime ? HeaderUtils::DISPOSITION_ATTACHMENT : $requested;
|
||||
|
||||
$response = new StreamedResponse(function () use ($stream): void {
|
||||
if (is_resource($stream)) {
|
||||
fpassthru($stream);
|
||||
fclose($stream);
|
||||
}
|
||||
});
|
||||
$response->headers->set('Content-Type', $mime);
|
||||
$response->headers->set('Content-Disposition', HeaderUtils::makeDisposition($disposition, $name));
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user