EntityResolutionHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.IO;
  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. ".rec",
  46. ".webm",
  47. ".mts",
  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. return MultiFileRegex.Match(path).Success || MultiFolderRegex.Match(path).Success;
  65. }
  66. /// <summary>
  67. /// The audio file extensions
  68. /// </summary>
  69. public static readonly string[] AudioFileExtensions = new[]
  70. {
  71. ".mp3",
  72. ".flac",
  73. ".wma",
  74. ".aac",
  75. ".acc",
  76. ".m4a",
  77. ".m4b",
  78. ".wav",
  79. ".ape",
  80. ".ogg",
  81. ".oga"
  82. };
  83. private static readonly Dictionary<string, string> AudioFileExtensionsDictionary = AudioFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  84. /// <summary>
  85. /// Determines whether [is audio file] [the specified args].
  86. /// </summary>
  87. /// <param name="path">The path.</param>
  88. /// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
  89. public static bool IsAudioFile(string 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. var extension = Path.GetExtension(path);
  106. if (string.IsNullOrEmpty(extension))
  107. {
  108. return false;
  109. }
  110. return VideoFileExtensionsDictionary.ContainsKey(extension);
  111. }
  112. /// <summary>
  113. /// Ensures DateCreated and DateModified have values
  114. /// </summary>
  115. /// <param name="item">The item.</param>
  116. /// <param name="args">The args.</param>
  117. /// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
  118. public static void EnsureDates(BaseItem item, ItemResolveArgs args, bool includeCreationTime)
  119. {
  120. if (!Path.IsPathRooted(item.Path))
  121. {
  122. return;
  123. }
  124. // See if a different path came out of the resolver than what went in
  125. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  126. {
  127. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  128. if (childData != null)
  129. {
  130. if (includeCreationTime)
  131. {
  132. item.DateCreated = childData.CreationTimeUtc;
  133. }
  134. item.DateModified = childData.LastWriteTimeUtc;
  135. }
  136. else
  137. {
  138. var fileData = FileSystem.GetFileSystemInfo(item.Path);
  139. if (fileData.Exists)
  140. {
  141. if (includeCreationTime)
  142. {
  143. item.DateCreated = fileData.CreationTimeUtc;
  144. }
  145. item.DateModified = fileData.LastWriteTimeUtc;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. if (includeCreationTime)
  152. {
  153. item.DateCreated = args.FileInfo.CreationTimeUtc;
  154. }
  155. item.DateModified = args.FileInfo.LastWriteTimeUtc;
  156. }
  157. }
  158. }
  159. }