From 5b562bd31c92178fab8585b4547aed3df37f16bf Mon Sep 17 00:00:00 2001 From: zhiyuan Date: Sat, 29 Mar 2025 20:00:29 +0800 Subject: [PATCH 1/2] test --- app/Http/Controllers/HouseController.php | 3 +++ app/Http/Controllers/OfficeController.php | 2 +- app/Http/Controllers/ProjectController.php | 6 ++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index c330f8aa..ce744d41 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -25,6 +25,7 @@ public function update(Request $request, House $house) $filename = $request->file('photo')->store('houses'); // TASK: Delete the old file from the storage + Storage::delete($house->photo); $house->update([ 'name' => $request->name, @@ -38,5 +39,7 @@ 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); + } } diff --git a/app/Http/Controllers/OfficeController.php b/app/Http/Controllers/OfficeController.php index fae443fa..a6331383 100644 --- a/app/Http/Controllers/OfficeController.php +++ b/app/Http/Controllers/OfficeController.php @@ -13,7 +13,7 @@ public function store(Request $request) // TASK: Upload the file "photo" so it would be written as // storage/app/public/offices/[original_filename] - + $request->file('photo')->storeAs('offices', $filename); Office::create([ 'name' => $request->name, 'photo' => $filename, diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 95aed4f8..a5f3815d 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -4,6 +4,7 @@ use App\Models\Project; use Illuminate\Http\Request; +use Illuminate\Validation\Rules\File; class ProjectController extends Controller { @@ -11,13 +12,14 @@ public function store(Request $request) { $request->validate([ // TASK: Write the validation rule so "logo" file would be MAX 1 megabyte + 'logo' => ['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->file('logo')->getClientOriginalName(); $request->file('logo')->storeAs('logos', $filename); - Project::create([ 'name' => $request->name, 'logo' => $filename, From ff9d1ceda7cd1aa0255be5ef358b1865ebf6590c Mon Sep 17 00:00:00 2001 From: zhiyuan Date: Sat, 29 Mar 2025 21:03:48 +0800 Subject: [PATCH 2/2] complete task --- app/Http/Controllers/CompanyController.php | 2 +- app/Http/Controllers/OfficeController.php | 2 +- app/Http/Controllers/ShopController.php | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 12fcb81d..3125deaf 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->getFirstMediaUrl('companies'); return view('companies.show', compact('company', 'photo')); } diff --git a/app/Http/Controllers/OfficeController.php b/app/Http/Controllers/OfficeController.php index a6331383..b8ac65b3 100644 --- a/app/Http/Controllers/OfficeController.php +++ b/app/Http/Controllers/OfficeController.php @@ -13,7 +13,7 @@ public function store(Request $request) // TASK: Upload the file "photo" so it would be written as // storage/app/public/offices/[original_filename] - $request->file('photo')->storeAs('offices', $filename); + $request->file('photo')->storeAs('public/offices', $filename); Office::create([ 'name' => $request->name, 'photo' => $filename, diff --git a/app/Http/Controllers/ShopController.php b/app/Http/Controllers/ShopController.php index b2c485a3..70073e0d 100644 --- a/app/Http/Controllers/ShopController.php +++ b/app/Http/Controllers/ShopController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Intervention\Image\Facades\Image; +use Illuminate\Support\Facades\Storage; class ShopController extends Controller { @@ -15,7 +16,9 @@ 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 - + $imagePath = storage_path('app/shops/' . $filename); + $resizedImage = Image::make($imagePath)->resize(500, 500); + $resizedImage->save(storage_path('app/shops/resized-' . $filename)); return 'Success'; } }