StackResolver.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using Emby.Naming.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  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. stack.Name = Path.GetFileName(directory.Key);
  40. stack.IsDirectoryStack = false;
  41. foreach (var file in directory)
  42. {
  43. if (file.IsDirectory)
  44. continue;
  45. stack.Files.Add(file.FullName);
  46. }
  47. result.Stacks.Add(stack);
  48. }
  49. return result;
  50. }
  51. public StackResult Resolve(IEnumerable<FileSystemMetadata> files)
  52. {
  53. var result = new StackResult();
  54. var resolver = new VideoResolver(_options);
  55. var list = files
  56. .Where(i => i.IsDirectory || (resolver.IsVideoFile(i.FullName) || resolver.IsStubFile(i.FullName)))
  57. .OrderBy(i => i.FullName)
  58. .ToList();
  59. var expressions = _options.VideoFileStackingRegexes;
  60. for (var i = 0; i < list.Count; i++)
  61. {
  62. var offset = 0;
  63. var file1 = list[i];
  64. var expressionIndex = 0;
  65. while (expressionIndex < expressions.Length)
  66. {
  67. var exp = expressions[expressionIndex];
  68. var stack = new FileStack();
  69. // (Title)(Volume)(Ignore)(Extension)
  70. var match1 = FindMatch(file1, exp, offset);
  71. if (match1.Success)
  72. {
  73. var title1 = match1.Groups[1].Value;
  74. var volume1 = match1.Groups[2].Value;
  75. var ignore1 = match1.Groups[3].Value;
  76. var extension1 = match1.Groups[4].Value;
  77. var j = i + 1;
  78. while (j < list.Count)
  79. {
  80. var file2 = list[j];
  81. if (file1.IsDirectory != file2.IsDirectory)
  82. {
  83. j++;
  84. continue;
  85. }
  86. // (Title)(Volume)(Ignore)(Extension)
  87. var match2 = FindMatch(file2, exp, offset);
  88. if (match2.Success)
  89. {
  90. var title2 = match2.Groups[1].Value;
  91. var volume2 = match2.Groups[2].Value;
  92. var ignore2 = match2.Groups[3].Value;
  93. var extension2 = match2.Groups[4].Value;
  94. if (string.Equals(title1, title2, StringComparison.OrdinalIgnoreCase))
  95. {
  96. if (!string.Equals(volume1, volume2, StringComparison.OrdinalIgnoreCase))
  97. {
  98. if (string.Equals(ignore1, ignore2, StringComparison.OrdinalIgnoreCase) &&
  99. string.Equals(extension1, extension2, StringComparison.OrdinalIgnoreCase))
  100. {
  101. if (stack.Files.Count == 0)
  102. {
  103. stack.Name = title1 + ignore1;
  104. stack.IsDirectoryStack = file1.IsDirectory;
  105. //stack.Name = title1 + ignore1 + extension1;
  106. stack.Files.Add(file1.FullName);
  107. }
  108. stack.Files.Add(file2.FullName);
  109. }
  110. else
  111. {
  112. // Sequel
  113. offset = 0;
  114. expressionIndex++;
  115. break;
  116. }
  117. }
  118. else if (!string.Equals(ignore1, ignore2, StringComparison.OrdinalIgnoreCase))
  119. {
  120. // False positive, try again with offset
  121. offset = match1.Groups[3].Index;
  122. break;
  123. }
  124. else
  125. {
  126. // Extension mismatch
  127. offset = 0;
  128. expressionIndex++;
  129. break;
  130. }
  131. }
  132. else
  133. {
  134. // Title mismatch
  135. offset = 0;
  136. expressionIndex++;
  137. break;
  138. }
  139. }
  140. else
  141. {
  142. // No match 2, next expression
  143. offset = 0;
  144. expressionIndex++;
  145. break;
  146. }
  147. j++;
  148. }
  149. if (j == list.Count)
  150. {
  151. expressionIndex = expressions.Length;
  152. }
  153. }
  154. else
  155. {
  156. // No match 1
  157. offset = 0;
  158. expressionIndex++;
  159. }
  160. if (stack.Files.Count > 1)
  161. {
  162. result.Stacks.Add(stack);
  163. i += stack.Files.Count - 1;
  164. break;
  165. }
  166. }
  167. }
  168. return result;
  169. }
  170. private 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 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. }