koel/app/Rules/SupportedAudioFile.php

33 lines
773 B
PHP
Raw Normal View History

<?php
namespace App\Rules;
use getID3;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Webmozart\Assert\Assert;
class SupportedAudioFile implements Rule
{
private const SUPPORTED_FORMATS = ['mp3', 'aac', 'ogg', 'flac', 'wav'];
/** @param UploadedFile $value */
public function passes($attribute, $value): bool
{
2022-08-08 16:00:59 +00:00
return 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;
}
public function message(): string
{
return 'Unsupported audio file';
}
}