Skip to content

Commit

Permalink
Completed the filestorage tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
comfortprince committed Oct 25, 2024
1 parent 5b954e6 commit e34e3ef
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 6 deletions.
8 changes: 7 additions & 1 deletion app/Http/Controllers/CompanyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

class CompanyController extends Controller
{
public function create() {
return view("companies.create");
}

public function store(Request $request)
{
$company = Company::create([
'name' => $request->name,
]);

$company->addMediaFromRequest('photo')->toMediaCollection('companies');

return 'Success';
Expand All @@ -20,7 +25,8 @@ public function store(Request $request)
public function show(Company $company)
{
// TASK: retrieve the full URL to the uploaded photo file, using Spatie Media Library
$photo = '???';

$photo = $company->getFirstMedia('companies')->getUrl();

return view('companies.show', compact('company', 'photo'));
}
Expand Down
12 changes: 10 additions & 2 deletions app/Http/Controllers/HouseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class HouseController extends Controller
{
public function create(){
return view("house.create");
}

public function edit(House $house){
return view("house.edit", compact("house"));
}
public function store(Request $request)
{
$filename = $request->file('photo')->store('houses');
Expand All @@ -23,8 +30,8 @@ public function store(Request $request)
public function update(Request $request, House $house)
{
$filename = $request->file('photo')->store('houses');

// TASK: Delete the old file from the storage
$files = Storage::files('houses');
Storage::delete(array_filter($files, fn ($file) => $file !== $filename ));

$house->update([
'name' => $request->name,
Expand All @@ -38,5 +45,6 @@ public function download(House $house)
{
// TASK: Return the $house->photo file from "storage/app/houses" folder
// for download in browser
return Storage::download($house->photo);
}
}
2 changes: 2 additions & 0 deletions app/Http/Controllers/OfficeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function store(Request $request)
// TASK: Upload the file "photo" so it would be written as
// storage/app/public/offices/[original_filename]

$request->photo->storeAs('offices', $filename, 'public');

Office::create([
'name' => $request->name,
'photo' => $filename,
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Models\Project;
use Illuminate\Validation\Rules\File;
use Illuminate\Http\Request;

class ProjectController extends Controller
Expand All @@ -11,11 +12,14 @@ public function store(Request $request)
{
$request->validate([
// TASK: Write the validation rule so "logo" file would be MAX 1 megabyte
'logo' => [
File::image()->max('1024')
]
]);

// TASK: change the below line so that $filename would contain only filename
// The same filename as the original uploaded file
$filename = '???';
$filename = $request->logo->getClientOriginalName();
$request->file('logo')->storeAs('logos', $filename);

Project::create([
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/ShopController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Storage;

class ShopController extends Controller
{
Expand All @@ -16,6 +17,10 @@ public function store(Request $request)
// to size of 500x500 and store it as /storage/app/shops/resized-$filename
// Use intervention/image package, it's already pre-installed for you

$image = Image::make(Storage::get('shops/'.$filename));
$image->resize(500,500);
$image->save(storage_path('app/shops/resized-'.$filename));

return 'Success';
}
}
4 changes: 4 additions & 0 deletions app/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ class Company extends Model implements HasMedia
use HasFactory, InteractsWithMedia;

protected $fillable = ['name'];

public static function last() {
return static::all()->last();
}
}
6 changes: 6 additions & 0 deletions resources/views/companies/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<form action="{{ route('companies.store') }}" method="post" enctype="multipart/form-data">
@csrf
<input type="text" name="name"> <br>
<input type="file" name="photo"> <br>
<input type="submit" value="Send Photo">
</form>
2 changes: 1 addition & 1 deletion resources/views/companies/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

<br />

<img src="{{ $photo }}" />
<img src="{{ $photo }}" />
6 changes: 6 additions & 0 deletions resources/views/file_upload.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<form action="http://127.0.0.1:8000/projects" method="POST" enctype="multipart/form-data">
@csrf
<input type="text" name="name"> <br>
<input type="file" name="logo"> <br>
<input type="submit" value="Upload">
</form>
6 changes: 6 additions & 0 deletions resources/views/house/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<form action="http://127.0.0.1:8000/houses" method="POST" enctype="multipart/form-data">
@csrf
<input type="text" name="name"> <br>
<input type="file" name="photo"> <br>
<input type="submit" value="Upload">
</form>
7 changes: 7 additions & 0 deletions resources/views/house/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<form action="http://127.0.0.1:8000/houses/{{ $house->id }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<input type="text" name="name" value="{{ $house->name }}"> <br>
<input type="file" name="photo"> <br>
<input type="submit" value="Upload">
</form>
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
});

Route::post('projects', [\App\Http\Controllers\ProjectController::class, 'store']);
Route::get('projects', function () {
return view('file_upload');
});

Route::get('houses/download/{house}', [\App\Http\Controllers\HouseController::class, 'download']);
Route::resource('houses', \App\Http\Controllers\HouseController::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class FileUploadTest extends TestCase
{
use RefreshDatabase;
use RefreshDatabase;

public function test_original_filename_upload()
{
Expand Down

0 comments on commit e34e3ef

Please sign in to comment.