From eb7cf686ea6b89fa962686effe7b9f239c4b477c Mon Sep 17 00:00:00 2001 From: PovilasKorop Date: Mon, 6 Dec 2021 08:10:53 +0200 Subject: [PATCH] Task 4 - download file --- README.md | 8 ++++++++ app/Http/Controllers/HouseController.php | 6 ++++++ routes/web.php | 2 ++ tests/Feature/FileUploadTest.php | 15 ++++++++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bac1d85..ceb078d 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,11 @@ In `app/Http/Controllers/HouseController.php` file, in the `update()` method, we Test method `test_update_file_remove_old_one()`. --- + +## Task 4. Download the Uploaded File + +In `app/Http/Controllers/HouseController.php` file, in the `download()` method, return the response that would automatically download the file with `$house->photo` filename from `storage/app/houses` folder. + +Test method `test_download_uploaded_file()`. + +--- diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index 1ce61f8..c330f8a 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -33,4 +33,10 @@ class HouseController extends Controller return 'Success'; } + + public function download(House $house) + { + // TASK: Return the $house->photo file from "storage/app/houses" folder + // for download in browser + } } diff --git a/routes/web.php b/routes/web.php index df14a28..f011615 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,4 +18,6 @@ Route::get('/', function () { }); Route::post('projects', [\App\Http\Controllers\ProjectController::class, 'store']); + +Route::get('houses/download/{house}', [\App\Http\Controllers\HouseController::class, 'download']); Route::resource('houses', \App\Http\Controllers\HouseController::class); diff --git a/tests/Feature/FileUploadTest.php b/tests/Feature/FileUploadTest.php index c460583..bd83399 100644 --- a/tests/Feature/FileUploadTest.php +++ b/tests/Feature/FileUploadTest.php @@ -54,11 +54,24 @@ class FileUploadTest extends TestCase $house = House::first(); $this->assertTrue(Storage::exists($house->photo)); - $response = $this->put('houses/1', [ + $response = $this->put('houses/' . $house->id, [ 'name' => 'Some name', 'photo' => UploadedFile::fake()->image('photo2.jpg') ]); $response->assertStatus(200); $this->assertFalse(Storage::exists($house->photo)); } + + public function test_download_uploaded_file() + { + $this->post('houses', [ + 'name' => 'Some name', + 'photo' => UploadedFile::fake()->image('photo.jpg') + ]); + $house = House::first(); + + $response = $this->get('houses/download/' . $house->id); + $response->assertStatus(200); + $response->assertDownload(str_replace('houses/', '', $house->photo)); + } }