diff --git a/Kyoo.Tests/Identifier/IdentifierTests.cs b/Kyoo.Tests/Identifier/IdentifierTests.cs index 170a1352..36ad94b9 100644 --- a/Kyoo.Tests/Identifier/IdentifierTests.cs +++ b/Kyoo.Tests/Identifier/IdentifierTests.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using Kyoo.Controllers; using Kyoo.Models; +using Kyoo.Models.Exceptions; using Kyoo.Models.Options; using Microsoft.Extensions.Options; using Moq; @@ -23,6 +24,10 @@ namespace Kyoo.Tests.Identifier "^\\/?(?.+)?\\/(?.+?)(?: \\((?\\d+)\\))?\\/\\k(?: \\(\\d+\\))? S(?\\d+)E(?\\d+)\\..*$", "^\\/?(?.+)?\\/(?.+?)(?: \\((?\\d+)\\))?\\/\\k(?: \\(\\d+\\))? (?\\d+)\\..*$", "^\\/?(?.+)?\\/(?.+?)(?: \\((?\\d+)\\))?\\/\\k(?: \\(\\d+\\))?\\..*$" + }, + SubtitleRegex = new[] + { + "^(?.+)\\.(?\\w{1,3})\\.(?default\\.)?(?forced\\.)?.*$" } }); @@ -131,5 +136,57 @@ namespace Kyoo.Tests.Identifier Assert.Null(episode.EpisodeNumber); Assert.Null(episode.AbsoluteNumber); } + + [Fact] + public async Task InvalidEpisodeIdentification() + { + _manager.Setup(x => x.GetAll(null, new Sort(), default)).ReturnsAsync(new[] + { + new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}} + }); + await Assert.ThrowsAsync(() => _identifier.Identify("/invalid/path")); + } + + [Fact] + public async Task SubtitleIdentification() + { + _manager.Setup(x => x.GetAll(null, new Sort(), default)).ReturnsAsync(new[] + { + new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}} + }); + Track track = await _identifier.IdentifyTrack("/kyoo/Library/Collection/Show (2000)/Show.eng.default.str"); + Assert.True(track.IsExternal); + Assert.Equal("eng", track.Language); + Assert.Equal("subrip", track.Codec); + Assert.True(track.IsDefault); + Assert.False(track.IsForced); + Assert.StartsWith("/kyoo/Library/Collection/Show (2000)/Show", track.Episode.Path); + } + + [Fact] + public async Task SubtitleIdentificationUnknownCodec() + { + _manager.Setup(x => x.GetAll(null, new Sort(), default)).ReturnsAsync(new[] + { + new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}} + }); + Track track = await _identifier.IdentifyTrack("/kyoo/Library/Collection/Show (2000)/Show.eng.default.extension"); + Assert.True(track.IsExternal); + Assert.Equal("eng", track.Language); + Assert.Equal("extension", track.Codec); + Assert.True(track.IsDefault); + Assert.False(track.IsForced); + Assert.StartsWith("/kyoo/Library/Collection/Show (2000)/Show", track.Episode.Path); + } + + [Fact] + public async Task InvalidSubtitleIdentification() + { + _manager.Setup(x => x.GetAll(null, new Sort(), default)).ReturnsAsync(new[] + { + new Library {Paths = new [] {"/kyoo", "/kyoo/Library/"}} + }); + await Assert.ThrowsAsync(() => _identifier.IdentifyTrack("/invalid/path")); + } } } \ No newline at end of file diff --git a/Kyoo/Controllers/RegexIdentifier.cs b/Kyoo/Controllers/RegexIdentifier.cs index c1a8ab45..c25e3e4f 100644 --- a/Kyoo/Controllers/RegexIdentifier.cs +++ b/Kyoo/Controllers/RegexIdentifier.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; @@ -109,32 +110,32 @@ namespace Kyoo.Controllers } /// - public async Task IdentifyTrack(string path) + public Task IdentifyTrack(string path) { - string relativePath = await _GetRelativePath(path); Match match = _configuration.CurrentValue.SubtitleRegex .Select(x => new Regex(x, RegexOptions.IgnoreCase | RegexOptions.Compiled)) - .Select(x => x.Match(relativePath)) + .Select(x => x.Match(path)) .FirstOrDefault(x => x.Success); if (match == null) throw new IdentificationFailedException($"The subtitle at {path} does not match the subtitle's regex."); string episodePath = match.Groups["Episode"].Value; - return new Track + string extension = Path.GetExtension(path); + return Task.FromResult(new Track { Type = StreamType.Subtitle, Language = match.Groups["Language"].Value, - IsDefault = match.Groups["Default"].Value.Length > 0, - IsForced = match.Groups["Forced"].Value.Length > 0, - Codec = FileExtensions.SubtitleExtensions[Path.GetExtension(path)], + IsDefault = match.Groups["Default"].Success, + IsForced = match.Groups["Forced"].Success, + Codec = FileExtensions.SubtitleExtensions.GetValueOrDefault(extension, extension[1..]), IsExternal = true, Path = path, Episode = new Episode { Path = episodePath } - }; + }); } } } \ No newline at end of file