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>
107 lines
4.4 KiB
PHP
107 lines
4.4 KiB
PHP
<?php
|
|
|
|
use App\Models\Share;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Livewire\Features\SupportFileUploads\FileUploadConfiguration;
|
|
|
|
test('cleanup removes expired shares', function () {
|
|
Storage::fake('shares');
|
|
|
|
$expired = Share::factory()->expired()->create();
|
|
$active = Share::factory()->expiresInHours(24)->create();
|
|
$noExpiry = Share::factory()->create(['expires_at' => null]);
|
|
|
|
$this->artisan('shares:cleanup')
|
|
->expectsOutputToContain('Cleaned up 1 expired share(s)')
|
|
->assertExitCode(0);
|
|
|
|
expect(Share::query()->find($expired->id))->toBeNull();
|
|
expect(Share::query()->find($active->id))->not->toBeNull();
|
|
expect(Share::query()->find($noExpiry->id))->not->toBeNull();
|
|
});
|
|
|
|
test('cleanup removes shares that reached download limit', function () {
|
|
Storage::fake('shares');
|
|
|
|
$reachedLimit = Share::factory()->withMaxDownloads(5)->create(['download_count' => 5]);
|
|
$underLimit = Share::factory()->withMaxDownloads(5)->create(['download_count' => 3]);
|
|
|
|
$this->artisan('shares:cleanup')
|
|
->expectsOutputToContain('Cleaned up 1 expired share(s)')
|
|
->assertExitCode(0);
|
|
|
|
expect(Share::query()->find($reachedLimit->id))->toBeNull();
|
|
expect(Share::query()->find($underLimit->id))->not->toBeNull();
|
|
});
|
|
|
|
test('cleanup keeps a share at its download limit for a day after its last download', function () {
|
|
Storage::fake('shares');
|
|
$recentlyDownloaded = Share::factory()->withMaxDownloads(1)->create(['download_count' => 1, 'last_downloaded_at' => now()->subHours(23)]);
|
|
$downloadedYesterday = Share::factory()->withMaxDownloads(1)->create(['download_count' => 1, 'last_downloaded_at' => now()->subHours(25)]);
|
|
|
|
$this->artisan('shares:cleanup')
|
|
->expectsOutputToContain('Cleaned up 1 expired share(s)')
|
|
->assertExitCode(0);
|
|
|
|
$this->assertModelExists($recentlyDownloaded);
|
|
$this->assertModelMissing($downloadedYesterday);
|
|
});
|
|
|
|
test('cleanup handles no expired shares', function () {
|
|
Storage::fake('shares');
|
|
|
|
Share::factory()->create(['expires_at' => null]);
|
|
|
|
$this->artisan('shares:cleanup')
|
|
->expectsOutputToContain('Cleaned up 0 expired share(s)')
|
|
->assertExitCode(0);
|
|
});
|
|
|
|
test('cleanup removes both expired and download-limited shares', function () {
|
|
Storage::fake('shares');
|
|
|
|
$expired = Share::factory()->expired()->create();
|
|
$limitReached = Share::factory()->withMaxDownloads(1)->create(['download_count' => 1]);
|
|
$active = Share::factory()->create(['expires_at' => null]);
|
|
|
|
$this->artisan('shares:cleanup')
|
|
->expectsOutputToContain('Cleaned up 2 expired share(s)')
|
|
->assertExitCode(0);
|
|
|
|
expect(Share::query()->find($expired->id))->toBeNull();
|
|
expect(Share::query()->find($limitReached->id))->toBeNull();
|
|
expect(Share::query()->find($active->id))->not->toBeNull();
|
|
});
|
|
|
|
test('cleanup removes uploads no chunk reached for 4 hours, and keeps recent ones and completed shares', function () {
|
|
Storage::fake('shares');
|
|
$this->freezeTime();
|
|
$abandoned = Share::factory()->pending()->create(['updated_at' => now()->subHours(4)->subMinute()]);
|
|
Storage::disk('shares')->put($abandoned->token.'/file.enc', 'encrypted');
|
|
$recent = Share::factory()->pending()->create(['updated_at' => now()->subHours(3)]);
|
|
$completed = Share::factory()->create(['updated_at' => now()->subDays(3)]);
|
|
|
|
$this->artisan('shares:cleanup')
|
|
->expectsOutputToContain('Cleaned up 1 abandoned upload(s)')
|
|
->assertExitCode(0);
|
|
|
|
$this->assertModelMissing($abandoned);
|
|
$this->assertModelExists($recent);
|
|
$this->assertModelExists($completed);
|
|
expect(Storage::disk('shares')->directories())->not->toContain($abandoned->token);
|
|
});
|
|
|
|
test('cleanup removes temporary upload files older than 4 hours and keeps newer ones', function () {
|
|
$storage = FileUploadConfiguration::storage();
|
|
$storage->put(FileUploadConfiguration::path('old.pdf'), 'unencrypted leftover');
|
|
$storage->put(FileUploadConfiguration::path('new.png'), 'a logo being chosen');
|
|
touch($storage->path(FileUploadConfiguration::path('old.pdf')), now()->subHours(5)->getTimestamp());
|
|
|
|
$this->artisan('shares:cleanup')
|
|
->expectsOutputToContain('Cleaned up 1 temporary upload file(s)')
|
|
->assertExitCode(0);
|
|
|
|
expect($storage->exists(FileUploadConfiguration::path('old.pdf')))->toBeFalse();
|
|
expect($storage->exists(FileUploadConfiguration::path('new.png')))->toBeTrue();
|
|
});
|