Skip to content

Commit a2c9d24

Browse files
committed
Test uploading multiple files
1 parent 05286dd commit a2c9d24

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

app/Http/Controllers/MediaUploaderController.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace App\Http\Controllers;
44

5-
use Illuminate\Http\Request;
5+
use App\Http\Requests\UploadMultipleFilesRequest;
6+
use Illuminate\Http\UploadedFile;
7+
use Illuminate\Support\Str;
68

79
class MediaUploaderController extends Controller
810
{
@@ -34,4 +36,22 @@ public function validateUpload()
3436

3537
return ['path' => $path];
3638
}
39+
40+
public function uploadMultipleFiles(UploadMultipleFilesRequest $request)
41+
{
42+
$paths = [];
43+
44+
foreach (\request()->file('files') as $key => $file) {
45+
if ($file instanceof UploadedFile) {
46+
$newFilename = Str::random(16) . '.' . $file->getClientOriginalExtension(); // do not believe it
47+
48+
// points to "app/public/validated" dir
49+
$path = $file->storeAs('validated', $newFilename, 'public');
50+
51+
$paths[$key] = $path;
52+
}
53+
}
54+
55+
return $paths;
56+
}
3757
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Http\UploadedFile;
7+
use Illuminate\Validation\Rule;
8+
9+
class UploadMultipleFilesRequest extends FormRequest
10+
{
11+
/**
12+
* Determine if the user is authorized to make this request.
13+
*
14+
* @return bool
15+
*/
16+
public function authorize()
17+
{
18+
return true;
19+
}
20+
21+
/**
22+
* Get the validation rules that apply to the request.
23+
*
24+
* @return array<string, mixed>
25+
*/
26+
public function rules()
27+
{
28+
return [
29+
'files' => ['required', 'array'],
30+
'files.*' => Rule::forEach(function($value, $attribute) {
31+
if ($value instanceof UploadedFile) {
32+
if (in_array($value->getMimeType(), ['image/jpeg', 'image/png', 'application/pdf'])) {
33+
return ['mimes:jpg,png,pdf', 'size:200'];
34+
} elseif ($value->getMimeType() === 'video/mp4') {
35+
return ['mimes:mp4', 'size:1024'];
36+
}
37+
}
38+
39+
return ['prohibited'];
40+
}),
41+
];
42+
}
43+
}

routes/web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@
4747
Route::post('/upload/renamed', 'rename')->name('upload.renamed');
4848

4949
Route::post('/upload/validation', 'validateUpload')->name('upload.validation');
50+
51+
Route::post('/upload/multiple-files', 'uploadMultipleFiles')->name('upload.multiple');
5052
});
5153
});

tests/Feature/UploadModuleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,23 @@ public function it_can_upload_valid_image()
6565

6666
Storage::disk('public')->assertExists($response->json('path'));
6767
}
68+
69+
/** @test */
70+
public function it_can_upload_multiple_files()
71+
{
72+
$this->actingAs(User::factory()->create());
73+
74+
Storage::fake('public');
75+
76+
$response = $this->post(route('upload.multiple'), [
77+
'files' => [
78+
UploadedFile::fake()->create('image-filename.jpg', 200, 'image/jpeg'),
79+
UploadedFile::fake()->create('video-filename.mp4', 1024, 'video/mp4'),
80+
UploadedFile::fake()->create('pdf-filename.pdf', 200, 'application/pdf'),
81+
// UploadedFile::fake()->create('some.mp3', 500, 'audio/mp3'),
82+
],
83+
]);
84+
85+
Storage::disk('public')->assertExists($response->json());
86+
}
6887
}

0 commit comments

Comments
 (0)