Skip to content

Commit

Permalink
sort imports, add admin check
Browse files Browse the repository at this point in the history
  • Loading branch information
butburg committed Aug 20, 2024
1 parent 050576a commit 94d3a65
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 29 deletions.
5 changes: 2 additions & 3 deletions src/app/Actions/CreateImageVariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -41,6 +39,7 @@ public function handleVariant(Image $image, $file, array $sizeTypes, string $pat
'height' => $imageData['height'],
]);
}
break;
}
}
}
8 changes: 8 additions & 0 deletions src/app/Actions/StoreImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 4 additions & 10 deletions src/app/Console/Commands/MigrateLegacyImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand All @@ -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)) {
Expand Down
21 changes: 5 additions & 16 deletions src/app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.');
}
}
Expand Down

0 comments on commit 94d3a65

Please sign in to comment.