koel/app/Rules/ImageData.php

22 lines
527 B
PHP
Raw Permalink Normal View History

<?php
namespace App\Rules;
2024-05-19 05:49:42 +00:00
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
2024-01-10 00:47:09 +00:00
use Illuminate\Support\Str;
2024-05-19 05:49:42 +00:00
class ImageData implements ValidationRule
{
2024-05-19 05:49:42 +00:00
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$passes = rescue(static function () use ($value) {
2024-01-10 00:47:09 +00:00
return (bool) preg_match('/data:image\/(jpe?g|png|webp|gif)/i', Str::before($value, ';'));
}) ?? false;
2024-05-19 05:49:42 +00:00
if (!$passes) {
$fail('Invalid DataURL string');
}
}
}