koel/app/Rules/SupportedAudioFile.php

31 lines
755 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
{
2024-05-19 05:49:42 +00:00
$passes = attempt(static function () use ($value) {
Assert::oneOf(
Arr::get((new getID3())->analyze($value->getRealPath()), 'fileformat'),
self::SUPPORTED_FORMATS
);
return true;
2022-08-08 16:00:59 +00:00
}, false) ?? false;
2024-05-19 05:49:42 +00:00
if (!$passes) {
$fail('Unsupported audio file');
}
}
}