From d30d71c2153bb54a45e3e29bbd735c2c706513bc Mon Sep 17 00:00:00 2001 From: thule129 Date: Sat, 4 May 2024 02:40:20 +0700 Subject: [PATCH] first commmit --- app/Http/Controllers/CompanyController.php | 2 +- app/Http/Controllers/HouseController.php | 4 ++++ app/Http/Controllers/OfficeController.php | 3 ++- app/Http/Controllers/ProjectController.php | 4 +++- app/Http/Controllers/ShopController.php | 4 ++++ 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 12fcb81d..93d49bf9 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -20,7 +20,7 @@ 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()->getUrl(); return view('companies.show', compact('company', 'photo')); } diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index c330f8aa..47064cb1 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -25,6 +25,8 @@ public function update(Request $request, House $house) $filename = $request->file('photo')->store('houses'); // TASK: Delete the old file from the storage + $oldPhoto = $house->photo; + Storage::delete($oldPhoto); $house->update([ 'name' => $request->name, @@ -36,6 +38,8 @@ public function update(Request $request, House $house) public function download(House $house) { + $savedPath = $house->photo; + Storage::download($savedPath); // TASK: Return the $house->photo file from "storage/app/houses" folder // for download in browser } diff --git a/app/Http/Controllers/OfficeController.php b/app/Http/Controllers/OfficeController.php index fae443fa..d465fe0a 100644 --- a/app/Http/Controllers/OfficeController.php +++ b/app/Http/Controllers/OfficeController.php @@ -9,10 +9,11 @@ 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] + $filename = $request->file('photo')->getClientOriginalName(); + Storage::disk('public')->putFileAs('offices', $request->file('photo'), $filename); Office::create([ 'name' => $request->name, diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 95aed4f8..0b73dee7 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -4,18 +4,20 @@ use App\Models\Project; use Illuminate\Http\Request; +use Illuminate\Validation\Rules\File; class ProjectController extends Controller { public function store(Request $request) { $request->validate([ + File::image()->max(1e6), // TASK: Write the validation rule so "logo" file would be MAX 1 megabyte ]); // TASK: change the below line so that $filename would contain only filename // The same filename as the original uploaded file - $filename = '???'; + $filename = $request->file('logo')->getClientOriginalName(); $request->file('logo')->storeAs('logos', $filename); Project::create([ diff --git a/app/Http/Controllers/ShopController.php b/app/Http/Controllers/ShopController.php index b2c485a3..807f1a8f 100644 --- a/app/Http/Controllers/ShopController.php +++ b/app/Http/Controllers/ShopController.php @@ -15,6 +15,10 @@ public function store(Request $request) // TASK: resize the uploaded image from /storage/app/shops/$filename // to size of 500x500 and store it as /storage/app/shops/resized-$filename // Use intervention/image package, it's already pre-installed for you + $originalPath = storage_path("app/shops/{$filename}"); + $newImg = Image::make($originalPath)->resize(500, 500); + + $newImg->save(storage_path("app/shops/resized-{$filename}")); return 'Success'; }