Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions app/Livewire/Dashboard/DashboardConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Livewire\Dashboard;

use App\Actions\Files\StoreUploadedFileAction;
use App\Exceptions\Files\FileStorageException;
use App\Exceptions\Files\UnsupportedFileFormatException;
use App\Models\FileRecord;
use App\Support\Files\UploadedFileRules;
use Livewire\Component;
Expand All @@ -26,13 +28,31 @@ public function storeUpload(StoreUploadedFileAction $storeUploadedFile): void
$this->uploadError = null;

$this->validate([
'upload' => UploadedFileRules::rules(),
'upload' => [
'required',
'file',
'max:'.UploadedFileRules::MAX_FILE_KILOBYTES,
],
], [
'upload.max' => 'This file is too large. Max upload size is '.(UploadedFileRules::MAX_FILE_KILOBYTES / 1024).' MB.',
]);

$fileRecord = $storeUploadedFile->handle(
user: auth()->user(),
file: $this->upload,
);
try {
$fileRecord = $storeUploadedFile->handle(
user: auth()->user(),
file: $this->upload,
);
} catch (UnsupportedFileFormatException) {
$this->uploadError = 'This file type is not supported in beta. Upload PNG, JPG, WEBP or PDF.';
$this->step = 'upload';

return;
} catch (FileStorageException) {
$this->uploadError = 'We could not store your file. Please try again.';
$this->step = 'upload';

return;
}

$this->currentFileId = $fileRecord->id;
$this->step = 'format';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class="flex flex-col items-center justify-center gap-4 rounded-[var(--ca-radius-
@error('upload')
<p class="text-sm text-[var(--ca-danger)]">{{ $message }}</p>
@enderror

@if ($uploadError)
<p class="text-sm text-[var(--ca-danger)]">{{ $uploadError }}</p>
@endif
</div>
@endif

Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/Livewire/DashboardConverterUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@
->assertDontSee('remove-me.png');
});

it('shows an error for unsupported upload format', function () {
Storage::fake('local');

$user = User::factory()->create();

Livewire::actingAs($user)
->test(DashboardConverter::class)
->set('upload', UploadedFile::fake()->create('notes.txt', 10, 'text/plain'))
->call('storeUpload')
->assertSet('step', 'upload')
->assertSee('not supported');
});

it('shows uploaded file summary after upload', function () {
Storage::fake('local');

Expand Down