StackResolver.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.AudioBook;
  8. using Emby.Naming.Common;
  9. using MediaBrowser.Model.IO;
  10. namespace Emby.Naming.Video
  11. {
  12. public class StackResolver
  13. {
  14. private readonly NamingOptions _options;
  15. public StackResolver(NamingOptions options)
  16. {
  17. _options = options;
  18. }
  19. public IEnumerable<FileStack> ResolveDirectories(IEnumerable<string> files)
  20. {
  21. return Resolve(files.Select(i => new FileSystemMetadata { FullName = i, IsDirectory = true }));
  22. }
  23. public IEnumerable<FileStack> ResolveFiles(IEnumerable<string> files)
  24. {
  25. return Resolve(files.Select(i => new FileSystemMetadata { FullName = i, IsDirectory = false }));
  26. }
  27. public IEnumerable<FileStack> ResolveAudioBooks(IEnumerable<AudioBookFileInfo> files)
  28. {
  29. var groupedDirectoryFiles = files.GroupBy(file => Path.GetDirectoryName(file.Path));
  30. foreach (var directory in groupedDirectoryFiles)
  31. {
  32. if (string.IsNullOrEmpty(directory.Key))
  33. {
  34. foreach (var file in directory)
  35. {
  36. var stack = new FileStack { Name = Path.GetFileNameWithoutExtension(file.Path), IsDirectoryStack = false };
  37. stack.Files.Add(file.Path);
  38. yield return stack;
  39. }
  40. }
  41. else
  42. {
  43. var stack = new FileStack { Name = Path.GetFileName(directory.Key), IsDirectoryStack = false };
  44. foreach (var file in directory)
  45. {
  46. stack.Files.Add(file.Path);
  47. }
  48. yield return stack;
  49. }
  50. }
  51. }
  52. public IEnumerable<FileStack> Resolve(IEnumerable<FileSystemMetadata> files)
  53. {
  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["title"].Value;
  74. var volume1 = match1.Groups["volume"].Value;
  75. var ignore1 = match1.Groups["ignore"].Value;
  76. var extension1 = match1.Groups["extension"].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.Files.Add(file1.FullName);
  106. }
  107. stack.Files.Add(file2.FullName);
  108. }
  109. else
  110. {
  111. // Sequel
  112. offset = 0;
  113. expressionIndex++;
  114. break;
  115. }
  116. }
  117. else if (!string.Equals(ignore1, ignore2, StringComparison.OrdinalIgnoreCase))
  118. {
  119. // False positive, try again with offset
  120. offset = match1.Groups[3].Index;
  121. break;
  122. }
  123. else
  124. {
  125. // Extension mismatch
  126. offset = 0;
  127. expressionIndex++;
  128. break;
  129. }
  130. }
  131. else
  132. {
  133. // Title mismatch
  134. offset = 0;
  135. expressionIndex++;
  136. break;
  137. }
  138. }
  139. else
  140. {
  141. // No match 2, next expression
  142. offset = 0;
  143. expressionIndex++;
  144. break;
  145. }
  146. j++;
  147. }
  148. if (j == list.Count)
  149. {
  150. expressionIndex = expressions.Length;
  151. }
  152. }
  153. else
  154. {
  155. // No match 1
  156. offset = 0;
  157. expressionIndex++;
  158. }
  159. if (stack.Files.Count > 1)
  160. {
  161. yield return stack;
  162. i += stack.Files.Count - 1;
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. private static string GetRegexInput(FileSystemMetadata file)
  169. {
  170. // For directories, dummy up an extension otherwise the expressions will fail
  171. var input = !file.IsDirectory
  172. ? file.FullName
  173. : file.FullName + ".mkv";
  174. return Path.GetFileName(input);
  175. }
  176. private static Match FindMatch(FileSystemMetadata input, Regex regex, int offset)
  177. {
  178. var regexInput = GetRegexInput(input);
  179. if (offset < 0 || offset >= regexInput.Length)
  180. {
  181. return Match.Empty;
  182. }
  183. return regex.Match(regexInput, offset);
  184. }
  185. }
  186. }