StackResolver.cs 8.0 KB

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