diff --git a/src/Protocols/Http/Response.php b/src/Protocols/Http/Response.php index 8ee51b7e0..76c901bca 100644 --- a/src/Protocols/Http/Response.php +++ b/src/Protocols/Http/Response.php @@ -56,7 +56,7 @@ class Response implements Stringable public ?array $file = null; /** - * Mine type map. + * Mime Type map. * @var array */ protected static array $mimeTypeMap = [ @@ -340,6 +340,17 @@ public function getHeaders(): array return $this->headers; } + /** + * Get Mime Type from extension. + * + * @param string $ext + * @return string + */ + public function getMimeType(string $ext): string + { + return self::$mimeTypeMap[$ext] ?? 'application/octet-stream'; + } + /** * Set status. * @@ -496,15 +507,13 @@ protected function createHeadForFile(array $fileInfo): string if ($baseName === '') { $baseName = 'unknown'; } + $mime = ''; if (!isset($headers['Content-Type'])) { - if (isset(self::$mimeTypeMap[$extension])) { - $head .= "Content-Type: " . self::$mimeTypeMap[$extension] . "\r\n"; - } else { - $head .= "Content-Type: application/octet-stream\r\n"; - } + $mime = $this->getMimeType($extension); + $head .= "Content-Type: " . $mime . "\r\n"; } - if (!isset($headers['Content-Disposition']) && !isset(self::$mimeTypeMap[$extension])) { + if (!isset($headers['Content-Disposition']) && $mime === 'application/octet-stream') { $head .= "Content-Disposition: attachment; filename=\"$baseName\"\r\n"; } diff --git a/tests/Unit/Protocols/Http/ResponseTest.php b/tests/Unit/Protocols/Http/ResponseTest.php index 1bd61fd6a..ec2a12654 100644 --- a/tests/Unit/Protocols/Http/ResponseTest.php +++ b/tests/Unit/Protocols/Http/ResponseTest.php @@ -54,4 +54,4 @@ expect((string)$response) ->toContain('Content-Type: image/jpeg') ->toContain('Last-Modified: '); -}); \ No newline at end of file +});