diff --git a/README.md b/README.md index cad03d8..f59691a 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ your concerns remain unanswered. * Serve a complete file. * Serve a single byte range of a file. * Serve multiple byte ranges of a file. +* Set a custom mimetype. ## Requirements diff --git a/src/FileStreamer.php b/src/FileStreamer.php index e4a39ad..28f3b8a 100644 --- a/src/FileStreamer.php +++ b/src/FileStreamer.php @@ -59,6 +59,10 @@ class FileStreamer * @var false|resource Handle to the file to serve. */ private $filePointer; + /** + * @var string|null If not null, it defines type mimetype of the file to download. + */ + private ?string $mimeType = null; /** * Class Constructor. @@ -90,6 +94,20 @@ public function setInline(bool $inline = true): void $this->inline = $inline; } + /** + * Set the mimetype of to file to serve. + * + * When not null, it overrides getting the mimetype automatically when the headers are sent. + * + * @param string|null $mimeType the mimetype of the file to serve. + * + * @return void + */ + public function setMimeType(?string $mimeType): void + { + $this->mimeType = $mimeType; + } + /** * Start serving the file. * @@ -231,8 +249,11 @@ private function sendHeaders(int $type): void // Get file mimetype. $filePath = $this->fileInfo->getPathname(); $fileSize = $this->fileInfo->getSize(); - $fileInfo = new finfo(); - $mimeType = @$fileInfo->file($filePath, FILEINFO_MIME_TYPE); + $mimeType = $this->mimeType; + if (null === $mimeType) { + $fileInfo = new finfo(); + $mimeType = @$fileInfo->file($filePath, FILEINFO_MIME_TYPE); + } // Caching headers as IE6 workaround. header('Pragma: public'); diff --git a/tests/FileStreamerTest.php b/tests/FileStreamerTest.php index e54318b..b569420 100644 --- a/tests/FileStreamerTest.php +++ b/tests/FileStreamerTest.php @@ -243,6 +243,7 @@ public function testStartMultiRange(): void /** * Test if the correct data is being served when no range is requested. + * Also, it tests setting a custom Content-Type. * * @return void */ @@ -265,8 +266,10 @@ public function testStartNoRange(): void // Inline disposition Test. Output::reset(); $mock->setInline(true); + $mock->setMimeType('digilive/test'); $expectedHeaders = array_merge(self::defaultHeaders, ['Content-Disposition: inline']); + $expectedHeaders[4] = 'Content-Type: digilive/test'; try { // Suppress warning or method will be aborted.