EntityResolutionHelper.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. //".asf",
  97. //".mp4"
  98. };
  99. private static readonly Dictionary<string, string> AudioFileExtensionsDictionary = AudioFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  100. /// <summary>
  101. /// Determines whether [is audio file] [the specified args].
  102. /// </summary>
  103. /// <param name="path">The path.</param>
  104. /// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
  105. public static bool IsAudioFile(string path)
  106. {
  107. if (string.IsNullOrEmpty(path))
  108. {
  109. throw new ArgumentNullException("path");
  110. }
  111. var extension = Path.GetExtension(path);
  112. if (string.IsNullOrEmpty(extension))
  113. {
  114. return false;
  115. }
  116. return AudioFileExtensionsDictionary.ContainsKey(extension);
  117. }
  118. /// <summary>
  119. /// Determines whether [is video file] [the specified path].
  120. /// </summary>
  121. /// <param name="path">The path.</param>
  122. /// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
  123. public static bool IsVideoFile(string path)
  124. {
  125. if (string.IsNullOrEmpty(path))
  126. {
  127. throw new ArgumentNullException("path");
  128. }
  129. var extension = Path.GetExtension(path);
  130. if (string.IsNullOrEmpty(extension))
  131. {
  132. return false;
  133. }
  134. return VideoFileExtensionsDictionary.ContainsKey(extension);
  135. }
  136. /// <summary>
  137. /// Determines whether [is place holder] [the specified path].
  138. /// </summary>
  139. /// <param name="path">The path.</param>
  140. /// <returns><c>true</c> if [is place holder] [the specified path]; otherwise, <c>false</c>.</returns>
  141. /// <exception cref="System.ArgumentNullException">path</exception>
  142. public static bool IsVideoPlaceHolder(string path)
  143. {
  144. if (string.IsNullOrEmpty(path))
  145. {
  146. throw new ArgumentNullException("path");
  147. }
  148. var extension = Path.GetExtension(path);
  149. return string.Equals(extension, ".disc", StringComparison.OrdinalIgnoreCase);
  150. }
  151. /// <summary>
  152. /// Ensures DateCreated and DateModified have values
  153. /// </summary>
  154. /// <param name="fileSystem">The file system.</param>
  155. /// <param name="item">The item.</param>
  156. /// <param name="args">The args.</param>
  157. /// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
  158. public static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime)
  159. {
  160. if (fileSystem == null)
  161. {
  162. throw new ArgumentNullException("fileSystem");
  163. }
  164. if (item == null)
  165. {
  166. throw new ArgumentNullException("item");
  167. }
  168. if (args == null)
  169. {
  170. throw new ArgumentNullException("args");
  171. }
  172. // See if a different path came out of the resolver than what went in
  173. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  174. {
  175. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  176. if (childData != null)
  177. {
  178. if (includeCreationTime)
  179. {
  180. item.DateCreated = fileSystem.GetCreationTimeUtc(childData);
  181. }
  182. item.DateModified = fileSystem.GetLastWriteTimeUtc(childData);
  183. }
  184. else
  185. {
  186. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  187. if (fileData.Exists)
  188. {
  189. if (includeCreationTime)
  190. {
  191. item.DateCreated = fileSystem.GetCreationTimeUtc(fileData);
  192. }
  193. item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData);
  194. }
  195. }
  196. }
  197. else
  198. {
  199. if (includeCreationTime)
  200. {
  201. item.DateCreated = fileSystem.GetCreationTimeUtc(args.FileInfo);
  202. }
  203. item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo);
  204. }
  205. }
  206. }
  207. }