diff --git a/app/Http/Controllers/MediaUploaderController.php b/app/Http/Controllers/MediaUploaderController.php index 99ebb61..64f900f 100644 --- a/app/Http/Controllers/MediaUploaderController.php +++ b/app/Http/Controllers/MediaUploaderController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Http\Requests\UploadMultipleFilesRequest; +use App\Jobs\ImageUploadAndResizingJob; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; @@ -95,4 +96,14 @@ public function download($filename) return response()->download($privateFilepath, $filename); } + + public function uploadAndResizing() + { + $image = request()->file('image'); + + if ($image instanceof UploadedFile) { + ImageUploadAndResizingJob::dispatch($image->getMimeType(), base64_encode($image->getContent())) + ->delay(now()->addSeconds(5)); + } + } } diff --git a/routes/web.php b/routes/web.php index 40cba8c..10b538f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -55,6 +55,8 @@ Route::post('/upload/resize', 'resize')->name('upload.resize'); + Route::post('/upload/resize/via/queue', 'uploadAndResizing')->name('upload.resize.via.queue'); + Route::post('/upload/private', 'uploadPrivate')->name('upload.private'); Route::get('/upload/download/{filename}', 'download')->name('upload.download'); diff --git a/tests/Feature/JobTest.php b/tests/Feature/JobTest.php index c77f97c..a7066f3 100644 --- a/tests/Feature/JobTest.php +++ b/tests/Feature/JobTest.php @@ -3,13 +3,18 @@ namespace Tests\Feature; use App\Jobs\ImageUploadAndResizingJob; +use App\Models\User; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; +use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Queue; use Tests\TestCase; class JobTest extends TestCase { + use RefreshDatabase; + /** @test */ public function a_job_is_dispatchable() { @@ -22,4 +27,20 @@ public function a_job_is_dispatchable() 'handle' )); } + + /** @test */ + public function queue_api_can_be_instructed_to_dispatch_image_upload_and_resizing_job() + { + $this->actingAs(User::factory()->create()); + + Queue::fake(); + + $this->post(route('upload.resize.via.queue'), [ + 'image' => $image = UploadedFile::fake() + ->image('image.jpg', 50, 50) + ->mimeType('image/jpeg') + ]); + + Queue::assertPushed(ImageUploadAndResizingJob::class); + } }