diff options
6 files changed, 148 insertions, 124 deletions
diff --git a/Directory.Packages.props b/Directory.Packages.props index a153e1d5f..a52bcb53e 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -22,7 +22,7 @@ <PackageVersion Include="IDisposableAnalyzers" Version="4.0.8" /> <PackageVersion Include="Jellyfin.XmlTv" Version="10.8.0" /> <PackageVersion Include="libse" Version="4.0.7" /> - <PackageVersion Include="LrcParser" Version="2023.524.0" /> + <PackageVersion Include="LrcParser" Version="2024.0728.2" /> <PackageVersion Include="MetaBrainz.MusicBrainz" Version="6.1.0" /> <PackageVersion Include="Microsoft.AspNetCore.Authorization" Version="8.0.8" /> <PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.8" /> diff --git a/MediaBrowser.Providers/Lyric/LrcLyricParser.cs b/MediaBrowser.Providers/Lyric/LrcLyricParser.cs index 67b26e457..fffdf4887 100644 --- a/MediaBrowser.Providers/Lyric/LrcLyricParser.cs +++ b/MediaBrowser.Providers/Lyric/LrcLyricParser.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.IO; using System.Linq; using Jellyfin.Extensions; @@ -20,7 +19,6 @@ public class LrcLyricParser : ILyricParser private readonly LyricParser _lrcLyricParser; private static readonly string[] _supportedMediaTypes = [".lrc", ".elrc"]; - private static readonly string[] _acceptedTimeFormats = ["HH:mm:ss", "H:mm:ss", "mm:ss", "m:ss"]; /// <summary> /// Initializes a new instance of the <see cref="LrcLyricParser"/> class. @@ -59,37 +57,7 @@ public class LrcLyricParser : ILyricParser return null; } - List<LrcParser.Model.Lyric> sortedLyricData = lyricData.Lyrics.Where(x => x.TimeTags.Count > 0).OrderBy(x => x.TimeTags.First().Value).ToList(); - - // Parse metadata rows - var metaDataRows = lyricData.Lyrics - .Where(x => x.TimeTags.Count == 0) - .Where(x => x.Text.StartsWith('[') && x.Text.EndsWith(']')) - .Select(x => x.Text) - .ToList(); - - var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); - foreach (string metaDataRow in metaDataRows) - { - var index = metaDataRow.IndexOf(':', StringComparison.OrdinalIgnoreCase); - if (index == -1) - { - continue; - } - - // Remove square bracket before field name, and after field value - // Example 1: [au: 1hitsong] - // Example 2: [ar: Calabrese] - var metaDataFieldName = GetMetadataFieldName(metaDataRow, index); - var metaDataFieldValue = GetMetadataValue(metaDataRow, index); - - if (string.IsNullOrEmpty(metaDataFieldName) || string.IsNullOrEmpty(metaDataFieldValue)) - { - continue; - } - - fileMetaData[metaDataFieldName] = metaDataFieldValue; - } + List<LrcParser.Model.Lyric> sortedLyricData = lyricData.Lyrics.OrderBy(x => x.StartTime).ToList(); if (sortedLyricData.Count == 0) { @@ -100,99 +68,10 @@ public class LrcLyricParser : ILyricParser for (int i = 0; i < sortedLyricData.Count; i++) { - var timeData = sortedLyricData[i].TimeTags.First().Value; - if (timeData is null) - { - continue; - } - - long ticks = TimeSpan.FromMilliseconds(timeData.Value).Ticks; + long ticks = TimeSpan.FromMilliseconds(sortedLyricData[i].StartTime).Ticks; lyricList.Add(new LyricLine(sortedLyricData[i].Text.Trim(), ticks)); } - if (fileMetaData.Count != 0) - { - // Map metaData values from LRC file to LyricMetadata properties - LyricMetadata lyricMetadata = MapMetadataValues(fileMetaData); - - return new LyricDto { Metadata = lyricMetadata, Lyrics = lyricList }; - } - return new LyricDto { Lyrics = lyricList }; } - - /// <summary> - /// Converts metadata from an LRC file to LyricMetadata properties. - /// </summary> - /// <param name="metaData">The metadata from the LRC file.</param> - /// <returns>A lyricMetadata object with mapped property data.</returns> - private static LyricMetadata MapMetadataValues(Dictionary<string, string> metaData) - { - LyricMetadata lyricMetadata = new(); - - if (metaData.TryGetValue("ar", out var artist) && !string.IsNullOrEmpty(artist)) - { - lyricMetadata.Artist = artist; - } - - if (metaData.TryGetValue("al", out var album) && !string.IsNullOrEmpty(album)) - { - lyricMetadata.Album = album; - } - - if (metaData.TryGetValue("ti", out var title) && !string.IsNullOrEmpty(title)) - { - lyricMetadata.Title = title; - } - - if (metaData.TryGetValue("au", out var author) && !string.IsNullOrEmpty(author)) - { - lyricMetadata.Author = author; - } - - if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length)) - { - if (DateTime.TryParseExact(length, _acceptedTimeFormats, null, DateTimeStyles.None, out var value)) - { - lyricMetadata.Length = value.TimeOfDay.Ticks; - } - } - - if (metaData.TryGetValue("by", out var by) && !string.IsNullOrEmpty(by)) - { - lyricMetadata.By = by; - } - - if (metaData.TryGetValue("offset", out var offset) && !string.IsNullOrEmpty(offset)) - { - if (int.TryParse(offset, out var value)) - { - lyricMetadata.Offset = TimeSpan.FromMilliseconds(value).Ticks; - } - } - - if (metaData.TryGetValue("re", out var creator) && !string.IsNullOrEmpty(creator)) - { - lyricMetadata.Creator = creator; - } - - if (metaData.TryGetValue("ve", out var version) && !string.IsNullOrEmpty(version)) - { - lyricMetadata.Version = version; - } - - return lyricMetadata; - } - - private static string GetMetadataFieldName(string metaDataRow, int index) - { - var metadataFieldName = metaDataRow.AsSpan(1, index - 1).Trim(); - return metadataFieldName.IsEmpty ? string.Empty : metadataFieldName.ToString(); - } - - private static string GetMetadataValue(string metaDataRow, int index) - { - var metadataValue = metaDataRow.AsSpan(index + 1, metaDataRow.Length - index - 2).Trim(); - return metadataValue.IsEmpty ? string.Empty : metadataValue.ToString(); - } } diff --git a/tests/Jellyfin.Server.Integration.Tests/Controllers/LiveTvControllerTests.cs b/tests/Jellyfin.Server.Integration.Tests/Controllers/LiveTvControllerTests.cs new file mode 100644 index 000000000..dd971fa87 --- /dev/null +++ b/tests/Jellyfin.Server.Integration.Tests/Controllers/LiveTvControllerTests.cs @@ -0,0 +1,96 @@ +using System.Net; +using System.Net.Http.Json; +using System.Net.Mime; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using Jellyfin.Extensions.Json; +using MediaBrowser.Model.LiveTv; +using Xunit; + +namespace Jellyfin.Server.Integration.Tests.Controllers; + +public sealed class LiveTvControllerTests : IClassFixture<JellyfinApplicationFactory> +{ + private readonly JellyfinApplicationFactory _factory; + private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; + private static string? _accessToken; + + public LiveTvControllerTests(JellyfinApplicationFactory factory) + { + _factory = factory; + } + + [Fact] + public async Task AddTunerHost_Unauthorized_ReturnsUnauthorized() + { + var client = _factory.CreateClient(); + + var body = new TunerHostInfo() + { + Type = "m3u", + Url = "Test Data/dummy.m3u8" + }; + + var response = await client.PostAsJsonAsync("/LiveTv/TunerHosts", body, _jsonOptions); + + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + } + + [Fact] + public async Task AddTunerHost_Valid_ReturnsCorrectResponse() + { + var client = _factory.CreateClient(); + client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client)); + + var body = new TunerHostInfo() + { + Type = "m3u", + Url = "Test Data/dummy.m3u8" + }; + + var response = await client.PostAsJsonAsync("/LiveTv/TunerHosts", body, _jsonOptions); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(MediaTypeNames.Application.Json, response.Content.Headers.ContentType?.MediaType); + Assert.Equal(Encoding.UTF8.BodyName, response.Content.Headers.ContentType?.CharSet); + var responseBody = await response.Content.ReadFromJsonAsync<TunerHostInfo>(); + Assert.NotNull(responseBody); + Assert.Equal(body.Type, responseBody.Type); + Assert.Equal(body.Url, responseBody.Url); + } + + [Fact] + public async Task AddTunerHost_InvalidType_ReturnsNotFound() + { + var client = _factory.CreateClient(); + client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client)); + + var body = new TunerHostInfo() + { + Type = "invalid", + Url = "Test Data/dummy.m3u8" + }; + + var response = await client.PostAsJsonAsync("/LiveTv/TunerHosts", body, _jsonOptions); + + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + } + + [Fact] + public async Task AddTunerHost_InvalidUrl_ReturnsNotFound() + { + var client = _factory.CreateClient(); + client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client)); + + var body = new TunerHostInfo() + { + Type = "m3u", + Url = "thisgoesnowhere" + }; + + var response = await client.PostAsJsonAsync("/LiveTv/TunerHosts", body, _jsonOptions); + + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + } +} diff --git a/tests/Jellyfin.Server.Integration.Tests/Controllers/PluginsControllerTests.cs b/tests/Jellyfin.Server.Integration.Tests/Controllers/PluginsControllerTests.cs new file mode 100644 index 000000000..547bfcc0f --- /dev/null +++ b/tests/Jellyfin.Server.Integration.Tests/Controllers/PluginsControllerTests.cs @@ -0,0 +1,45 @@ +using System.Net; +using System.Net.Http.Json; +using System.Net.Mime; +using System.Text; +using System.Threading.Tasks; +using Jellyfin.Extensions.Json; +using MediaBrowser.Model.Plugins; +using Xunit; + +namespace Jellyfin.Server.Integration.Tests.Controllers; + +public sealed class PluginsControllerTests : IClassFixture<JellyfinApplicationFactory> +{ + private readonly JellyfinApplicationFactory _factory; + private static string? _accessToken; + + public PluginsControllerTests(JellyfinApplicationFactory factory) + { + _factory = factory; + } + + [Fact] + public async Task GetPlugins_Unauthorized_ReturnsUnauthorized() + { + var client = _factory.CreateClient(); + + var response = await client.GetAsync("/Plugins"); + + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + } + + [Fact] + public async Task GetPlugins_Authorized_ReturnsCorrectResponse() + { + var client = _factory.CreateClient(); + client.DefaultRequestHeaders.AddAuthHeader(_accessToken ??= await AuthHelper.CompleteStartupAsync(client)); + + var response = await client.GetAsync("/Plugins"); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(MediaTypeNames.Application.Json, response.Content.Headers.ContentType?.MediaType); + Assert.Equal(Encoding.UTF8.BodyName, response.Content.Headers.ContentType?.CharSet); + _ = await response.Content.ReadFromJsonAsync<PluginInfo[]>(JsonDefaults.Options); + } +} diff --git a/tests/Jellyfin.Server.Integration.Tests/Jellyfin.Server.Integration.Tests.csproj b/tests/Jellyfin.Server.Integration.Tests/Jellyfin.Server.Integration.Tests.csproj index a5296d8c9..8228c0df7 100644 --- a/tests/Jellyfin.Server.Integration.Tests/Jellyfin.Server.Integration.Tests.csproj +++ b/tests/Jellyfin.Server.Integration.Tests/Jellyfin.Server.Integration.Tests.csproj @@ -18,6 +18,9 @@ </ItemGroup> <ItemGroup> + <None Include="Test Data\**\*.*"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> <!-- Don't run tests in parallel --> <None Update="xunit.runner.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> diff --git a/tests/Jellyfin.Server.Integration.Tests/Test Data/dummy.m3u8 b/tests/Jellyfin.Server.Integration.Tests/Test Data/dummy.m3u8 new file mode 100644 index 000000000..7f60f38a6 --- /dev/null +++ b/tests/Jellyfin.Server.Integration.Tests/Test Data/dummy.m3u8 @@ -0,0 +1 @@ +C:\Music |
