A 29 GB share with 254 files failed as one ZIP: the single request ran into the 300-second request limit and, behind a proxy we cannot configure, could be cut at any time with no way to resume. Single-file downloads now answer HTTP Range requests (206, ETag and If-Range, 416 past the end): a SEALCHK2 file is decrypted from the chunk holding the first byte asked for. On that, the download page's new "Download all to a folder" (File System Access API, Chrome and Edge on a computer) saves every file into a folder the recipient picks, one request per file, in its folder path; a broken connection is retried from the byte on disk, complete files are skipped, and failed files get a Retry button. Other browsers keep the ZIP and the per-file buttons. The ZIP is sized up front with ZipStream's strict simulation and sent with Content-Length, so the browser shows progress and notices a cut. Both download routes lift the time limit for their streams. The recipient's download window now restarts with each download, so a long file-by-file download never outlasts it. The download page shows the file count and total size and marks the files this browser has downloaded, and the upload page gains "Browse Folder", which keeps each file's path as a dropped folder does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
197 lines
7.1 KiB
PHP
197 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Share;
|
|
use App\Models\ShareFile;
|
|
use App\Services\FileEncryptionService;
|
|
use App\Services\ShareService;
|
|
use GuzzleHttp\Psr7\PumpStream;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Symfony\Component\HttpFoundation\HeaderUtils;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
use ZipStream\CompressionMethod;
|
|
use ZipStream\OperationMode;
|
|
use ZipStream\ZipStream;
|
|
|
|
class DownloadController extends Controller
|
|
{
|
|
public function __construct(
|
|
private FileEncryptionService $encryptionService,
|
|
private ShareService $shareService,
|
|
) {}
|
|
|
|
/**
|
|
* Download all files as a ZIP archive, streamed file by file as it is decrypted: stored without
|
|
* compression, with ZIP64 for files over 4 GB, and never held in memory or written to disk. The
|
|
* archive's size is worked out from the files' sizes first, so the browser shows its progress
|
|
* and notices a download cut short. The private text is left out: it is only ever shown on the
|
|
* page.
|
|
*/
|
|
public function download(Request $request, Share $share): StreamedResponse
|
|
{
|
|
abort_if(! $share->isCompleted() || $share->isExpired(), 404);
|
|
|
|
$share->load(['files' => fn ($query) => $query->where('is_text', false)]);
|
|
abort_if($share->files->isEmpty(), 404);
|
|
|
|
$key = $this->shareService->sessionDecryptionKey($share, $request->session());
|
|
|
|
// Counted before the body streams: the session is saved by then.
|
|
abort_unless($this->shareService->claimDownload($share, $request->session()), 404);
|
|
|
|
$zip = new ZipStream(
|
|
operationMode: OperationMode::SIMULATE_STRICT,
|
|
defaultCompressionMethod: CompressionMethod::STORE,
|
|
defaultEnableZeroHeader: true,
|
|
sendHttpHeaders: false,
|
|
flushOutput: true,
|
|
);
|
|
|
|
foreach ($share->files as $file) {
|
|
$zip->addFileFromCallback(
|
|
fileName: $this->archiveName($file),
|
|
exactSize: $file->file_size,
|
|
callback: fn (): PumpStream => $this->decryptedStream($share, $file, $key),
|
|
);
|
|
}
|
|
|
|
$size = $zip->finish();
|
|
|
|
return new StreamedResponse(function () use ($zip): void {
|
|
// A large share takes longer than any request limit to send.
|
|
set_time_limit(0);
|
|
|
|
$zip->executeSimulation();
|
|
}, 200, [
|
|
'Content-Type' => 'application/zip',
|
|
'Content-Length' => $size,
|
|
'Content-Disposition' => HeaderUtils::makeDisposition('attachment', 'share-'.$share->token.'.zip'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Download a single file; never the private text, which is only ever shown on the page. A
|
|
* `Range` request gets the bytes from where an interrupted download stopped, so the browser,
|
|
* or the page's folder download, resumes it instead of starting over.
|
|
*/
|
|
public function downloadFile(Request $request, Share $share, ShareFile $shareFile): StreamedResponse
|
|
{
|
|
abort_if(! $share->isCompleted() || $share->isExpired(), 404);
|
|
abort_if($shareFile->share_id !== $share->id || $shareFile->is_text, 404);
|
|
|
|
$key = $this->shareService->sessionDecryptionKey($share, $request->session());
|
|
|
|
$size = $shareFile->file_size;
|
|
$etag = '"'.hash('xxh128', $shareFile->stored_path).'"';
|
|
[$start, $end] = $this->requestedRange($request, $size, $etag) ?? [0, $size - 1];
|
|
|
|
abort_unless($this->shareService->claimDownload($share, $request->session()), 404);
|
|
|
|
$encryptedPath = Storage::disk('shares')->path($share->token.'/'.basename($shareFile->stored_path));
|
|
$partial = $start > 0 || $end < $size - 1;
|
|
|
|
$headers = [
|
|
'Content-Type' => $shareFile->mime_type ?? 'application/octet-stream',
|
|
'Content-Disposition' => HeaderUtils::makeDisposition(
|
|
'attachment',
|
|
$shareFile->original_name,
|
|
'download',
|
|
),
|
|
'Content-Length' => max($end - $start + 1, 0),
|
|
'Accept-Ranges' => 'bytes',
|
|
'ETag' => $etag,
|
|
];
|
|
|
|
if ($partial) {
|
|
$headers['Content-Range'] = "bytes {$start}-{$end}/{$size}";
|
|
}
|
|
|
|
return new StreamedResponse(function () use ($encryptedPath, $key, $start, $end): void {
|
|
set_time_limit(0);
|
|
|
|
$remaining = $end - $start + 1;
|
|
|
|
foreach ($this->encryptionService->decryptedChunks($encryptedPath, $key, $start) as $chunk) {
|
|
if (strlen($chunk) >= $remaining) {
|
|
echo substr($chunk, 0, $remaining);
|
|
flush();
|
|
|
|
return;
|
|
}
|
|
|
|
echo $chunk;
|
|
flush();
|
|
$remaining -= strlen($chunk);
|
|
}
|
|
}, $partial ? 206 : 200, $headers);
|
|
}
|
|
|
|
/**
|
|
* The first and last byte a single `Range` request asks for, or null to send the whole file:
|
|
* without a range, with one this does not read (several ranges, another unit), or with an
|
|
* `If-Range` naming another version of the file. A range wholly past the end is refused.
|
|
*
|
|
* @return array{0: int, 1: int}|null
|
|
*/
|
|
private function requestedRange(Request $request, int $size, string $etag): ?array
|
|
{
|
|
$range = (string) $request->header('Range');
|
|
|
|
if ($size === 0 || ! preg_match('/^bytes=(\d*)-(\d*)$/', $range, $matches) || $matches[1].$matches[2] === ''
|
|
|| ($request->hasHeader('If-Range') && $request->header('If-Range') !== $etag)) {
|
|
return null;
|
|
}
|
|
|
|
[$start, $end] = $matches[1] === ''
|
|
? [max($size - (int) $matches[2], 0), $size - 1]
|
|
: [(int) $matches[1], $matches[2] === '' ? $size - 1 : min((int) $matches[2], $size - 1)];
|
|
|
|
abort_if($start > $end, 416, headers: ['Content-Range' => "bytes */{$size}"]);
|
|
|
|
return [$start, $end];
|
|
}
|
|
|
|
/**
|
|
* A file's decrypted content as a stream ZipStream reads from.
|
|
*/
|
|
private function decryptedStream(Share $share, ShareFile $file, string $key): PumpStream
|
|
{
|
|
$chunks = $this->encryptionService->decryptedChunks(
|
|
Storage::disk('shares')->path($share->token.'/'.basename($file->stored_path)),
|
|
$key,
|
|
);
|
|
|
|
return new PumpStream(function () use ($chunks): string|false {
|
|
while ($chunks->valid() && $chunks->current() === '') {
|
|
$chunks->next();
|
|
}
|
|
|
|
if (! $chunks->valid()) {
|
|
return false;
|
|
}
|
|
|
|
$chunk = $chunks->current();
|
|
$chunks->next();
|
|
|
|
return $chunk;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* A file's path inside the archive: its folder path when it came from a dropped folder, never
|
|
* one that could reach outside the archive.
|
|
*/
|
|
private function archiveName(ShareFile $file): string
|
|
{
|
|
$filename = str_replace('\\', '/', $file->relative_path ?: $file->original_name);
|
|
|
|
if (str_starts_with($filename, '/') || str_contains($filename, '..')) {
|
|
return basename($filename);
|
|
}
|
|
|
|
return $filename;
|
|
}
|
|
}
|