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>
80 lines
2.6 KiB
PHP
80 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Models\Share;
|
|
use App\Services\ShareService;
|
|
use Carbon\CarbonInterval;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.app')]
|
|
class ShareDownload extends Component
|
|
{
|
|
public Share $share;
|
|
|
|
public bool $authenticated = false;
|
|
|
|
#[Validate('required|string')]
|
|
public string $password = '';
|
|
|
|
public function mount(Share $share, ShareService $shareService): void
|
|
{
|
|
$this->share = $share->load('files');
|
|
|
|
// A share at its download limit stays open for the recipient who took its last download.
|
|
if (! $share->isCompleted() || $share->isExpired()
|
|
|| ($share->hasReachedDownloadLimit() && $shareService->downloadWindowEndsAt($share, session()->driver()) === null)) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! $share->isPasswordProtected()) {
|
|
$this->authenticated = true;
|
|
}
|
|
|
|
if ($share->isPasswordProtected() && session('share_key_'.$share->token)) {
|
|
$this->authenticated = true;
|
|
}
|
|
}
|
|
|
|
public function verifyPassword(ShareService $shareService): void
|
|
{
|
|
$rateLimitKey = 'share-password:'.$this->share->token.'|'.request()->ip();
|
|
|
|
if (RateLimiter::tooManyAttempts($rateLimitKey, 5)) {
|
|
$seconds = RateLimiter::availableIn($rateLimitKey);
|
|
$this->addError('password', __('Too many attempts. Please try again in :seconds seconds.', ['seconds' => $seconds]));
|
|
|
|
return;
|
|
}
|
|
|
|
$this->validate();
|
|
|
|
if (! $shareService->verifyPassword($this->share, $this->password)) {
|
|
RateLimiter::hit($rateLimitKey, 60);
|
|
$this->addError('password', __('The password is incorrect.'));
|
|
|
|
return;
|
|
}
|
|
|
|
RateLimiter::clear($rateLimitKey);
|
|
|
|
$encryptionKey = $shareService->getDecryptionKey($this->share, $this->password);
|
|
session(['share_key_'.$this->share->token => $encryptionKey]);
|
|
$this->authenticated = true;
|
|
}
|
|
|
|
public function render(): mixed
|
|
{
|
|
$shareService = app(ShareService::class);
|
|
|
|
return view('livewire.share-download', [
|
|
'downloadWindowEndsAt' => $shareService->downloadWindowEndsAt($this->share, session()->driver()),
|
|
'remainingDownloads' => $this->share->max_downloads ? max($this->share->max_downloads - $this->share->download_count, 0) : null,
|
|
'downloadWindow' => CarbonInterval::minutes(ShareService::DOWNLOAD_WINDOW_MINUTES)->cascade()->forHumans(),
|
|
]);
|
|
}
|
|
}
|