koel/app/Rules/ImageData.php

22 lines
535 B
PHP
Raw 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
{
2024-05-19 05:49:42 +00:00
$passes = attempt(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, ';'));
2022-08-08 16:00:59 +00:00
}, false) ?? false;
2024-05-19 05:49:42 +00:00
if (!$passes) {
$fail('Invalid DataURL string');
}
}
}