Skip to content

Commit

Permalink
Make exporters provide a MIME type (#319)
Browse files Browse the repository at this point in the history
This allows us to set a correct `Content-Type` header in the
`BinaryFileResponse`. If we don't do this, the user is required to have
automatic MIME type detection available, such as provided by
`symfony/mime`.

Co-authored-by: Jelle Raaijmakers <[email protected]>
  • Loading branch information
gmta and Jelle Raaijmakers committed Nov 14, 2023
1 parent 01cec9a commit 552ca1d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Exporter/Csv/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function export(array $columnNames, \Iterator $data): \SplFileInfo
return new \SplFileInfo($filePath);
}

public function getMimeType(): string
{
return 'text/csv';
}

public function getName(): string
{
return 'csv';
Expand Down
5 changes: 5 additions & 0 deletions src/Exporter/DataTableExporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ interface DataTableExporterInterface
*/
public function export(array $columnNames, \Iterator $data): \SplFileInfo;

/**
* The MIME type of the exported file.
*/
public function getMimeType(): string;

/**
* A unique name to identify the exporter.
*/
Expand Down
1 change: 1 addition & 0 deletions src/Exporter/DataTableExporterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function getResponse(): Response

$response = new BinaryFileResponse($file);
$response->deleteFileAfterSend(true);
$response->headers->set('Content-Type', $exporter->getMimeType());
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->getFile()->getFilename());

$this->dataTable->getEventDispatcher()->dispatch(new DataTableExporterResponseEvent($response), DataTableExporterEvents::PRE_RESPONSE);
Expand Down
5 changes: 5 additions & 0 deletions src/Exporter/Excel/ExcelExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ private function autoSizeColumnWidth(Worksheet $sheet): void
}
}

public function getMimeType(): string
{
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
}

public function getName(): string
{
return 'excel';
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixtures/AppBundle/DataTable/Exporter/TxtExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function export(array $columnNames, \Iterator $data): \SplFileInfo
return new \SplFileInfo($filename);
}

public function getMimeType(): string
{
return 'text/plain';
}

public function getName(): string
{
return 'txt';
Expand Down

0 comments on commit 552ca1d

Please sign in to comment.