Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Classes/Definition/ContentTypeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
enum ContentTypeDefinition {
case DEFAULT;

case FILE_OCTET_STREAM;

case FILE_PLAIN;

case IMAGE_JPEG;
Expand Down
26 changes: 24 additions & 2 deletions Classes/Middleware/MiddlewareApiAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ public function buildUri(int $pageId, array $additionalGetParams = [], ?string $

/**
* @param ContentTypeDefinition $contentType defaults to JSON content type if omitted
* @param array<string, string> $withHeaders
*/
public function createResponse(string $string, ContentTypeDefinition $contentType = ContentTypeDefinition::DEFAULT, string $fileName = ''): void {
public function createResponse(string $string, ContentTypeDefinition $contentType = ContentTypeDefinition::DEFAULT, string $fileName = '', array $withHeaders = []): void {
$response = $this->responseFactory->createResponse();

switch ($contentType) {
Expand All @@ -181,10 +182,27 @@ public function createResponse(string $string, ContentTypeDefinition $contentTyp

break;

case ContentTypeDefinition::FILE_OCTET_STREAM:
$response = $response
->withHeader('Content-Type', 'application/octet-stream')
->withHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"')
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate')
->withHeader('Pragma', 'public')
->withHeader('Content-Length', (string) strlen($string))
;

break;

case ContentTypeDefinition::FILE_PLAIN:
$response = $response
->withHeader('Content-Type', 'text/plain; charset=utf-8')
->withAddedHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"')
->withHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"')
->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate')
->withHeader('Pragma', 'public')
->withHeader('Content-Length', (string) strlen($string))
;

break;
Expand All @@ -203,6 +221,10 @@ public function createResponse(string $string, ContentTypeDefinition $contentTyp
break;
}

foreach ($withHeaders as $name => $value) {
$response = $response->withHeader($name, $value);
}

$response->getBody()->write($string);

$this->response = $response;
Expand Down
Loading