Replace ZipStream with native ZipArchive for FrankenPHP compatibility

ZipStream writes via fwrite(php://output) which FrankenPHP silently
drops, resulting in 0-byte ZIP downloads. Switch to ZipArchive to build
the ZIP as a temp file on disk, then serve with response()->download().

- Rewrite download() to use ZipArchive + BinaryFileResponse
- Remove decryptFileToCallback() from FileEncryptionService
- Update tests for BinaryFileResponse instead of StreamedResponse
- Remove maennchen/zipstream-php dependency

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
surtic86
2026-02-25 20:34:05 +01:00
co-authored by Claude Opus 4.6
parent e37b322dde
commit 729e01462b
5 changed files with 127 additions and 133 deletions
+41 -31
View File
@@ -7,8 +7,9 @@ use App\Models\ShareFile;
use App\Services\FileEncryptionService;
use App\Services\ShareService;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use ZipStream\ZipStream;
use ZipArchive;
class DownloadController extends Controller
{
@@ -20,40 +21,39 @@ class DownloadController extends Controller
/**
* Download all files as a ZIP archive.
*/
public function download(Share $share): StreamedResponse
public function download(Share $share): BinaryFileResponse
{
abort_if($share->isExpired() || $share->hasReachedDownloadLimit(), 404);
$share->load('files');
$key = $this->resolveDecryptionKey($share);
$this->shareService->recordDownload($share);
$tempPath = tempnam(sys_get_temp_dir(), 'sealshare_');
return new StreamedResponse(function () use ($share, $key): void {
$zip = new ZipStream(
outputName: 'share-'.$share->token.'.zip',
sendHttpHeaders: false,
);
$zip = new ZipArchive;
$zip->open($tempPath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
foreach ($share->files as $file) {
$encryptedPath = Storage::disk('shares')->path($share->token.'/'.basename($file->stored_path));
$callback = $this->encryptionService->decryptFileToCallback($encryptedPath, $key);
foreach ($share->files as $file) {
$encryptedPath = Storage::disk('shares')->path($share->token.'/'.basename($file->stored_path));
$content = $this->encryptionService->decryptFile($encryptedPath, $key);
$filename = $file->relative_path ?: $file->original_name;
$filename = str_replace('\\', '/', $filename);
$filename = $file->relative_path ?: $file->original_name;
$filename = str_replace('\\', '/', $filename);
if (str_starts_with($filename, '/') || str_contains($filename, '..')) {
$filename = basename($filename);
}
$zip->addFileFromCallback(fileName: $filename, callback: $callback, exactSize: $file->file_size);
if (str_starts_with($filename, '/') || str_contains($filename, '..')) {
$filename = basename($filename);
}
$zip->finish();
}, 200, [
$zip->addFromString($filename, $content);
}
$zip->close();
$this->shareService->recordDownload($share);
return response()->download($tempPath, 'share-'.$share->token.'.zip', [
'Content-Type' => 'application/zip',
'Content-Disposition' => 'attachment; filename="share-'.$share->token.'.zip"',
]);
])->deleteFileAfterSend(true);
}
/**
@@ -66,17 +66,27 @@ class DownloadController extends Controller
$key = $this->resolveDecryptionKey($share);
$this->shareService->recordDownload($share);
$encryptedPath = Storage::disk('shares')->path($share->token.'/'.basename($shareFile->stored_path));
$mimeType = $shareFile->mime_type ?? 'application/octet-stream';
return $this->encryptionService->decryptFileStream(
$encryptedPath,
$key,
$shareFile->original_name,
$shareFile->mime_type ?? 'application/octet-stream',
$shareFile->file_size,
);
$headers = [
'Content-Type' => $mimeType,
'Content-Disposition' => \Symfony\Component\HttpFoundation\HeaderUtils::makeDisposition(
'attachment',
$shareFile->original_name,
'download',
),
];
if ($shareFile->file_size !== null) {
$headers['Content-Length'] = $shareFile->file_size;
}
return new StreamedResponse(function () use ($encryptedPath, $key, $share): void {
$this->encryptionService->streamDecryptedFile($encryptedPath, $key);
$this->shareService->recordDownload($share);
}, 200, $headers);
}
/**
+10 -20
View File
@@ -2,7 +2,6 @@
namespace App\Services;
use Closure;
use Generator;
use RuntimeException;
use Symfony\Component\HttpFoundation\HeaderUtils;
@@ -181,30 +180,21 @@ class FileEncryptionService
}
/**
* Return a closure that decrypts a file into a temporary stream resource.
* Suitable for ZipStream's addFileFromCallback.
* Stream decrypted file content directly to output (echo).
* Use this when you need to add post-streaming logic inside a StreamedResponse callback.
*/
public function decryptFileToCallback(string $encryptedPath, string $key): Closure
public function streamDecryptedFile(string $encryptedPath, string $key): void
{
return function () use ($encryptedPath, $key) {
$tmp = tmpfile();
if ($tmp === false) {
throw new RuntimeException('Cannot create temporary file');
if ($this->isChunkedFormat($encryptedPath)) {
foreach ($this->decryptChunks($encryptedPath, $key) as $chunk) {
echo $chunk;
flush();
}
if ($this->isChunkedFormat($encryptedPath)) {
foreach ($this->decryptChunks($encryptedPath, $key) as $chunk) {
fwrite($tmp, $chunk);
}
} else {
fwrite($tmp, $this->decryptLegacy($encryptedPath, $key));
}
return;
}
rewind($tmp);
return $tmp;
};
echo $this->decryptLegacy($encryptedPath, $key);
}
/**