Skip to content

Commit

Permalink
Added config for cleaning up old files. (#8296)
Browse files Browse the repository at this point in the history
Co-authored-by: Caleb Porzio <calebporzio@gmail.com>
  • Loading branch information
Boorinio and calebporzio committed Apr 18, 2024
1 parent 10bf80e commit 27436fc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/livewire.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
'jpg', 'jpeg', 'mpga', 'webp', 'wma',
],
'max_upload_time' => 5, // Max duration (in minutes) before an upload is invalidated...
'cleanup' => true, // Should cleanup temporary uploads older than 24 hrs...
],

/*
Expand Down
5 changes: 5 additions & 0 deletions src/Features/SupportFileUploads/FileUploadConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public static function middleware()
return config('livewire.temporary_file_upload.middleware') ?: 'throttle:60,1';
}

public static function shouldCleanupOldUploads()
{
return config('livewire.temporary_file_upload.cleanup', true);
}

public static function rules()
{
$rules = config('livewire.temporary_file_upload.rules');
Expand Down
33 changes: 33 additions & 0 deletions src/Features/SupportFileUploads/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,39 @@ public function temporary_files_older_than_24_hours_are_cleaned_up_on_every_new_
$this->assertCount(1, FileUploadConfiguration::storage()->allFiles());
}

/** @test */
public function temporary_files_older_than_24_hours_are_not_cleaned_up_if_configuration_specifies()
{
config()->set('livewire.temporary_file_upload.cleanup', false);

Storage::fake('avatars');

$file = UploadedFile::fake()->image('avatar.jpg');
$file2 = UploadedFile::fake()->image('avatar.jpg');
$file3 = UploadedFile::fake()->image('avatar.jpg');

Livewire::test(FileUploadComponent::class)
->set('photo', $file)
->call('upload', 'uploaded-avatar.png');

Livewire::test(FileUploadComponent::class)
->set('photo', $file2)
->call('upload', 'uploaded-avatar2.png');

$this->assertCount(2, FileUploadConfiguration::storage()->allFiles());

// Make temporary files look 2 days old.
foreach (FileUploadConfiguration::storage()->allFiles() as $fileShortPath) {
touch(FileUploadConfiguration::storage()->path($fileShortPath), now()->subDays(2)->timestamp);
}

Livewire::test(FileUploadComponent::class)
->set('photo', $file3)
->call('upload', 'uploaded-avatar3.png');

$this->assertCount(3, FileUploadConfiguration::storage()->allFiles());
}

/** @test */
public function temporary_files_older_than_24_hours_are_not_cleaned_up_on_every_new_upload_when_using_S3()
{
Expand Down
4 changes: 3 additions & 1 deletion src/Features/SupportFileUploads/WithFileUploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ function _startUpload($name, $fileInfo, $isMultiple)

function _finishUpload($name, $tmpPath, $isMultiple)
{
$this->cleanupOldUploads();
if (FileUploadConfiguration::shouldCleanupOldUploads()) {
$this->cleanupOldUploads();
}

if ($isMultiple) {
$file = collect($tmpPath)->map(function ($i) {
Expand Down

0 comments on commit 27436fc

Please sign in to comment.