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
{
///
/// TXT File Lyric Provider.
///
public class TxtLyricsProvider : ILyricsProvider
{
///
/// Initializes a new instance of the class.
///
public TxtLyricsProvider()
{
FileExtensions = new Collection
{
"lrc", "txt"
};
}
///
/// Gets a value indicating the File Extenstions this provider works with.
///
public Collection? FileExtensions { get; }
///
/// Gets or Sets a value indicating whether Process() generated data.
///
/// true if data generated; otherwise, false.
public bool HasData { get; set; }
///
/// Gets or Sets Data object generated by Process() method.
///
/// Object with data if no error occured; otherwise, null.
public object? Data { get; set; }
///
/// Opens lyric file for [the specified item], and processes it for API return.
///
/// The item to to process.
public void Process(BaseItem item)
{
string? lyricFilePath = Helpers.ItemHelper.GetLyricFilePath(item.Path);
if (string.IsNullOrEmpty(lyricFilePath))
{
return;
}
List lyricsList = new List();
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 };
}
}
}