With a download limit of 1, downloading one file of a share with several files deleted the share and the files not yet downloaded. ShareService::recordDownload() ran at the end of every download request, one file or the ZIP alike, and deleted the share as soon as download_count reached max_downloads. It has worked that way since the first commit. - One recipient's visit is one download. The first file or ZIP a session downloads is counted when it starts, in one conditional UPDATE that also checks the limit, so two recipients starting at once can't both take the last download. The session remembers the time, and for ShareService::DOWNLOAD_WINDOW_MINUTES (60) it may start more downloads of the share without counting them, even once the limit is reached. The claim happens in the controller before streaming, because the session is saved before the body is sent, and after the share key is resolved, so a request without the key uses nothing. - A share at its limit is closed to everyone else at once. The hourly cleanup deletes it 24 hours after shares.last_downloaded_at (new column), since a ZIP opens each file only when it reaches it and a large download can outlast the hour. - The download page of a limited share says how many downloads are left, switches to "You have 1 hour" on the first press (Alpine, as a download link does not render the page again), and shows the time left on the next visit. - The admin dashboard shows "2 of 3 downloads", marks shares at their limit "Download limit reached" and leaves them out of Active Shares. - Tests: the regression (3 files, limit 1: every file and the ZIP download, counted once), another recipient, the end of the hour, the last download going to one of two recipients, requests refused before streaming, unlimited shares, the page notes in PHP and in Chromium, the dashboard, and the cleanup at 23 and 25 hours. The tests of recordDownload() and of the instant deletion are gone. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
139 lines
4.6 KiB
PHP
139 lines
4.6 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\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.
|
|
*/
|
|
public function download(Request $request, Share $share): StreamedResponse
|
|
{
|
|
abort_if(! $share->isCompleted() || $share->isExpired(), 404);
|
|
|
|
$share->load('files');
|
|
$key = $this->resolveDecryptionKey($share);
|
|
|
|
// Counted before the body streams: the session is saved by then.
|
|
abort_unless($this->shareService->claimDownload($share, $request->session()), 404);
|
|
|
|
return new StreamedResponse(function () use ($share, $key): void {
|
|
$zip = new ZipStream(
|
|
defaultCompressionMethod: CompressionMethod::STORE,
|
|
defaultEnableZeroHeader: true,
|
|
sendHttpHeaders: false,
|
|
flushOutput: true,
|
|
);
|
|
|
|
foreach ($share->files as $file) {
|
|
$chunks = $this->encryptionService->decryptedChunks(
|
|
Storage::disk('shares')->path($share->token.'/'.basename($file->stored_path)),
|
|
$key,
|
|
);
|
|
|
|
$zip->addFileFromPsr7Stream(fileName: $this->archiveName($file), stream: 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;
|
|
}));
|
|
}
|
|
|
|
$zip->finish();
|
|
}, 200, [
|
|
'Content-Type' => 'application/zip',
|
|
'Content-Disposition' => HeaderUtils::makeDisposition('attachment', 'share-'.$share->token.'.zip'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Download a single file.
|
|
*/
|
|
public function downloadFile(Request $request, Share $share, ShareFile $shareFile): StreamedResponse
|
|
{
|
|
abort_if(! $share->isCompleted() || $share->isExpired(), 404);
|
|
abort_if($shareFile->share_id !== $share->id, 404);
|
|
|
|
$key = $this->resolveDecryptionKey($share);
|
|
|
|
abort_unless($this->shareService->claimDownload($share, $request->session()), 404);
|
|
|
|
$encryptedPath = Storage::disk('shares')->path($share->token.'/'.basename($shareFile->stored_path));
|
|
$mimeType = $shareFile->mime_type ?? 'application/octet-stream';
|
|
|
|
$headers = [
|
|
'Content-Type' => $mimeType,
|
|
'Content-Disposition' => 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): void {
|
|
$this->encryptionService->streamDecryptedFile($encryptedPath, $key);
|
|
}, 200, $headers);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Resolve the decryption key from session or share.
|
|
*/
|
|
private function resolveDecryptionKey(Share $share): string
|
|
{
|
|
if ($share->isPasswordProtected()) {
|
|
$key = session('share_key_'.$share->token);
|
|
|
|
abort_if(! $key, 403, 'Password required');
|
|
|
|
return $key;
|
|
}
|
|
|
|
return $this->shareService->getDecryptionKey($share);
|
|
}
|
|
}
|