StackResolver.cs 9.4 KB

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