1111use Psr \Http \Message \StreamFactoryInterface ;
1212use Symfony \Bridge \PsrHttpMessage \Factory \HttpFoundationFactory ;
1313use Symfony \Bridge \PsrHttpMessage \Factory \PsrHttpFactory ;
14+ use Symfony \Bridge \PsrHttpMessage \Factory \UploadedFile ;
1415use Symfony \Bridge \PsrHttpMessage \HttpFoundationFactoryInterface ;
1516use Symfony \Bridge \PsrHttpMessage \HttpMessageFactoryInterface ;
17+ use Symfony \Component \HttpFoundation \Request ;
18+ use Symfony \Component \HttpFoundation \Response ;
1619use Symfony \Component \HttpKernel \KernelInterface ;
1720use 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