2019-12-13 12:11:37 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2018-09-12 10:26:21 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.RegularExpressions;
|
2019-01-13 12:17:29 -07:00
|
|
|
using Emby.Naming.Common;
|
2018-09-12 10:26:21 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
|
|
|
namespace Emby.Naming.Video
|
|
|
|
{
|
|
|
|
public class StackResolver
|
|
|
|
{
|
|
|
|
private readonly NamingOptions _options;
|
|
|
|
|
|
|
|
public StackResolver(NamingOptions options)
|
|
|
|
{
|
|
|
|
_options = options;
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:18:56 -07:00
|
|
|
public IEnumerable<FileStack> ResolveDirectories(IEnumerable<string> files)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2020-03-25 09:53:03 -07:00
|
|
|
return Resolve(files.Select(i => new FileSystemMetadata { FullName = i, IsDirectory = true }));
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
2020-01-22 14:18:56 -07:00
|
|
|
public IEnumerable<FileStack> ResolveFiles(IEnumerable<string> files)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2020-03-25 09:53:03 -07:00
|
|
|
return Resolve(files.Select(i => new FileSystemMetadata { FullName = i, IsDirectory = false }));
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
2020-01-22 14:18:56 -07:00
|
|
|
public IEnumerable<FileStack> ResolveAudioBooks(IEnumerable<FileSystemMetadata> files)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2020-03-25 09:53:03 -07:00
|
|
|
foreach (var directory in files.GroupBy(file =>
|
|
|
|
file.IsDirectory ? file.FullName : Path.GetDirectoryName(file.FullName)))
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2020-03-25 09:53:03 -07:00
|
|
|
var stack = new FileStack { Name = Path.GetFileName(directory.Key), IsDirectoryStack = false };
|
2018-09-12 10:26:21 -07:00
|
|
|
foreach (var file in directory)
|
|
|
|
{
|
|
|
|
if (file.IsDirectory)
|
2019-05-10 11:37:42 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
continue;
|
2019-05-10 11:37:42 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
stack.Files.Add(file.FullName);
|
|
|
|
}
|
2019-05-10 11:37:42 -07:00
|
|
|
|
2020-01-22 14:18:56 -07:00
|
|
|
yield return stack;
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:18:56 -07:00
|
|
|
public IEnumerable<FileStack> Resolve(IEnumerable<FileSystemMetadata> files)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
var resolver = new VideoResolver(_options);
|
|
|
|
|
|
|
|
var list = files
|
2020-01-22 14:18:56 -07:00
|
|
|
.Where(i => i.IsDirectory || resolver.IsVideoFile(i.FullName) || resolver.IsStubFile(i.FullName))
|
2018-09-12 10:26:21 -07:00
|
|
|
.OrderBy(i => i.FullName)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
var expressions = _options.VideoFileStackingRegexes;
|
|
|
|
|
|
|
|
for (var i = 0; i < list.Count; i++)
|
|
|
|
{
|
|
|
|
var offset = 0;
|
|
|
|
|
|
|
|
var file1 = list[i];
|
|
|
|
|
|
|
|
var expressionIndex = 0;
|
|
|
|
while (expressionIndex < expressions.Length)
|
|
|
|
{
|
|
|
|
var exp = expressions[expressionIndex];
|
|
|
|
var stack = new FileStack();
|
|
|
|
|
|
|
|
// (Title)(Volume)(Ignore)(Extension)
|
|
|
|
var match1 = FindMatch(file1, exp, offset);
|
|
|
|
|
|
|
|
if (match1.Success)
|
|
|
|
{
|
|
|
|
var title1 = match1.Groups[1].Value;
|
|
|
|
var volume1 = match1.Groups[2].Value;
|
|
|
|
var ignore1 = match1.Groups[3].Value;
|
|
|
|
var extension1 = match1.Groups[4].Value;
|
|
|
|
|
|
|
|
var j = i + 1;
|
|
|
|
while (j < list.Count)
|
|
|
|
{
|
|
|
|
var file2 = list[j];
|
|
|
|
|
|
|
|
if (file1.IsDirectory != file2.IsDirectory)
|
|
|
|
{
|
|
|
|
j++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (Title)(Volume)(Ignore)(Extension)
|
|
|
|
var match2 = FindMatch(file2, exp, offset);
|
|
|
|
|
|
|
|
if (match2.Success)
|
|
|
|
{
|
|
|
|
var title2 = match2.Groups[1].Value;
|
|
|
|
var volume2 = match2.Groups[2].Value;
|
|
|
|
var ignore2 = match2.Groups[3].Value;
|
|
|
|
var extension2 = match2.Groups[4].Value;
|
|
|
|
|
|
|
|
if (string.Equals(title1, title2, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
if (!string.Equals(volume1, volume2, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2019-05-10 11:37:42 -07:00
|
|
|
if (string.Equals(ignore1, ignore2, StringComparison.OrdinalIgnoreCase)
|
|
|
|
&& string.Equals(extension1, extension2, StringComparison.OrdinalIgnoreCase))
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
if (stack.Files.Count == 0)
|
|
|
|
{
|
|
|
|
stack.Name = title1 + ignore1;
|
|
|
|
stack.IsDirectoryStack = file1.IsDirectory;
|
|
|
|
stack.Files.Add(file1.FullName);
|
|
|
|
}
|
2019-05-10 11:37:42 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
stack.Files.Add(file2.FullName);
|
|
|
|
}
|
2019-01-07 16:27:46 -07:00
|
|
|
else
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
// Sequel
|
|
|
|
offset = 0;
|
|
|
|
expressionIndex++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!string.Equals(ignore1, ignore2, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
// False positive, try again with offset
|
|
|
|
offset = match1.Groups[3].Index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Extension mismatch
|
|
|
|
offset = 0;
|
|
|
|
expressionIndex++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Title mismatch
|
|
|
|
offset = 0;
|
|
|
|
expressionIndex++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No match 2, next expression
|
|
|
|
offset = 0;
|
|
|
|
expressionIndex++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j == list.Count)
|
|
|
|
{
|
|
|
|
expressionIndex = expressions.Length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No match 1
|
|
|
|
offset = 0;
|
|
|
|
expressionIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack.Files.Count > 1)
|
|
|
|
{
|
2020-01-22 14:18:56 -07:00
|
|
|
yield return stack;
|
2018-09-12 10:26:21 -07:00
|
|
|
i += stack.Files.Count - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 13:56:35 -07:00
|
|
|
private static string GetRegexInput(FileSystemMetadata file)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
// For directories, dummy up an extension otherwise the expressions will fail
|
|
|
|
var input = !file.IsDirectory
|
|
|
|
? file.FullName
|
|
|
|
: file.FullName + ".mkv";
|
|
|
|
|
|
|
|
return Path.GetFileName(input);
|
|
|
|
}
|
|
|
|
|
2020-02-19 13:56:35 -07:00
|
|
|
private static Match FindMatch(FileSystemMetadata input, Regex regex, int offset)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
var regexInput = GetRegexInput(input);
|
|
|
|
|
|
|
|
if (offset < 0 || offset >= regexInput.Length)
|
|
|
|
{
|
|
|
|
return Match.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return regex.Match(regexInput, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|