Skip to content

Commit 47938c6

Browse files
committed
Delete all uploaded files after request handle
1 parent 7d82a57 commit 47938c6

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/Http/HttpRequestHandler.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
use Psr\Http\Message\StreamFactoryInterface;
1212
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
1313
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
14+
use Symfony\Bridge\PsrHttpMessage\Factory\UploadedFile;
1415
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
1516
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
1619
use Symfony\Component\HttpKernel\KernelInterface;
1720
use Symfony\Component\HttpKernel\TerminableInterface;
1821

@@ -48,7 +51,8 @@ public function __invoke(ServerRequestInterface $request): ResponseInterface
4851

4952
private function handleFile(string $file): ResponseInterface
5053
{
51-
return $this->responseFactory->createResponse()
54+
return $this->responseFactory
55+
->createResponse()
5256
->withBody($this->streamFactory->createStreamFromFile($file))
5357
->withHeader('Content-Type', (new MimeTypeMapper())->lookupMimeTypeFromPath($file))
5458
;
@@ -66,14 +70,24 @@ private function handle(ServerRequestInterface $request): ResponseInterface
6670
/** @var WorkerProcess $worker */
6771
$worker = $this->kernel->getContainer()->get('phpstreamserver.worker');
6872

73+
$worker->getEventLoop()->defer(fn() => $this->terminate($symfonyRequest, $symfonyResponse));
74+
75+
return $this->psrHttpFactory->createResponse($symfonyResponse);
76+
}
77+
78+
private function terminate(Request $symfonyRequest, Response $symfonyResponse): void
79+
{
6980
if ($this->kernel instanceof TerminableInterface) {
70-
$worker->getEventLoop()->defer(function () use ($symfonyRequest, $symfonyResponse): void {
71-
/** @psalm-suppress UndefinedInterfaceMethod */
72-
$this->kernel->terminate($symfonyRequest, $symfonyResponse);
73-
});
81+
$this->kernel->terminate($symfonyRequest, $symfonyResponse);
7482
}
7583

76-
return $this->psrHttpFactory->createResponse($symfonyResponse);
84+
// Delete all uploaded files
85+
$files = $symfonyRequest->files->all();
86+
\array_walk_recursive($files, static function (UploadedFile $file) {
87+
if (\file_exists($file->getRealPath())) {
88+
\unlink($file->getRealPath());
89+
}
90+
});
7791
}
7892

7993
private function findFileInPublicDirectory(string $requestPath): string|null

0 commit comments

Comments
 (0)