Skip to content

Commit

Permalink
Allows uploads by all
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jul 23, 2024
1 parent 7da9e85 commit bb5498b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 42 deletions.
6 changes: 2 additions & 4 deletions app/Livewire/Questions/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Models\User;
use App\Rules\MaxUploads;
use App\Rules\NoBlankCharacters;
use App\Rules\VerifiedOnly;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -35,11 +34,11 @@ final class Create extends Component
#[Locked]
public int $uploadLimit = 1;

#[Locked]
/**
* Max file size allowed.
*/
public int $maxFileSize = 2048;
#[Locked]
public int $maxFileSize = 1024 * 8;

/**
* The component's user ID.
Expand Down Expand Up @@ -90,7 +89,6 @@ public function runImageValidation(): void
rules: [
'images' => [
'bail',
new VerifiedOnly(),
new MaxUploads($this->uploadLimit),
],
'images.*' => [
Expand Down
54 changes: 26 additions & 28 deletions resources/views/livewire/questions/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,33 @@ class="relative group/menu">
x-ref="content"
/>

@if (auth()->user()?->is_verified || auth()->user()?->is_company_verified)
<div
class="absolute top-0 right-0 mt-2 mr-2 group-hover/menu:inline-block hidden">
<button title="Upload an image" x-ref="imageButton"
:disabled="uploading || images.length >= uploadLimit"
class="rounded-lg bg-slate-800 text-sm text-slate-400 p-1.5 hover:text-pink-500"
:class="{'cursor-not-allowed text-pink-500': uploading || images.length >= uploadLimit}"
>
<x-heroicon-o-camera class="h-5 w-5"/>
</button>
</div>
<input class="hidden" type="file" x-ref="imageInput" accept="image/*" />
<input class="hidden" type="file" x-ref="imageUpload" accept="image/*" wire:model="images" />
<div
class="absolute top-0 right-0 mt-2 mr-2 group-hover/menu:inline-block hidden">
<button title="Upload an image" x-ref="imageButton"
:disabled="uploading || images.length >= uploadLimit"
class="rounded-lg bg-slate-800 text-sm text-slate-400 p-1.5 hover:text-pink-500"
:class="{'cursor-not-allowed text-pink-500': uploading || images.length >= uploadLimit}"
>
<x-heroicon-o-camera class="h-5 w-5"/>
</button>
</div>
<input class="hidden" type="file" x-ref="imageInput" accept="image/*" />
<input class="hidden" type="file" x-ref="imageUpload" accept="image/*" wire:model="images" />

<div x-show="images.length > 0" class="relative mt-2 flex h-20 flex-wrap gap-2">
<template x-for="(image, index) in images" :key="index">
<div class="relative h-20 w-20">
<img :src="image.path" :alt="image.originalName"
x-on:click="createMarkdownImage(index)"
title="Reinsert the image"
class="h-full w-full rounded-lg object-cover cursor-pointer"/>
<button @click="removeImage($event, index)"
class="absolute top-0.5 right-0.5 p-1 rounded-md bg-slate-800 bg-opacity-75 text-slate-400 hover:text-pink-500">
<x-icons.close class="size-4"/>
</button>
</div>
</template>
</div>
@endif
<div x-show="images.length > 0" class="relative mt-2 flex h-20 flex-wrap gap-2">
<template x-for="(image, index) in images" :key="index">
<div class="relative h-20 w-20">
<img :src="image.path" :alt="image.originalName"
x-on:click="createMarkdownImage(index)"
title="Reinsert the image"
class="h-full w-full rounded-lg object-cover cursor-pointer"/>
<button @click="removeImage($event, index)"
class="absolute top-0.5 right-0.5 p-1 rounded-md bg-slate-800 bg-opacity-75 text-slate-400 hover:text-pink-500">
<x-icons.close class="size-4"/>
</button>
</div>
</template>
</div>

<p class="text-right text-xs text-slate-400"><span x-text="$wire.content.length"></span> / {{ $this->maxContentLength}}</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
avatar: null,
errors: @json($errors->get('avatar')),
checkFileSize(target) {
const maxFileSize = 2 * 1024 * 1024;
const maxFileSize = 8 * 1024 * 1024;
if ((target.files[0]?.size ?? 0) > maxFileSize) {
this.errors = ["The avatar may not be greater than 2MB."];
target.value = null;
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/Livewire/Questions/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,11 @@
'toId' => $user->id,
]);

expect($component->maxFileSize)->toBe(1024 * 2)
expect($component->maxFileSize)->toBe(1024 * 8)
->and($component->uploadLimit)->toBe(1);
});

test('only verified users can upload images', function () {
test('non verified users can upload images', function () {
$user = User::factory()->unverified()->create();

$component = Livewire::actingAs($user)->test(Create::class, [
Expand All @@ -505,9 +505,7 @@
$component->set('images', [UploadedFile::fake()->image('test.jpg')]);
$component->call('uploadImages');

$component->assertHasErrors([
'images' => 'This action is only available to verified users. Get verified in your profile settings.',
]);
$component->assertHasNoErrors();
});

test('company verified users can upload images', function () {
Expand Down Expand Up @@ -583,19 +581,21 @@
'is_verified' => true,
]);

$maxFileSize = 1024 * 2;
$maxFileSize = 1024 * 8;

$component = Livewire::actingAs($user)->test(Create::class, [
'toId' => $user->id,
]);

$largeFile = UploadedFile::fake()->image('test.jpg')->size(1024 * 5);
$largeFile = UploadedFile::fake()->image('test.jpg')->size(1024 * 16);

$component->set('images', [$largeFile]);
$component->call('runImageValidation');

expect($component->get('images'))->toBeArray()
->and($component->get('images'))->not()->toContain($largeFile);
expect($component->get('images'))
->toBeArray()
->and($component->get('images'))
->not()->toContain($largeFile);

$component->assertHasErrors([
'images.0' => "The image may not be greater than {$maxFileSize} kilobytes.",
Expand Down

0 comments on commit bb5498b

Please sign in to comment.