EntityResolutionHelper.cs 8.5 KB

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