mirror of
https://github.com/koel/koel
synced 2024-11-28 06:50:27 +00:00
33 lines
750 B
PHP
33 lines
750 B
PHP
|
<?php
|
||
|
|
||
|
namespace App\Services;
|
||
|
|
||
|
use Throwable;
|
||
|
|
||
|
class SimpleLrcReader
|
||
|
{
|
||
|
public function tryReadForMediaFile(string $mediaFilePath): string
|
||
|
{
|
||
|
$lrcFilePath = self::getLrcFilePath($mediaFilePath);
|
||
|
|
||
|
try {
|
||
|
return $lrcFilePath ? trim(file_get_contents($lrcFilePath)) : '';
|
||
|
} catch (Throwable) {
|
||
|
return '';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static function getLrcFilePath(string $mediaFilePath): ?string
|
||
|
{
|
||
|
foreach (['.lrc', '.LRC'] as $extension) {
|
||
|
$lrcFilePath = preg_replace('/\.[^.]+$/', $extension, $mediaFilePath);
|
||
|
|
||
|
if (is_file($lrcFilePath) && is_readable($lrcFilePath)) {
|
||
|
return $lrcFilePath;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
}
|