123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- 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 };
- }
- }
- }
|