StackResolver.cs 7.9 KB

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