jellyfin-server/MediaBrowser.Controller/Lyrics/LyricInfo.cs

50 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.IO;
using Jellyfin.Extensions;
2022-09-17 21:37:38 +00:00
namespace MediaBrowser.Controller.Lyrics;
/// <summary>
/// Lyric helper methods.
/// </summary>
public static class LyricInfo
{
/// <summary>
2022-09-17 21:37:38 +00:00
/// Gets matching lyric file for a requested item.
/// </summary>
2022-09-17 21:37:38 +00: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>
public static string? GetLyricFilePath(this ILyricProvider lyricProvider, string itemPath)
{
2022-09-20 12:48:08 +00:00
// Ensure we have a provider
if (lyricProvider is null)
{
return null;
}
2022-09-20 12:48:08 +00:00
// Ensure the path to the item is not null
string? itemDirectoryPath = Path.GetDirectoryName(itemPath);
if (itemDirectoryPath is null)
{
return null;
}
2022-09-20 12:48:08 +00:00
// Ensure the directory path exists
if (!Directory.Exists(itemDirectoryPath))
{
2022-09-20 12:48:08 +00:00
return null;
}
foreach (var lyricFilePath in Directory.GetFiles(itemDirectoryPath, $"{Path.GetFileNameWithoutExtension(itemPath)}.*"))
{
if (lyricProvider.SupportedMediaTypes.Contains(Path.GetExtension(lyricFilePath.AsSpan())[1..], StringComparison.OrdinalIgnoreCase))
{
2022-09-17 21:37:38 +00:00
return lyricFilePath;
}
}
2022-09-17 21:37:38 +00:00
return null;
}
}