From bc214378599288f9f9d185adeafbe1cdf915b7c2 Mon Sep 17 00:00:00 2001 From: maged-web Date: Sun, 5 May 2024 00:56:18 +0300 Subject: [PATCH 1/8] answers --- app/Http/Controllers/CompanyController.php | 2 +- app/Http/Controllers/HouseController.php | 14 ++++++++++++++ app/Http/Controllers/OfficeController.php | 2 +- app/Http/Controllers/ProjectController.php | 4 +++- app/Http/Controllers/ShopController.php | 13 ++++++++++++- 5 files changed, 31 insertions(+), 4 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/HouseController.php b/app/Http/Controllers/HouseController.php index c330f8aa..1a850bfc 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -22,6 +22,7 @@ public function store(Request $request) public function update(Request $request, House $house) { + $fileOld=$house->photo; $filename = $request->file('photo')->store('houses'); // TASK: Delete the old file from the storage @@ -31,6 +32,10 @@ public function update(Request $request, House $house) 'photo' => $filename, ]); + if($fileOld && !$fileOld==$filename) + { + Storage::delete($fileOld); + } return 'Success'; } @@ -38,5 +43,14 @@ public function download(House $house) { // TASK: Return the $house->photo file from "storage/app/houses" folder // for download in browser + $filePath=storage_path('app/houses' . $house->photo); + if(file_exists($filePath)) + { + return response()->download($filePath); + + } + else + abort(404, 'File not found'); + } } diff --git a/app/Http/Controllers/OfficeController.php b/app/Http/Controllers/OfficeController.php index fae443fa..528efa7d 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('public/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..683f9d63 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -11,11 +11,13 @@ public function store(Request $request) { $request->validate([ // TASK: Write the validation rule so "logo" file would be MAX 1 megabyte + 'logo'=>'required|file|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([ diff --git a/app/Http/Controllers/ShopController.php b/app/Http/Controllers/ShopController.php index b2c485a3..8d6f8572 100644 --- a/app/Http/Controllers/ShopController.php +++ b/app/Http/Controllers/ShopController.php @@ -14,7 +14,18 @@ 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 + $originalName=storage_path('app/shops/' . $filename); + + + $resizedImage=Image::make($originalName)->resize(500,500)->encode(); + + + $resizedImageName='resized-' . $filename; + + + $resizedImagePath=storage_path('app/shops/' . $resizedImageName); + + file_put_contents($resizedImagePath,$resizedImage); return 'Success'; } From eed68b818660abe543afeb82eb8046b15d0da7d2 Mon Sep 17 00:00:00 2001 From: maged-web Date: Sun, 5 May 2024 01:09:40 +0300 Subject: [PATCH 2/8] V2 --- app/Http/Controllers/HouseController.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index 1a850bfc..755e4563 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -22,20 +22,19 @@ public function store(Request $request) public function update(Request $request, House $house) { - $fileOld=$house->photo; $filename = $request->file('photo')->store('houses'); // TASK: Delete the old file from the storage + if($house->photo) + { + Storage::delete($house->photo); + } $house->update([ 'name' => $request->name, 'photo' => $filename, ]); - if($fileOld && !$fileOld==$filename) - { - Storage::delete($fileOld); - } return 'Success'; } @@ -43,14 +42,13 @@ public function download(House $house) { // TASK: Return the $house->photo file from "storage/app/houses" folder // for download in browser - $filePath=storage_path('app/houses' . $house->photo); + $filePath = storage_path("app/houses/{$house->photo}"); if(file_exists($filePath)) { return response()->download($filePath); - } - else - abort(404, 'File not found'); - + else { + return response()->json(['error' => 'File not found'], 404); + } } -} +} \ No newline at end of file From f8768a2a4ee0b4cc58c5f58c45c941bdb9493e96 Mon Sep 17 00:00:00 2001 From: maged-web Date: Sun, 5 May 2024 01:14:17 +0300 Subject: [PATCH 3/8] V3 --- app/Http/Controllers/HouseController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index 755e4563..8786c9f6 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -4,6 +4,7 @@ use App\Models\House; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Storage; class HouseController extends Controller @@ -45,7 +46,7 @@ public function download(House $house) $filePath = storage_path("app/houses/{$house->photo}"); if(file_exists($filePath)) { - return response()->download($filePath); + return Storage::download($filePath); } else { return response()->json(['error' => 'File not found'], 404); From 82b2cc064840f49a80a2c37246dab36214ed80b5 Mon Sep 17 00:00:00 2001 From: maged-web Date: Sun, 5 May 2024 01:23:43 +0300 Subject: [PATCH 4/8] v4 --- app/Http/Controllers/HouseController.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index 8786c9f6..7f98e98b 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -42,14 +42,9 @@ public function update(Request $request, House $house) public function download(House $house) { // TASK: Return the $house->photo file from "storage/app/houses" folder - // for download in browser - $filePath = storage_path("app/houses/{$house->photo}"); - if(file_exists($filePath)) - { - return Storage::download($filePath); - } - else { - return response()->json(['error' => 'File not found'], 404); - } + $filePath = storage_path('app/houses/' . $house->photo); + + return response()->download($filePath); + //return Storage::download('houses/' . $house->photo); } } \ No newline at end of file From 1ada712496c960c36015e0f56b7c6bfee4c28dae Mon Sep 17 00:00:00 2001 From: maged-web Date: Sun, 5 May 2024 01:34:07 +0300 Subject: [PATCH 5/8] final --- app/Http/Controllers/HouseController.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index 7f98e98b..bbc09654 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -42,9 +42,8 @@ public function update(Request $request, House $house) public function download(House $house) { // TASK: Return the $house->photo file from "storage/app/houses" folder + // for download in browser $filePath = storage_path('app/houses/' . $house->photo); - return response()->download($filePath); - //return Storage::download('houses/' . $house->photo); } } \ No newline at end of file From b9b5fa66b63cacb9890299ed5da770cdbab23a0f Mon Sep 17 00:00:00 2001 From: maged-web Date: Sun, 5 May 2024 01:43:06 +0300 Subject: [PATCH 6/8] final1 --- app/Http/Controllers/HouseController.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index bbc09654..0f75ee5a 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -43,7 +43,12 @@ public function download(House $house) { // TASK: Return the $house->photo file from "storage/app/houses" folder // for download in browser - $filePath = storage_path('app/houses/' . $house->photo); - return response()->download($filePath); + $filePath = 'houses/' . $house->photo; + + if (Storage::exists($filePath)) { + return Storage::download($filePath); + } else { + abort(404, 'File not found'); + } } } \ No newline at end of file From ac7535b9b082f44b3b5343b06018f6c0148c3ec5 Mon Sep 17 00:00:00 2001 From: maged-web Date: Sun, 5 May 2024 01:50:33 +0300 Subject: [PATCH 7/8] last time --- app/Http/Controllers/HouseController.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index 0f75ee5a..eae2f38a 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -43,12 +43,13 @@ public function download(House $house) { // TASK: Return the $house->photo file from "storage/app/houses" folder // for download in browser - $filePath = 'houses/' . $house->photo; + $filePath = storage_path('app/houses/' . $house->photo); - if (Storage::exists($filePath)) { - return Storage::download($filePath); - } else { - abort(404, 'File not found'); - } + // Check if the file exists + if (!Storage::exists('houses/' . $house->photo)) { + abort(404); + } + + return response()->download($filePath, $house->photo); } } \ No newline at end of file From 990ca228b8e13cd49488941995be47a6b07aad25 Mon Sep 17 00:00:00 2001 From: maged-web Date: Sun, 5 May 2024 01:53:59 +0300 Subject: [PATCH 8/8] error --- app/Http/Controllers/HouseController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index eae2f38a..ef13dbfd 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -43,7 +43,7 @@ public function download(House $house) { // TASK: Return the $house->photo file from "storage/app/houses" folder // for download in browser - $filePath = storage_path('app/houses/' . $house->photo); + $filePath = storage_path('app/houses/'); // Check if the file exists if (!Storage::exists('houses/' . $house->photo)) {