StackResolver.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 list = files
  81. .Where(i => i.IsDirectory || VideoResolver.IsVideoFile(i.FullName, _options) || VideoResolver.IsStubFile(i.FullName, _options))
  82. .OrderBy(i => i.FullName)
  83. .ToList();
  84. var expressions = _options.VideoFileStackingRegexes;
  85. for (var i = 0; i < list.Count; i++)
  86. {
  87. var offset = 0;
  88. var file1 = list[i];
  89. var expressionIndex = 0;
  90. while (expressionIndex < expressions.Length)
  91. {
  92. var exp = expressions[expressionIndex];
  93. var stack = new FileStack();
  94. // (Title)(Volume)(Ignore)(Extension)
  95. var match1 = FindMatch(file1, exp, offset);
  96. if (match1.Success)
  97. {
  98. var title1 = match1.Groups["title"].Value;
  99. var volume1 = match1.Groups["volume"].Value;
  100. var ignore1 = match1.Groups["ignore"].Value;
  101. var extension1 = match1.Groups["extension"].Value;
  102. var j = i + 1;
  103. while (j < list.Count)
  104. {
  105. var file2 = list[j];
  106. if (file1.IsDirectory != file2.IsDirectory)
  107. {
  108. j++;
  109. continue;
  110. }
  111. // (Title)(Volume)(Ignore)(Extension)
  112. var match2 = FindMatch(file2, exp, offset);
  113. if (match2.Success)
  114. {
  115. var title2 = match2.Groups[1].Value;
  116. var volume2 = match2.Groups[2].Value;
  117. var ignore2 = match2.Groups[3].Value;
  118. var extension2 = match2.Groups[4].Value;
  119. if (string.Equals(title1, title2, StringComparison.OrdinalIgnoreCase))
  120. {
  121. if (!string.Equals(volume1, volume2, StringComparison.OrdinalIgnoreCase))
  122. {
  123. if (string.Equals(ignore1, ignore2, StringComparison.OrdinalIgnoreCase)
  124. && string.Equals(extension1, extension2, StringComparison.OrdinalIgnoreCase))
  125. {
  126. if (stack.Files.Count == 0)
  127. {
  128. stack.Name = title1 + ignore1;
  129. stack.IsDirectoryStack = file1.IsDirectory;
  130. stack.Files.Add(file1.FullName);
  131. }
  132. stack.Files.Add(file2.FullName);
  133. }
  134. else
  135. {
  136. // Sequel
  137. offset = 0;
  138. expressionIndex++;
  139. break;
  140. }
  141. }
  142. else if (!string.Equals(ignore1, ignore2, StringComparison.OrdinalIgnoreCase))
  143. {
  144. // False positive, try again with offset
  145. offset = match1.Groups[3].Index;
  146. break;
  147. }
  148. else
  149. {
  150. // Extension mismatch
  151. offset = 0;
  152. expressionIndex++;
  153. break;
  154. }
  155. }
  156. else
  157. {
  158. // Title mismatch
  159. offset = 0;
  160. expressionIndex++;
  161. break;
  162. }
  163. }
  164. else
  165. {
  166. // No match 2, next expression
  167. offset = 0;
  168. expressionIndex++;
  169. break;
  170. }
  171. j++;
  172. }
  173. if (j == list.Count)
  174. {
  175. expressionIndex = expressions.Length;
  176. }
  177. }
  178. else
  179. {
  180. // No match 1
  181. offset = 0;
  182. expressionIndex++;
  183. }
  184. if (stack.Files.Count > 1)
  185. {
  186. yield return stack;
  187. i += stack.Files.Count - 1;
  188. break;
  189. }
  190. }
  191. }
  192. }
  193. private static string GetRegexInput(FileSystemMetadata file)
  194. {
  195. // For directories, dummy up an extension otherwise the expressions will fail
  196. var input = !file.IsDirectory
  197. ? file.FullName
  198. : file.FullName + ".mkv";
  199. return Path.GetFileName(input);
  200. }
  201. private static Match FindMatch(FileSystemMetadata input, Regex regex, int offset)
  202. {
  203. var regexInput = GetRegexInput(input);
  204. if (offset < 0 || offset >= regexInput.Length)
  205. {
  206. return Match.Empty;
  207. }
  208. return regex.Match(regexInput, offset);
  209. }
  210. }
  211. }