Upload file to public disk

This commit is contained in:
PovilasKorop
2021-12-06 11:07:06 +02:00
parent eb7cf686ea
commit 7233196e0c
7 changed files with 110 additions and 0 deletions
+8
View File
@@ -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()`.
---
+30
View File
@@ -0,0 +1,30 @@
<?php
namespace App\Http\Controllers;
use App\Models\Office;
use Illuminate\Http\Request;
class OfficeController extends Controller
{
public function store(Request $request)
{
$filename = $request->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'));
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Office extends Model
{
use HasFactory;
protected $fillable = ['name', 'photo'];
}
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOfficesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('offices', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('photo');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('offices');
}
}
+5
View File
@@ -0,0 +1,5 @@
Office: {{ $office->name }}
<br />
<img src="{{ public_path('offices/' . $office->photo) }}" />
+2
View File
@@ -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);
+19
View File
@@ -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));
}
}