Skip to content

Commit 81dbddc

Browse files
fix Downloader
1 parent 29a772b commit 81dbddc

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

src/Utils/Downloader/Downloader.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
class Downloader
1212
{
13+
protected int $chunkSize = 8192;
14+
1315
protected function __construct(
1416
protected ?string $filename = null,
1517
protected ?int $size = null,
@@ -70,6 +72,12 @@ public function mimeType(?string $mimeType = null): static
7072
return $this;
7173
}
7274

75+
public function chunkSize(int $bytes): static
76+
{
77+
$this->chunkSize = max(1024, $bytes);
78+
return $this;
79+
}
80+
7381
/**
7482
* Send headers for the download.
7583
*/
@@ -80,7 +88,12 @@ protected function sendHeaders(): void
8088
}
8189
header("Content-Type: {$this->mimeType}");
8290
header('Content-Description: file transfer');
83-
header("Content-Disposition: attachment;filename={$this->filename}");
91+
92+
$filename = $this->filename;
93+
$encoded = rawurlencode($filename);
94+
95+
header("Content-Disposition: attachment; filename=\"{$encoded}\"; filename*=UTF-8''{$encoded}");
96+
8497

8598
if ($this->size) {
8699
header("Content-Length: {$this->size}");
@@ -90,6 +103,26 @@ protected function sendHeaders(): void
90103
header('Expires: -1');
91104
header('Cache-Control: no-cache');
92105
header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
106+
107+
if (function_exists('apache_setenv')) {
108+
apache_setenv('no-gzip', '1');
109+
}
110+
ini_set('zlib.output_compression', 'Off');
111+
}
112+
113+
protected function flushOutput(): void
114+
{
115+
if (ob_get_level() > 0) {
116+
@ob_flush();
117+
}
118+
flush();
119+
}
120+
121+
protected function clearOutputBuffers(): void
122+
{
123+
while (ob_get_level() > 0) {
124+
ob_end_clean();
125+
}
93126
}
94127

95128
/**

src/Utils/Downloader/DownloaderDirect.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public function name(string $name): static
3131
*/
3232
public function autoMimeType(): self
3333
{
34-
$this->mimeType = mime_content_type($this->path);
34+
$mime = mime_content_type($this->path);
35+
if ($mime !== false) {
36+
$this->mimeType = $mime;
37+
}
3538

3639
return $this;
3740
}
@@ -58,13 +61,22 @@ public function get(): void
5861
}
5962
ini_set('max_execution_time', $this->maxExecutionTime);
6063

64+
$this->clearOutputBuffers();
6165
$this->sendHeaders();
6266
$file = fopen($this->path, 'rb');
67+
if ($file === false) {
68+
throw new \RuntimeException("Unable to open file: {$this->path}");
69+
}
70+
71+
while (!feof($file)) {
72+
$chunk = fread($file, $this->chunkSize);
73+
if ($chunk === false) {
74+
break;
75+
}
76+
77+
echo $chunk;
78+
$this->flushOutput();
6379

64-
while (! feof($file)) {
65-
echo fread($file, 1024 * 8);
66-
ob_flush();
67-
flush();
6880
if ($this->speed) {
6981
usleep($this->speed);
7082
}

src/Utils/Downloader/DownloaderZipStream.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function get(): void
3939
throw new \Exception('`maennchen/zipstream-php` is not installed. Install it from GitHub https://github.com/maennchen/ZipStream-PHP');
4040
}
4141

42+
$this->clearOutputBuffers();
4243
$this->sendHeaders();
4344
$zip = new \ZipStream\ZipStream(
4445
outputName: str_replace(' ', '.', $this->filename),

0 commit comments

Comments
 (0)