EntityResolutionHelper.cs 8.1 KB

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