From 7233196e0c432b6633fe529a54ca15549e631e78 Mon Sep 17 00:00:00 2001 From: PovilasKorop Date: Mon, 6 Dec 2021 11:07:06 +0200 Subject: [PATCH] Upload file to public disk --- README.md | 8 +++++ app/Http/Controllers/OfficeController.php | 30 +++++++++++++++++ app/Models/Office.php | 13 ++++++++ ...2021_12_06_085315_create_offices_table.php | 33 +++++++++++++++++++ resources/views/offices/show.blade.php | 5 +++ routes/web.php | 2 ++ tests/Feature/FileUploadTest.php | 19 +++++++++++ 7 files changed, 110 insertions(+) create mode 100644 app/Http/Controllers/OfficeController.php create mode 100644 app/Models/Office.php create mode 100644 database/migrations/2021_12_06_085315_create_offices_table.php create mode 100644 resources/views/offices/show.blade.php diff --git a/README.md b/README.md index ceb078d..cb48536 100644 --- a/README.md +++ b/README.md @@ -56,3 +56,11 @@ In `app/Http/Controllers/HouseController.php` file, in the `download()` method, Test method `test_download_uploaded_file()`. --- + +## Task 5. Upload File to Public + +In `app/Http/Controllers/OfficeController.php` file, in the `store()` method, upload the file to the "public" disk, to its "offices" folder. The test asserts that the file exists in the respected location, and is shown in the offices/show.blade.php + +Test method `test_public_file_show()`. + +--- diff --git a/app/Http/Controllers/OfficeController.php b/app/Http/Controllers/OfficeController.php new file mode 100644 index 0000000..fae443f --- /dev/null +++ b/app/Http/Controllers/OfficeController.php @@ -0,0 +1,30 @@ +file('photo')->getClientOriginalName(); + + // TASK: Upload the file "photo" so it would be written as + // storage/app/public/offices/[original_filename] + + Office::create([ + 'name' => $request->name, + 'photo' => $filename, + ]); + + return 'Success'; + } + + public function show(Office $office) + { + return view('offices.show', compact('office')); + } + +} diff --git a/app/Models/Office.php b/app/Models/Office.php new file mode 100644 index 0000000..06d5c0d --- /dev/null +++ b/app/Models/Office.php @@ -0,0 +1,13 @@ +id(); + $table->string('name'); + $table->string('photo'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('offices'); + } +} diff --git a/resources/views/offices/show.blade.php b/resources/views/offices/show.blade.php new file mode 100644 index 0000000..c94245c --- /dev/null +++ b/resources/views/offices/show.blade.php @@ -0,0 +1,5 @@ +Office: {{ $office->name }} + +
+ + diff --git a/routes/web.php b/routes/web.php index f011615..ed68b5d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -21,3 +21,5 @@ 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); + +Route::resource('offices', \App\Http\Controllers\OfficeController::class); diff --git a/tests/Feature/FileUploadTest.php b/tests/Feature/FileUploadTest.php index bd83399..1ccf1cc 100644 --- a/tests/Feature/FileUploadTest.php +++ b/tests/Feature/FileUploadTest.php @@ -3,10 +3,12 @@ namespace Tests\Feature; use App\Models\House; +use App\Models\Office; use App\Models\Project; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; use Tests\TestCase; class FileUploadTest extends TestCase @@ -74,4 +76,21 @@ class FileUploadTest extends TestCase $response->assertStatus(200); $response->assertDownload(str_replace('houses/', '', $house->photo)); } + + public function test_public_file_show() + { + $filename = Str::random(8) . '.jpg'; + + $this->post('offices', [ + 'name' => 'Some name', + 'photo' => UploadedFile::fake()->image($filename) + ]); + $office = Office::first(); + + $this->assertTrue(Storage::disk('public')->exists('offices/' . $filename)); + + $response = $this->get('offices/' . $office->id); + $response->assertStatus(200); + $response->assertSee(public_path('offices/' . $filename)); + } }