jellyfin/Jellyfin.Api/Helpers/ItemHelper.cs

121 lines
4 KiB
C#
Raw Normal View History

2022-09-10 02:14:23 +02:00
using System;
using System.Collections.Generic;
2022-09-10 20:29:30 +02:00
using System.Dynamic;
using System.Globalization;
2022-09-10 02:14:23 +02:00
using System.IO;
2022-09-10 20:29:30 +02:00
using System.Linq;
2022-09-10 02:14:23 +02:00
using Jellyfin.Api.Models.UserDtos;
2022-09-10 20:29:30 +02:00
using LrcParser.Model;
using LrcParser.Parser;
2022-09-10 02:14:23 +02:00
using MediaBrowser.Controller.Entities;
namespace Jellyfin.Api.Helpers
{
/// <summary>
/// Item helper.
/// </summary>
public static class ItemHelper
{
/// <summary>
/// Opens lyrics file, converts to a List of Lyrics, and returns it.
/// </summary>
/// <param name="item">Requested Item.</param>
/// <returns>Collection of Lyrics.</returns>
2022-09-10 20:29:30 +02:00
internal static object? GetLyricData(BaseItem item)
2022-09-10 02:14:23 +02:00
{
2022-09-10 20:58:03 +02:00
List<ILyricsProvider> providerList = new List<ILyricsProvider>();
2022-09-10 02:14:23 +02:00
2022-09-10 20:58:03 +02:00
// Find all classes that implement ILyricsProvider Interface
var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface);
2022-09-10 02:14:23 +02:00
2022-09-10 20:58:03 +02:00
if (!foundLyricProviders.Any())
2022-09-10 02:14:23 +02:00
{
2022-09-10 20:58:03 +02:00
return null;
}
2022-09-10 02:14:23 +02:00
2022-09-10 20:58:03 +02:00
foreach (var provider in foundLyricProviders)
{
providerList.Add((ILyricsProvider)Activator.CreateInstance(provider));
}
2022-09-10 02:14:23 +02:00
2022-09-10 20:58:03 +02:00
foreach (ILyricsProvider provider in providerList)
{
provider.Process(item);
if (provider.HasData)
2022-09-10 02:14:23 +02:00
{
2022-09-10 20:58:03 +02:00
return provider.Data;
2022-09-10 02:14:23 +02:00
}
}
2022-09-10 20:58:03 +02:00
return null;
}
2022-09-10 20:29:30 +02:00
2022-09-10 20:58:03 +02:00
/// <summary>
/// Checks if requested item has a matching lyric file.
/// </summary>
/// <param name="itemPath">Path of requested item.</param>
/// <returns>True if item has a matching lyrics file.</returns>
public static string? GetLyricFilePath(string itemPath)
{
List<string> supportedLyricFileExtensions = new List<string>();
2022-09-10 20:29:30 +02:00
2022-09-10 20:58:03 +02:00
// Find all classes that implement ILyricsProvider Interface
var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface);
2022-09-10 20:29:30 +02:00
2022-09-10 20:58:03 +02:00
if (!foundLyricProviders.Any())
2022-09-10 02:14:23 +02:00
{
2022-09-10 20:29:30 +02:00
return null;
2022-09-10 02:14:23 +02:00
}
2022-09-10 20:58:03 +02:00
// Iterate over all found lyric providers
foreach (var provider in foundLyricProviders)
2022-09-10 02:14:23 +02:00
{
2022-09-10 20:58:03 +02:00
var foundProvider = (ILyricsProvider)Activator.CreateInstance(provider);
if (foundProvider?.FileExtensions is null)
{
continue;
}
if (foundProvider.FileExtensions.Any())
{
// Gather distinct list of handled file extentions
foreach (string lyricFileExtension in foundProvider.FileExtensions)
{
if (!supportedLyricFileExtensions.Contains(lyricFileExtension))
{
supportedLyricFileExtensions.Add(lyricFileExtension);
}
}
}
2022-09-10 02:14:23 +02:00
}
2022-09-10 20:58:03 +02:00
foreach (string lyricFileExtension in supportedLyricFileExtensions)
2022-09-10 02:14:23 +02:00
{
2022-09-10 20:58:03 +02:00
string lyricFilePath = @Path.ChangeExtension(itemPath, lyricFileExtension);
if (System.IO.File.Exists(lyricFilePath))
2022-09-10 20:29:30 +02:00
{
2022-09-10 20:58:03 +02:00
return lyricFilePath;
2022-09-10 20:29:30 +02:00
}
2022-09-10 02:14:23 +02:00
}
2022-09-10 20:58:03 +02:00
return null;
}
/// <summary>
/// Checks if requested item has a matching local lyric file.
/// </summary>
/// <param name="itemPath">Path of requested item.</param>
/// <returns>True if item has a matching lyrics file; otherwise false.</returns>
public static bool HasLyricFile(string itemPath)
{
string? lyricFilePath = GetLyricFilePath(itemPath);
return !string.IsNullOrEmpty(lyricFilePath);
2022-09-10 02:14:23 +02:00
}
}
}