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>
83 lines
2.8 KiB
PHP
83 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\Share;
|
|
use App\Models\ShareFile;
|
|
use App\Services\ShareService;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
|
|
#[Layout('layouts.app')]
|
|
class AdminDashboard extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
/**
|
|
* The orders the shares list offers, each a column and a direction.
|
|
*
|
|
* @var array<string, array{0: string, 1: string}>
|
|
*/
|
|
public const SORTS = [
|
|
'newest' => ['created_at', 'desc'],
|
|
'oldest' => ['created_at', 'asc'],
|
|
'expiring' => ['expires_at', 'asc'],
|
|
'largest' => ['total_size', 'desc'],
|
|
'most-downloaded' => ['download_count', 'desc'],
|
|
'most-files' => ['files_count', 'desc'],
|
|
];
|
|
|
|
public string $sort = 'newest';
|
|
|
|
/** The share the delete dialog is asking about, while it is open. */
|
|
public ?int $deletingShareId = null;
|
|
|
|
public function deleteShare(int $shareId, ShareService $shareService): void
|
|
{
|
|
$share = Share::query()->findOrFail($shareId);
|
|
$shareService->deleteShare($share);
|
|
|
|
$this->deletingShareId = null;
|
|
}
|
|
|
|
/**
|
|
* A new order starts again from the first page.
|
|
*/
|
|
public function updatedSort(): void
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function render(): mixed
|
|
{
|
|
$shareService = app(ShareService::class);
|
|
|
|
// The sort comes from the browser: only a known order reaches the query.
|
|
[$column, $direction] = self::SORTS[$this->sort] ?? self::SORTS['newest'];
|
|
|
|
// Shares whose files are still being uploaded are not shares yet; their bytes do count as used space.
|
|
$shares = Share::query()
|
|
->whereNotNull('completed_at')
|
|
->withCount('files')
|
|
// Shares that never expire come after every share that does, whichever way expiry is sorted.
|
|
->when($column === 'expires_at', fn ($query) => $query->orderByRaw('expires_at is null'))
|
|
->orderBy($column, $direction)
|
|
->orderByDesc('id')
|
|
->paginate(15);
|
|
|
|
return view('livewire.admin.admin-dashboard', [
|
|
'shares' => $shares,
|
|
'totalShares' => Share::query()->whereNotNull('completed_at')->count(),
|
|
'activeShares' => Share::query()->whereNotNull('completed_at')->where(function ($q) {
|
|
$q->whereNull('expires_at')->orWhere('expires_at', '>', now());
|
|
})->where(function ($q) {
|
|
$q->whereNull('max_downloads')->orWhereColumn('download_count', '<', 'max_downloads');
|
|
})->count(),
|
|
'totalFiles' => ShareFile::query()->whereHas('share', fn ($query) => $query->whereNotNull('completed_at'))->count(),
|
|
'usedSpace' => $shareService->getTotalUsedSpace(),
|
|
'maxQuota' => $shareService->getMaxStorageQuota(),
|
|
]);
|
|
}
|
|
}
|