EntityResolutionHelper.cs 7.6 KB

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