2022-09-20 13:34:46 -04:00
|
|
|
using System;
|
2022-09-15 19:45:26 -04:00
|
|
|
using System.IO;
|
2022-09-20 08:36:43 -04:00
|
|
|
using Jellyfin.Extensions;
|
2022-09-15 19:45:26 -04:00
|
|
|
|
2022-09-17 17:37:38 -04:00
|
|
|
namespace MediaBrowser.Controller.Lyrics;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Lyric helper methods.
|
|
|
|
/// </summary>
|
|
|
|
public static class LyricInfo
|
2022-09-15 19:45:26 -04:00
|
|
|
{
|
|
|
|
/// <summary>
|
2022-09-17 17:37:38 -04:00
|
|
|
/// Gets matching lyric file for a requested item.
|
2022-09-15 19:45:26 -04:00
|
|
|
/// </summary>
|
2022-09-17 17:37:38 -04:00
|
|
|
/// <param name="lyricProvider">The lyricProvider interface to use.</param>
|
|
|
|
/// <param name="itemPath">Path of requested item.</param>
|
|
|
|
/// <returns>Lyric file path if passed lyric provider's supported media type is found; otherwise, null.</returns>
|
2022-09-20 08:36:43 -04:00
|
|
|
public static string? GetLyricFilePath(this ILyricProvider lyricProvider, string itemPath)
|
2022-09-15 19:45:26 -04:00
|
|
|
{
|
2022-09-20 08:48:08 -04:00
|
|
|
// Ensure we have a provider
|
2022-09-20 08:36:43 -04:00
|
|
|
if (lyricProvider is null)
|
2022-09-15 19:45:26 -04:00
|
|
|
{
|
2022-09-20 08:36:43 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-20 08:48:08 -04:00
|
|
|
// Ensure the path to the item is not null
|
|
|
|
string? itemDirectoryPath = Path.GetDirectoryName(itemPath);
|
|
|
|
if (itemDirectoryPath is null)
|
2022-09-20 08:36:43 -04:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-20 08:48:08 -04:00
|
|
|
// Ensure the directory path exists
|
|
|
|
if (!Directory.Exists(itemDirectoryPath))
|
2022-09-20 08:36:43 -04:00
|
|
|
{
|
2022-09-20 08:48:08 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var lyricFilePath in Directory.GetFiles(itemDirectoryPath, $"{Path.GetFileNameWithoutExtension(itemPath)}.*"))
|
|
|
|
{
|
2022-09-26 16:24:21 -04:00
|
|
|
if (lyricProvider.SupportedMediaTypes.Contains(Path.GetExtension(lyricFilePath.AsSpan())[1..], StringComparison.OrdinalIgnoreCase))
|
2022-09-15 19:45:26 -04:00
|
|
|
{
|
2022-09-17 17:37:38 -04:00
|
|
|
return lyricFilePath;
|
2022-09-15 19:45:26 -04:00
|
|
|
}
|
|
|
|
}
|
2022-09-17 17:37:38 -04:00
|
|
|
|
|
|
|
return null;
|
2022-09-15 19:45:26 -04:00
|
|
|
}
|
|
|
|
}
|