Skip to content

Commit 2f4508d

Browse files
committed
Allow set custom Content-Type
1 parent 9912fbd commit 2f4508d

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

src/ResponseDownload.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ trait ResponseDownload
5252
* @param int $delay Delay between flushs in microseconds
5353
* @param int $readLength Bytes read by flush
5454
* @param string|null $filename A custom filename
55+
* @param string|null $contentType A custom Content-Type
5556
*
5657
* @throws InvalidArgumentException If invalid file path
5758
* @throws RuntimeException If can not get the file size or modification time
@@ -64,7 +65,8 @@ public function setDownload(
6465
bool $acceptRanges = true,
6566
int $delay = 0,
6667
int $readLength = 1024,
67-
?string $filename = null
68+
?string $filename = null,
69+
?string $contentType = null
6870
) : static {
6971
$realpath = \realpath($filepath);
7072
if ($realpath === false || !\is_file($realpath)) {
@@ -106,10 +108,10 @@ public function setDownload(
106108
}
107109
}
108110
$this->setHeader(ResponseHeader::CONTENT_LENGTH, (string) $this->filesize);
109-
$this->setHeader(
110-
ResponseHeader::CONTENT_TYPE,
111-
\mime_content_type($this->filepath) ?: 'application/octet-stream'
112-
);
111+
if ($contentType === null) {
112+
$contentType = \mime_content_type($this->filepath) ?: 'application/octet-stream';
113+
}
114+
$this->setHeader(ResponseHeader::CONTENT_TYPE, $contentType);
113115
$this->sendType = 'normal';
114116
return $this;
115117
}

tests/ResponseDownloadTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,13 @@ public function testReadFileWithDelay() : void
219219
self::assertLessThan(4, $endTime - $startTime);
220220
self::assertTrue($this->response->isSent());
221221
}
222+
223+
public function testContentType() : void
224+
{
225+
$filepath = __DIR__ . '/files/logo.png';
226+
$this->response->setDownload($filepath, acceptRanges: false);
227+
self::assertSame('image/png', $this->response->getHeader('Content-Type'));
228+
$this->response->setDownload($filepath, acceptRanges: false, contentType: 'foo/bar');
229+
self::assertSame('foo/bar', $this->response->getHeader('Content-Type'));
230+
}
222231
}

0 commit comments

Comments
 (0)