From 94d3a65906b6ffbe177fcf0601d00c461e9313e0 Mon Sep 17 00:00:00 2001 From: butburg Date: Tue, 20 Aug 2024 18:21:46 +0200 Subject: [PATCH] sort imports, add admin check --- src/app/Actions/CreateImageVariants.php | 5 ++--- src/app/Actions/StoreImage.php | 8 +++++++ .../Console/Commands/MigrateLegacyImages.php | 14 ++++--------- src/app/Http/Controllers/PostController.php | 21 +++++-------------- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/app/Actions/CreateImageVariants.php b/src/app/Actions/CreateImageVariants.php index c873ff6..42155ed 100644 --- a/src/app/Actions/CreateImageVariants.php +++ b/src/app/Actions/CreateImageVariants.php @@ -2,11 +2,9 @@ namespace App\Actions; +use App\Enums\ImageSizeType; use App\Models\Image; use App\Models\ImageVariant; -use App\Enums\ImageSizeType; - - class CreateImageVariants { @@ -41,6 +39,7 @@ public function handleVariant(Image $image, $file, array $sizeTypes, string $pat 'height' => $imageData['height'], ]); } + break; } } } diff --git a/src/app/Actions/StoreImage.php b/src/app/Actions/StoreImage.php index 437d662..6b82198 100644 --- a/src/app/Actions/StoreImage.php +++ b/src/app/Actions/StoreImage.php @@ -5,11 +5,19 @@ use Intervention\Image\Laravel\Facades\Image; // Import Image facade use Illuminate\Support\Facades\Storage; use App\Enums\ImageSizeType; +use Illuminate\Http\UploadedFile; +use Symfony\Component\Finder\SplFileInfo; +use InvalidArgumentException; class StoreImage { public function handleStore($file, ImageSizeType $sizeType, string $filePath): array { + // Check if the file is an instance of either UploadedFile or SplFileInfo + if (!($file instanceof UploadedFile || $file instanceof SplFileInfo)) { + throw new InvalidArgumentException('The $file parameter must be an instance of UploadedFile or SplFileInfo.'); + } + // Load the image $image = Image::read($file); diff --git a/src/app/Console/Commands/MigrateLegacyImages.php b/src/app/Console/Commands/MigrateLegacyImages.php index 7c0678d..adc93fb 100644 --- a/src/app/Console/Commands/MigrateLegacyImages.php +++ b/src/app/Console/Commands/MigrateLegacyImages.php @@ -2,20 +2,14 @@ namespace App\Console\Commands; -use Illuminate\Console\Command; -use App\Models\Post; -use App\Models\Image; use App\Actions\CreateImageVariants; use App\Enums\ImageSizeType; +use App\Models\Image; +use App\Models\Post; +use Illuminate\Console\Command; use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Storage; -use Illuminate\Support\Facades\DB; -use Symfony\Component\HttpFoundation\File\UploadedFile; - - - class MigrateLegacyImages extends Command { /** @@ -37,7 +31,7 @@ class MigrateLegacyImages extends Command */ public function handle() { - $legacyImagePath = '/var/www/html/storage/app/public/files/posts/images_legacy'; + $legacyImagePath = storage_path('app/public/files/posts/images_legacy'); // Check if the directory exists if (!File::exists($legacyImagePath)) { diff --git a/src/app/Http/Controllers/PostController.php b/src/app/Http/Controllers/PostController.php index 9aace5c..da646ff 100644 --- a/src/app/Http/Controllers/PostController.php +++ b/src/app/Http/Controllers/PostController.php @@ -2,31 +2,20 @@ namespace App\Http\Controllers; -use App\Actions\StoreImage; use App\Actions\CreateImageVariants; use App\Enums\ImageSizeType; - use App\Http\Requests\Post\StoreRequest; // Importing the StoreRequest form request use App\Http\Requests\Post\UpdateRequest; // Importing the UpdateRequest form request -use Illuminate\Support\Facades\Storage; - -use Illuminate\Http\Request; -use Illuminate\Http\RedirectResponse; - -use App\Models\Post; // Importing the Post model use App\Models\Image; -use App\Models\ImageVariant; - +use App\Models\Post; // Importing the Post model use Illuminate\Contracts\View\View; - -use Illuminate\Support\Facades\File; +use Illuminate\Http\RedirectResponse; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Storage; class PostController extends Controller { - protected const IMAGE_DIRECTORY = 'files/posts/images/'; - protected const IMAGE_QUALITY = 90; - protected array $thumbnailSizes = ['1400', '640']; /** * Display a listing of the resource. @@ -257,7 +246,7 @@ public function makedraft(string $id): RedirectResponse private function userIsOwner($user_id_from_post): void { - if ($user_id_from_post !== Auth::id()) { + if ($user_id_from_post !== Auth::id() and Auth::user()->usertype !== 'admin') { abort(403, 'Unauthorized action.'); } }