EntityResolutionHelper.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System.Globalization;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text.RegularExpressions;
  10. namespace MediaBrowser.Controller.Resolvers
  11. {
  12. /// <summary>
  13. /// Class EntityResolutionHelper
  14. /// </summary>
  15. public static class EntityResolutionHelper
  16. {
  17. /// <summary>
  18. /// Any folder named in this list will be ignored - can be added to at runtime for extensibility
  19. /// </summary>
  20. public static readonly List<string> IgnoreFolders = new List<string>
  21. {
  22. "metadata",
  23. "ps3_update",
  24. "ps3_vprm",
  25. "extrafanart",
  26. "extrathumbs",
  27. ".actors",
  28. ".wd_tv"
  29. };
  30. /// <summary>
  31. /// Any extension in this list is considered a video file - can be added to at runtime for extensibility
  32. /// </summary>
  33. public static List<string> VideoFileExtensions = new List<string>
  34. {
  35. ".mkv",
  36. ".m2t",
  37. ".m2ts",
  38. ".img",
  39. ".iso",
  40. ".mk3d",
  41. ".ts",
  42. ".rmvb",
  43. ".mov",
  44. ".avi",
  45. ".mpg",
  46. ".mpeg",
  47. ".wmv",
  48. ".mp4",
  49. ".divx",
  50. ".dvr-ms",
  51. ".wtv",
  52. ".ogm",
  53. ".ogv",
  54. ".asf",
  55. ".m4v",
  56. ".flv",
  57. ".f4v",
  58. ".3gp",
  59. ".webm",
  60. ".mts",
  61. ".m2v",
  62. ".rec"
  63. };
  64. private static readonly Dictionary<string, string> VideoFileExtensionsDictionary = VideoFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  65. private static readonly Regex MultiFileRegex = new Regex(
  66. @"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)(.*?)(\.[^.]+)$",
  67. RegexOptions.Compiled | RegexOptions.IgnoreCase);
  68. private static readonly Regex MultiFolderRegex = new Regex(
  69. @"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)$",
  70. RegexOptions.Compiled | RegexOptions.IgnoreCase);
  71. /// <summary>
  72. /// Determines whether [is multi part file] [the specified path].
  73. /// </summary>
  74. /// <param name="path">The path.</param>
  75. /// <returns><c>true</c> if [is multi part file] [the specified path]; otherwise, <c>false</c>.</returns>
  76. public static bool IsMultiPartFile(string path)
  77. {
  78. if (string.IsNullOrEmpty(path))
  79. {
  80. throw new ArgumentNullException("path");
  81. }
  82. path = Path.GetFileName(path);
  83. return MultiFileRegex.Match(path).Success;
  84. }
  85. public static bool IsMultiPartFolder(string path)
  86. {
  87. if (string.IsNullOrEmpty(path))
  88. {
  89. throw new ArgumentNullException("path");
  90. }
  91. path = Path.GetFileName(path);
  92. return MultiFolderRegex.Match(path).Success;
  93. }
  94. /// <summary>
  95. /// The audio file extensions
  96. /// </summary>
  97. public static readonly string[] AudioFileExtensions =
  98. {
  99. ".mp3",
  100. ".flac",
  101. ".wma",
  102. ".aac",
  103. ".acc",
  104. ".m4a",
  105. ".m4b",
  106. ".wav",
  107. ".ape",
  108. ".ogg",
  109. ".oga"
  110. //".asf",
  111. //".mp4"
  112. };
  113. private static readonly Dictionary<string, string> AudioFileExtensionsDictionary = AudioFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  114. /// <summary>
  115. /// Determines whether [is audio file] [the specified args].
  116. /// </summary>
  117. /// <param name="path">The path.</param>
  118. /// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
  119. public static bool IsAudioFile(string path)
  120. {
  121. if (string.IsNullOrEmpty(path))
  122. {
  123. throw new ArgumentNullException("path");
  124. }
  125. var extension = Path.GetExtension(path);
  126. if (string.IsNullOrEmpty(extension))
  127. {
  128. return false;
  129. }
  130. return AudioFileExtensionsDictionary.ContainsKey(extension);
  131. }
  132. /// <summary>
  133. /// Determines whether [is video file] [the specified path].
  134. /// </summary>
  135. /// <param name="path">The path.</param>
  136. /// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
  137. public static bool IsVideoFile(string path)
  138. {
  139. if (string.IsNullOrEmpty(path))
  140. {
  141. throw new ArgumentNullException("path");
  142. }
  143. var extension = Path.GetExtension(path);
  144. if (string.IsNullOrEmpty(extension))
  145. {
  146. return false;
  147. }
  148. return VideoFileExtensionsDictionary.ContainsKey(extension);
  149. }
  150. /// <summary>
  151. /// Determines whether [is place holder] [the specified path].
  152. /// </summary>
  153. /// <param name="path">The path.</param>
  154. /// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
  155. /// <exception cref="System.ArgumentNullException">path</exception>
  156. public static bool IsVideoPlaceHolder(string path)
  157. {
  158. if (string.IsNullOrEmpty(path))
  159. {
  160. throw new ArgumentNullException("path");
  161. }
  162. var extension = Path.GetExtension(path);
  163. return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
  164. }
  165. /// <summary>
  166. /// Determines whether [is multi disc album folder] [the specified path].
  167. /// </summary>
  168. /// <param name="path">The path.</param>
  169. /// <returns><c>true</c> if [is multi disc album folder] [the specified path]; otherwise, <c>false</c>.</returns>
  170. public static bool IsMultiDiscAlbumFolder(string path)
  171. {
  172. var filename = Path.GetFileName(path);
  173. if (string.IsNullOrWhiteSpace(filename))
  174. {
  175. return false;
  176. }
  177. // Normalize
  178. // Remove whitespace
  179. filename = filename.Replace("-", string.Empty);
  180. filename = Regex.Replace(filename, @"\s+", "");
  181. var prefixes = new[] { "disc", "cd", "disk" };
  182. foreach (var prefix in prefixes)
  183. {
  184. if (filename.IndexOf(prefix, StringComparison.OrdinalIgnoreCase) == 0)
  185. {
  186. var tmp = filename.Substring(prefix.Length);
  187. int val;
  188. if (int.TryParse(tmp, NumberStyles.Any, CultureInfo.InvariantCulture, out val))
  189. {
  190. return true;
  191. }
  192. }
  193. }
  194. return false;
  195. }
  196. /// <summary>
  197. /// Ensures DateCreated and DateModified have values
  198. /// </summary>
  199. /// <param name="fileSystem">The file system.</param>
  200. /// <param name="item">The item.</param>
  201. /// <param name="args">The args.</param>
  202. /// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
  203. public static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime)
  204. {
  205. if (fileSystem == null)
  206. {
  207. throw new ArgumentNullException("fileSystem");
  208. }
  209. if (item == null)
  210. {
  211. throw new ArgumentNullException("item");
  212. }
  213. if (args == null)
  214. {
  215. throw new ArgumentNullException("args");
  216. }
  217. // See if a different path came out of the resolver than what went in
  218. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  219. {
  220. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  221. if (childData != null)
  222. {
  223. if (includeCreationTime)
  224. {
  225. item.DateCreated = fileSystem.GetCreationTimeUtc(childData);
  226. }
  227. item.DateModified = fileSystem.GetLastWriteTimeUtc(childData);
  228. }
  229. else
  230. {
  231. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  232. if (fileData.Exists)
  233. {
  234. if (includeCreationTime)
  235. {
  236. item.DateCreated = fileSystem.GetCreationTimeUtc(fileData);
  237. }
  238. item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData);
  239. }
  240. }
  241. }
  242. else
  243. {
  244. if (includeCreationTime)
  245. {
  246. item.DateCreated = fileSystem.GetCreationTimeUtc(args.FileInfo);
  247. }
  248. item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo);
  249. }
  250. }
  251. }
  252. }