StackResolver.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using Emby.Naming.Common;
  8. using MediaBrowser.Model.IO;
  9. namespace Emby.Naming.Video
  10. {
  11. public class StackResolver
  12. {
  13. private readonly NamingOptions _options;
  14. public StackResolver(NamingOptions options)
  15. {
  16. _options = options;
  17. }
  18. public IEnumerable<FileStack> ResolveDirectories(IEnumerable<string> files)
  19. {
  20. return Resolve(files.Select(i => new FileSystemMetadata { FullName = i, IsDirectory = true }));
  21. }
  22. public IEnumerable<FileStack> ResolveFiles(IEnumerable<string> files)
  23. {
  24. return Resolve(files.Select(i => new FileSystemMetadata { FullName = i, IsDirectory = false }));
  25. }
  26. public IEnumerable<FileStack> ResolveAudioBooks(IEnumerable<FileSystemMetadata> files)
  27. {
  28. var groupedDirectoryFiles = files.GroupBy(file =>
  29. file.IsDirectory
  30. ? file.FullName
  31. : Path.GetDirectoryName(file.FullName));
  32. foreach (var directory in groupedDirectoryFiles)
  33. {
  34. var stack = new FileStack { Name = Path.GetFileName(directory.Key), IsDirectoryStack = false };
  35. foreach (var file in directory)
  36. {
  37. if (file.IsDirectory)
  38. {
  39. continue;
  40. }
  41. stack.Files.Add(file.FullName);
  42. }
  43. yield return stack;
  44. }
  45. }
  46. public IEnumerable<FileStack> Resolve(IEnumerable<FileSystemMetadata> files)
  47. {
  48. var resolver = new VideoResolver(_options);
  49. var list = files
  50. .Where(i => i.IsDirectory || resolver.IsVideoFile(i.FullName) || resolver.IsStubFile(i.FullName))
  51. .OrderBy(i => i.FullName)
  52. .ToList();
  53. var expressions = _options.VideoFileStackingRegexes;
  54. for (var i = 0; i < list.Count; i++)
  55. {
  56. var offset = 0;
  57. var file1 = list[i];
  58. var expressionIndex = 0;
  59. while (expressionIndex < expressions.Length)
  60. {
  61. var exp = expressions[expressionIndex];
  62. var stack = new FileStack();
  63. // (Title)(Volume)(Ignore)(Extension)
  64. var match1 = FindMatch(file1, exp, offset);
  65. if (match1.Success)
  66. {
  67. var title1 = match1.Groups[1].Value;
  68. var volume1 = match1.Groups[2].Value;
  69. var ignore1 = match1.Groups[3].Value;
  70. var extension1 = match1.Groups[4].Value;
  71. var j = i + 1;
  72. while (j < list.Count)
  73. {
  74. var file2 = list[j];
  75. if (file1.IsDirectory != file2.IsDirectory)
  76. {
  77. j++;
  78. continue;
  79. }
  80. // (Title)(Volume)(Ignore)(Extension)
  81. var match2 = FindMatch(file2, exp, offset);
  82. if (match2.Success)
  83. {
  84. var title2 = match2.Groups[1].Value;
  85. var volume2 = match2.Groups[2].Value;
  86. var ignore2 = match2.Groups[3].Value;
  87. var extension2 = match2.Groups[4].Value;
  88. if (string.Equals(title1, title2, StringComparison.OrdinalIgnoreCase))
  89. {
  90. if (!string.Equals(volume1, volume2, StringComparison.OrdinalIgnoreCase))
  91. {
  92. if (string.Equals(ignore1, ignore2, StringComparison.OrdinalIgnoreCase)
  93. && string.Equals(extension1, extension2, StringComparison.OrdinalIgnoreCase))
  94. {
  95. if (stack.Files.Count == 0)
  96. {
  97. stack.Name = title1 + ignore1;
  98. stack.IsDirectoryStack = file1.IsDirectory;
  99. stack.Files.Add(file1.FullName);
  100. }
  101. stack.Files.Add(file2.FullName);
  102. }
  103. else
  104. {
  105. // Sequel
  106. offset = 0;
  107. expressionIndex++;
  108. break;
  109. }
  110. }
  111. else if (!string.Equals(ignore1, ignore2, StringComparison.OrdinalIgnoreCase))
  112. {
  113. // False positive, try again with offset
  114. offset = match1.Groups[3].Index;
  115. break;
  116. }
  117. else
  118. {
  119. // Extension mismatch
  120. offset = 0;
  121. expressionIndex++;
  122. break;
  123. }
  124. }
  125. else
  126. {
  127. // Title mismatch
  128. offset = 0;
  129. expressionIndex++;
  130. break;
  131. }
  132. }
  133. else
  134. {
  135. // No match 2, next expression
  136. offset = 0;
  137. expressionIndex++;
  138. break;
  139. }
  140. j++;
  141. }
  142. if (j == list.Count)
  143. {
  144. expressionIndex = expressions.Length;
  145. }
  146. }
  147. else
  148. {
  149. // No match 1
  150. offset = 0;
  151. expressionIndex++;
  152. }
  153. if (stack.Files.Count > 1)
  154. {
  155. yield return stack;
  156. i += stack.Files.Count - 1;
  157. break;
  158. }
  159. }
  160. }
  161. }
  162. private static string GetRegexInput(FileSystemMetadata file)
  163. {
  164. // For directories, dummy up an extension otherwise the expressions will fail
  165. var input = !file.IsDirectory
  166. ? file.FullName
  167. : file.FullName + ".mkv";
  168. return Path.GetFileName(input);
  169. }
  170. private static Match FindMatch(FileSystemMetadata input, Regex regex, int offset)
  171. {
  172. var regexInput = GetRegexInput(input);
  173. if (offset < 0 || offset >= regexInput.Length)
  174. {
  175. return Match.Empty;
  176. }
  177. return regex.Match(regexInput, offset);
  178. }
  179. }
  180. }