EntityResolutionHelper.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. return MultiFileRegex.Match(path).Success || MultiFolderRegex.Match(path).Success;
  69. }
  70. /// <summary>
  71. /// The audio file extensions
  72. /// </summary>
  73. public static readonly string[] AudioFileExtensions =
  74. {
  75. ".mp3",
  76. ".flac",
  77. ".wma",
  78. ".aac",
  79. ".acc",
  80. ".m4a",
  81. ".m4b",
  82. ".wav",
  83. ".ape",
  84. ".ogg",
  85. ".oga"
  86. };
  87. private static readonly Dictionary<string, string> AudioFileExtensionsDictionary = AudioFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  88. /// <summary>
  89. /// Determines whether [is audio file] [the specified args].
  90. /// </summary>
  91. /// <param name="path">The path.</param>
  92. /// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
  93. public static bool IsAudioFile(string path)
  94. {
  95. if (string.IsNullOrEmpty(path))
  96. {
  97. throw new ArgumentNullException("path");
  98. }
  99. var extension = Path.GetExtension(path);
  100. if (string.IsNullOrEmpty(extension))
  101. {
  102. return false;
  103. }
  104. return AudioFileExtensionsDictionary.ContainsKey(extension);
  105. }
  106. /// <summary>
  107. /// Determines whether [is video file] [the specified path].
  108. /// </summary>
  109. /// <param name="path">The path.</param>
  110. /// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
  111. public static bool IsVideoFile(string path)
  112. {
  113. if (string.IsNullOrEmpty(path))
  114. {
  115. throw new ArgumentNullException("path");
  116. }
  117. var extension = Path.GetExtension(path);
  118. if (string.IsNullOrEmpty(extension))
  119. {
  120. return false;
  121. }
  122. return VideoFileExtensionsDictionary.ContainsKey(extension);
  123. }
  124. /// <summary>
  125. /// Determines whether [is place holder] [the specified path].
  126. /// </summary>
  127. /// <param name="path">The path.</param>
  128. /// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
  129. /// <exception cref="System.ArgumentNullException">path</exception>
  130. public static bool IsVideoPlaceHolder(string path)
  131. {
  132. if (string.IsNullOrEmpty(path))
  133. {
  134. throw new ArgumentNullException("path");
  135. }
  136. var extension = Path.GetExtension(path);
  137. return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
  138. }
  139. /// <summary>
  140. /// Ensures DateCreated and DateModified have values
  141. /// </summary>
  142. /// <param name="fileSystem">The file system.</param>
  143. /// <param name="item">The item.</param>
  144. /// <param name="args">The args.</param>
  145. /// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
  146. public static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime)
  147. {
  148. if (fileSystem == null)
  149. {
  150. throw new ArgumentNullException("fileSystem");
  151. }
  152. if (item == null)
  153. {
  154. throw new ArgumentNullException("item");
  155. }
  156. if (args == null)
  157. {
  158. throw new ArgumentNullException("args");
  159. }
  160. // See if a different path came out of the resolver than what went in
  161. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  162. {
  163. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  164. if (childData != null)
  165. {
  166. if (includeCreationTime)
  167. {
  168. item.DateCreated = fileSystem.GetCreationTimeUtc(childData);
  169. }
  170. item.DateModified = fileSystem.GetLastWriteTimeUtc(childData);
  171. }
  172. else
  173. {
  174. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  175. if (fileData.Exists)
  176. {
  177. if (includeCreationTime)
  178. {
  179. item.DateCreated = fileSystem.GetCreationTimeUtc(fileData);
  180. }
  181. item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData);
  182. }
  183. }
  184. }
  185. else
  186. {
  187. if (includeCreationTime)
  188. {
  189. item.DateCreated = fileSystem.GetCreationTimeUtc(args.FileInfo);
  190. }
  191. item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo);
  192. }
  193. }
  194. }
  195. }