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