From da7d9c00183209729351f9a821c65027e75a59ec Mon Sep 17 00:00:00 2001 From: hmdee Date: Thu, 28 Nov 2024 11:08:35 +0200 Subject: [PATCH 1/4] test9 --- app/Http/Controllers/CompanyController.php | 3 +-- app/Http/Controllers/HouseController.php | 14 ++++++++++++++ app/Http/Controllers/OfficeController.php | 14 ++++++++++---- app/Http/Controllers/ProjectController.php | 3 ++- app/Http/Controllers/ShopController.php | 15 ++++++++++++--- 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 12fcb81d..c92ae0aa 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -20,9 +20,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->getFirstMediaUrl('photos'); return view('companies.show', compact('company', 'photo')); } - } diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index c330f8aa..b7bb9af2 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -25,6 +25,10 @@ public function update(Request $request, House $house) $filename = $request->file('photo')->store('houses'); // TASK: Delete the old file from the storage + if ($house->photo) { + // حذف الملف القديم من التخزين + Storage::delete('houses/' . $house->photo); + } $house->update([ 'name' => $request->name, @@ -38,5 +42,15 @@ public function download(House $house) { // TASK: Return the $house->photo file from "storage/app/houses" folder // for download in browser + $filePath = 'houses/' . $house->photo; + + // التحقق إذا كان الملف موجودًا + if (Storage::exists($filePath)) { + // إرجاع الملف ليتم تحميله عبر المتصفح + return Storage::download($filePath); + } + + // إذا لم يكن الملف موجودًا، إرجاع خطأ 404 + return abort(404, 'File not found'); } } diff --git a/app/Http/Controllers/OfficeController.php b/app/Http/Controllers/OfficeController.php index fae443fa..2abaf6c5 100644 --- a/app/Http/Controllers/OfficeController.php +++ b/app/Http/Controllers/OfficeController.php @@ -9,14 +9,21 @@ class OfficeController extends Controller { public function store(Request $request) { + // التحقق من وجود الملف في الطلب + $request->validate([ + 'photo' => 'required|file|max:1024', // التحقق من أن الملف موجود ولا يتجاوز 1 ميجابايت + ]); + + // الحصول على اسم الملف الأصلي $filename = $request->file('photo')->getClientOriginalName(); - // TASK: Upload the file "photo" so it would be written as - // storage/app/public/offices/[original_filename] + // رفع الملف إلى المجلد 'public/offices' وتخزينه مع الاسم الأصلي + $path = $request->file('photo')->storeAs('public/offices', $filename); + // إنشاء السجل الجديد في قاعدة البيانات Office::create([ 'name' => $request->name, - 'photo' => $filename, + 'photo' => $filename, // حفظ اسم الملف في قاعدة البيانات ]); return 'Success'; @@ -26,5 +33,4 @@ public function show(Office $office) { return view('offices.show', compact('office')); } - } diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 95aed4f8..65778dd8 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -11,11 +11,12 @@ 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..1c2169fc 100644 --- a/app/Http/Controllers/ShopController.php +++ b/app/Http/Controllers/ShopController.php @@ -9,12 +9,21 @@ class ShopController extends Controller { public function store(Request $request) { + // 1. الحصول على اسم الملف الأصلي $filename = $request->file('photo')->getClientOriginalName(); + + // 2. رفع الصورة إلى المجلد 'shops' $request->file('photo')->storeAs('shops', $filename); - // 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 + // 3. تغيير حجم الصورة باستخدام Intervention Image + $image = Image::make(storage_path('app/shops/' . $filename)); // تحميل الصورة من المسار + + // 4. تغيير الحجم إلى 500x500 بيكسل + $image->resize(500, 500); + + // 5. حفظ الصورة المعدلة باسم جديد (resized-{$filename}) + $resizedFilename = 'resized-' . $filename; + $image->save(storage_path('app/shops/' . $resizedFilename)); // حفظ الصورة المعدلة في المجلد نفسه return 'Success'; } From 71cd794a5a2147115c68c6afc66c6170bf657f5d Mon Sep 17 00:00:00 2001 From: hmdee Date: Thu, 28 Nov 2024 12:18:12 +0200 Subject: [PATCH 2/4] =?UTF-8?q?=D8=A7=D9=84=D8=A7=D8=AE=D8=AA=D8=A8=D8=A7?= =?UTF-8?q?=D8=B19?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/CompanyController.php | 3 ++- app/Http/Controllers/HouseController.php | 8 ++++---- app/Http/Controllers/ProjectController.php | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index c92ae0aa..c447759c 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -20,7 +20,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 = $company->getFirstMediaUrl('photos'); + + $photo = $company->getMedia('companies')[0]->getFullUrl(); return view('companies.show', compact('company', 'photo')); } diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index b7bb9af2..c1686625 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -25,10 +25,10 @@ public function update(Request $request, House $house) $filename = $request->file('photo')->store('houses'); // TASK: Delete the old file from the storage - if ($house->photo) { - // حذف الملف القديم من التخزين - Storage::delete('houses/' . $house->photo); - } + + // حذف الملف القديم من التخزين + + Storage::delete($house->photo); $house->update([ 'name' => $request->name, diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 65778dd8..7fab32d3 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -11,7 +11,7 @@ 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', + 'logo' => [File::image()->max(1024)] ]); // TASK: change the below line so that $filename would contain only filename From d4ccdeab0f31c4379f25398edf28a50a54c80fcc Mon Sep 17 00:00:00 2001 From: hmdee Date: Thu, 28 Nov 2024 12:22:32 +0200 Subject: [PATCH 3/4] ddddd --- app/Http/Controllers/ProjectController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 7fab32d3..84d13992 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 { From b586ea6eaf280b7c3a5eb547f675d39d23ad2bfd Mon Sep 17 00:00:00 2001 From: hmdee Date: Thu, 28 Nov 2024 12:26:37 +0200 Subject: [PATCH 4/4] ccc --- app/Http/Controllers/ProjectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 84d13992..4724e6e0 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -17,7 +17,7 @@ public function store(Request $request) // TASK: change the below line so that $filename would contain only filename // The same filename as the original uploaded file - $filename = $request->file('logo')->getClientOriginalName(); + $filename = $request->logo->getClientOriginalName(); $request->file('logo')->storeAs('logos', $filename); Project::create([