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
22 changes: 19 additions & 3 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Builder implements Responsable

protected int $bytesSent = 0;

protected bool $sendHeaders = true;

protected Collection $meta;

protected Queue $queue;
Expand Down Expand Up @@ -137,6 +139,8 @@ public function saveTo($output): int
default => throw new InvalidArgumentException('Invalid output provided'),
};

$this->sendHeaders = false;

return $this->process();
}

Expand All @@ -151,8 +155,8 @@ public function process(): int

if ($this->canPredictZipSize()) {
$size = $zip->finish();
header('Content-Length: '.$size);
header('X-Accel-Buffering: no');
$this->header('Content-Length: '.$size);
$this->header('X-Accel-Buffering: no');

event(new ZipStreaming($this, $zip, $size));

Expand Down Expand Up @@ -214,6 +218,8 @@ public function getFinalSize(): int

public function response(): StreamedResponse
{
$this->sendHeaders = true;

return new StreamedResponse(function () {
$this->process();
}, 200);
Expand All @@ -230,8 +236,9 @@ protected function prepare(): ZipStream
operationMode: $this->canPredictZipSize() ? OperationMode::SIMULATE_STRICT : OperationMode::NORMAL,
comment: $this->getComment(),
outputStream: $this->getOutputStream(),
httpHeaderCallback: $this->header(...),
outputName: $this->getOutputName(),
flushOutput: true,
flushOutput: true
);

$this->queue->each->prepare($zip);
Expand All @@ -251,4 +258,13 @@ protected function getOutputStream(): StreamInterface

return $this->outputStream;
}

protected function header(string $header): static
{
if($this->sendHeaders) {
header($header);
}

return $this;
}
}
2 changes: 0 additions & 2 deletions tests/ConflictTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace STS\ZipStream\Tests;

use GuzzleHttp\Psr7\BufferStream;
use Illuminate\Support\Str;
use STS\ZipStream\Builder;
Expand Down
2 changes: 0 additions & 2 deletions tests/FileTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace STS\ZipStream\Tests;

use Orchestra\Testbench\TestCase;
use STS\ZipStream\Models\File;
use STS\ZipStream\Models\HttpFile;
Expand Down
65 changes: 65 additions & 0 deletions tests/StreamingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use Orchestra\Testbench\TestCase;
use STS\ZipStream\Facades\Zip;
use STS\ZipStream\ZipStreamServiceProvider;
use Symfony\Component\HttpFoundation\StreamedResponse;

class StreamingTest extends TestCase
{
protected function getPackageProviders($app)
{
return [ZipStreamServiceProvider::class];
}

protected function getPackageAliases($app)
{
return [
'Zip' => Zip::class
];
}

protected function defineRoutes($router)
{
$router->get('/stream', function () {
$testrun = microtime();
file_put_contents("/tmp/test1.txt", "this is the first test file for test run $testrun");

return Zip::create("test.zip")
->add("/tmp/test1.txt")
->then(fn() => unlink("/tmp/test1.txt"));
});

$router->get('/save', function () {
$testrun = microtime();
file_put_contents("/tmp/test1.txt", "this is the first test file for test run $testrun");

$dir = "/tmp/" . Str::random();

Zip::create("test.zip")
->add("/tmp/test1.txt")
->saveTo($dir);

return "$dir/test.zip";
});
}

public function testZipStream()
{
$response = $this->get('/stream');

// All we really care about is that the response is a StreamedResponse
$this->assertInstanceOf(StreamedResponse::class, $response->baseResponse);
}

public function testZipSaveToDiskOnly()
{
$response = $this->get('/save');

// Make sure we did NOT get a stream response this time, and that the zip exists
$this->assertNotInstanceOf(StreamedResponse::class, $response->baseResponse);
$this->assertTrue(file_exists($response->getContent()));

unlink($response->getContent());
}
}
2 changes: 0 additions & 2 deletions tests/ZipTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace STS\ZipStream\Tests;

use GuzzleHttp\Psr7\BufferStream;
use Illuminate\Support\Str;
use STS\ZipStream\Builder;
Expand Down