Skip to content

Commit

Permalink
Merge branch 'main' into fix/guzzle-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro authored Dec 26, 2024
2 parents 9b2fd98 + 9573e05 commit c0b3a72
Show file tree
Hide file tree
Showing 15 changed files with 884 additions and 768 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
use Illuminate\View\View;
use Illuminate\Validation\ValidationException;
use Illuminate\View\View;

final readonly class RegisteredUserController
{
Expand All @@ -31,7 +31,7 @@ public function create(): View
/**
* Handle an incoming registration request.
*
* @throws \Illuminate\Validation\ValidationException
* @throws ValidationException
*/
public function store(Request $request): RedirectResponse
{
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Middleware/EnsureVerifiedEmailsForSignInUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Handle an incoming request.
*
* @param Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
Expand Down
70 changes: 59 additions & 11 deletions app/Livewire/Questions/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ final class Create extends Component
{
use WithFileUploads;

/**
* The disk to store the images.
*/
private const string IMAGE_DISK = 'public';

/**
* Max number of images allowed.
*/
Expand Down Expand Up @@ -279,12 +284,48 @@ public function render(): View
]);
}

/**
* Validate and delete the image if it meets criteria.
*/
public function deleteImageAfterValidation(string $path): void
{
if (! $this->validateImagePath($path)) {
return;
}

$this->deleteImage($path);
}

/**
* Validate if the image path is eligible for deletion.
*/
private function validateImagePath(string $path): bool
{
$images = $this->getSessionImages();

return in_array($path, $images, true) && $this->isValidImageFile($path);
}

/**
* Check if the path exists and is a valid image file.
*/
private function isValidImageFile(string $path): bool
{
if (! Storage::disk(self::IMAGE_DISK)->exists($path)) {
return false;
}

$imageContent = Storage::disk(self::IMAGE_DISK)->get($path) ?: '';

return @getimagesizefromstring($imageContent) !== false;
}

/**
* Optimize the images.
*/
private function optimizeImage(string $path): void
{
$imagePath = Storage::disk('public')->path($path);
$imagePath = Storage::disk(self::IMAGE_DISK)->path($path);
$imagick = new Imagick($imagePath);

if ($imagick->getNumberImages() > 1) {
Expand Down Expand Up @@ -318,7 +359,7 @@ private function deleteImage(string $path): void
return;
}

Storage::disk('public')->delete($path);
Storage::disk(self::IMAGE_DISK)->delete($path);
$this->cleanSession($path);
}

Expand All @@ -331,7 +372,7 @@ private function uploadImages(): void
$today = now()->format('Y-m-d');

/** @var string $path */
$path = $image->store("images/{$today}", 'public');
$path = $image->store("images/{$today}", self::IMAGE_DISK);
$this->optimizeImage($path);

if ($path) {
Expand All @@ -356,10 +397,7 @@ private function uploadImages(): void
*/
private function cleanSession(string $path): void
{
/** @var array<int, string> $images */
$images = session()->get('images', []);

$remainingImages = collect($images)
$remainingImages = collect($this->getSessionImages())
->reject(fn (string $imagePath): bool => $imagePath === $path);

session()->put('images', $remainingImages->toArray());
Expand All @@ -370,13 +408,23 @@ private function cleanSession(string $path): void
*/
private function deleteUnusedImages(): void
{
/** @var array<int, string> $images */
$images = session()->get('images', []);

collect($images)
collect($this->getSessionImages())
->reject(fn (string $path): bool => str_contains($this->content, $path))
->each(fn (string $path): ?bool => $this->deleteImage($path));

session()->forget('images');
}

/**
* Get the session images.
*
* @return array<int, string>
*/
private function getSessionImages(): array
{
/** @var array<int, string> $images */
$images = session()->get('images', []);

return $images;
}
}
2 changes: 1 addition & 1 deletion app/Models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function casts(): array
'answer_updated_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'pinned' => 'bool',
'pinned' => 'boolean',
'is_ignored' => 'boolean',
'views' => 'integer',
];
Expand Down
1 change: 1 addition & 0 deletions app/Services/MetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function ensureCorrectSize(string $value): string
$doc = new DOMDocument();
@$doc->loadHTML($value);
$iframe = $doc->getElementsByTagName('iframe')->item(0);

if ($iframe) {
$iframe->setAttribute('width', (string) self::CARD_WIDTH);
$iframe->setAttribute('height', (string) self::CARD_HEIGHT);
Expand Down
32 changes: 16 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
"require": {
"php": "^8.3",
"ext-imagick": "^3.7.0",
"filament/filament": "^3.2.124",
"intervention/image": "^3.9.1",
"filament/filament": "^3.2.131",
"intervention/image": "^3.10.0",
"laravel/fortify": "^1.21.1",
"laravel/framework": "^11.32.0",
"laravel/pennant": "^1.13.0",
"laravel/pulse": "^1.2.6",
"laravel/socialite": "^5.16.0",
"laravel/framework": "^11.36.1",
"laravel/pennant": "^1.14.0",
"laravel/pulse": "^1.3.2",
"laravel/socialite": "^5.16.1",
"laravel/tinker": "^2.10.0",
"livewire/livewire": "^3.5.12",
"matomo/device-detector": "^6.4.1",
"matomo/device-detector": "^6.4.2",
"nunomaduro/laravel-optimize-database": "^1.0.5",
"panphp/pan": "^0.1.8",
"pinkary-project/type-guard": "^0.1.0",
"scrivo/highlight.php": "^9.18.1.10",
"simplesoftwareio/simple-qrcode": "^4.2",
"spatie/laravel-mailcoach-mailer": "^1.4.0"
"spatie/laravel-mailcoach-mailer": "^1.5.1"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.14.7",
"fakerphp/faker": "^1.24.0",
"larastan/larastan": "^2.9.11",
"laravel/pint": "^1.18.1",
"laravel/sail": "^1.38.0",
"barryvdh/laravel-debugbar": "^3.14.10",
"fakerphp/faker": "^1.24.1",
"larastan/larastan": "^2.9.12",
"laravel/pint": "^1.18.3",
"laravel/sail": "^1.39.1",
"mockery/mockery": "^1.6.12",
"nunomaduro/collision": "^8.5",
"pestphp/pest": "^3.5.1",
"pestphp/pest": "^3.7.1",
"pestphp/pest-plugin-laravel": "^3.0.0",
"pestphp/pest-plugin-type-coverage": "^3.2.0",
"pestphp/pest-plugin-type-coverage": "^3.2.1",
"rector/rector": "^1.2.10"
},
"autoload": {
Expand Down Expand Up @@ -70,7 +70,7 @@
"test:types": "phpstan analyse",
"test:arch": "pest --filter=arch",
"test:type-coverage": "pest --type-coverage --min=100",
"test:unit": "pest --parallel --coverage --min=100",
"test:unit": "pest --parallel --coverage --exactly=99.4",
"test": [
"@test:lint",
"@test:refactor",
Expand Down
Loading

0 comments on commit c0b3a72

Please sign in to comment.