jellyfin/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs

82 lines
2.5 KiB
C#
Raw Normal View History

2022-09-10 20:58:03 +02:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;
namespace Jellyfin.Api.Models.UserDtos
{
/// <summary>
/// TXT File Lyric Provider.
/// </summary>
public class TxtLyricsProvider : ILyricsProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="TxtLyricsProvider"/> class.
/// </summary>
public TxtLyricsProvider()
{
FileExtensions = new Collection<string>
{
"lrc", "txt"
};
}
/// <summary>
/// Gets a value indicating the File Extenstions this provider works with.
/// </summary>
public Collection<string>? FileExtensions { get; }
/// <summary>
/// Gets or Sets a value indicating whether Process() generated data.
/// </summary>
/// <returns><c>true</c> if data generated; otherwise, <c>false</c>.</returns>
public bool HasData { get; set; }
/// <summary>
/// Gets or Sets Data object generated by Process() method.
/// </summary>
/// <returns><c>Object</c> with data if no error occured; otherwise, <c>null</c>.</returns>
public object? Data { get; set; }
/// <summary>
/// Opens lyric file for [the specified item], and processes it for API return.
/// </summary>
/// <param name="item">The item to to process.</param>
public void Process(BaseItem item)
{
string? lyricFilePath = Helpers.ItemHelper.GetLyricFilePath(item.Path);
if (string.IsNullOrEmpty(lyricFilePath))
{
return;
}
List<Lyric> lyricsList = new List<Lyric>();
string lyricData = System.IO.File.ReadAllText(lyricFilePath);
// Splitting on Environment.NewLine caused some new lines to be missed in Windows.
char[] newLinedelims = new[] { '\r', '\n' };
string[] lyricTextLines = lyricData.Split(newLinedelims, StringSplitOptions.RemoveEmptyEntries);
if (!lyricTextLines.Any())
{
return;
}
foreach (string lyricLine in lyricTextLines)
{
lyricsList.Add(new Lyric { Text = lyricLine });
}
this.HasData = true;
this.Data = new { lyrics = lyricsList };
}
}
}