koel/app/Rules/SupportedAudioFile.php

31 lines
747 B
PHP
Raw Normal View History

<?php
namespace App\Rules;
2024-05-19 05:49:42 +00:00
use Closure;
use getID3;
2024-05-19 05:49:42 +00:00
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;
use Webmozart\Assert\Assert;
2024-05-19 05:49:42 +00:00
class SupportedAudioFile implements ValidationRule
{
private const SUPPORTED_FORMATS = ['mp3', 'aac', 'ogg', 'flac', 'wav'];
2024-05-19 05:49:42 +00:00
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$passes = rescue(static function () use ($value) {
Assert::oneOf(
Arr::get((new getID3())->analyze($value->getRealPath()), 'fileformat'),
self::SUPPORTED_FORMATS
);
return true;
}) ?? false;
2024-05-19 05:49:42 +00:00
if (!$passes) {
$fail('Unsupported audio file');
}
}
}