Skip to content

Commit db739fe

Browse files
author
Julien Neuhart
committed
Makre sure the tmp XSLS is removed
1 parent 178018e commit db739fe

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/api/src/Infrastructure/Controller/DownloadXLSXController.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@
88
use Ramsey\Uuid\Uuid;
99
use Symfony\Component\HttpFoundation\Response;
1010

11+
use function file_exists;
1112
use function Safe\file_get_contents;
1213
use function Safe\unlink;
1314

1415
abstract class DownloadXLSXController extends DownloadController
1516
{
1617
protected function createResponseWithXLSXAttachment(string $filename, Xlsx $xlsx): Response
1718
{
18-
$tmpFilename = Uuid::uuid4()->toString() . '.xlsx';
19-
$xlsx->save($tmpFilename);
20-
$fileContent = file_get_contents($tmpFilename);
21-
unlink($tmpFilename);
19+
try {
20+
$tmpFilename = Uuid::uuid4()->toString() . '.xlsx';
21+
$xlsx->save($tmpFilename);
22+
$fileContent = file_get_contents($tmpFilename); // Get the file content.
23+
} finally {
24+
if (file_exists($tmpFilename)) {
25+
unlink($tmpFilename); // Delete the file.
26+
}
27+
}
2228

2329
return $this->createResponseWithAttachment(
2430
$filename,

0 commit comments

Comments
 (0)