EntityResolutionHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.IO;
  4. using MediaBrowser.Controller.Library;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text.RegularExpressions;
  10. namespace MediaBrowser.Controller.Resolvers
  11. {
  12. /// <summary>
  13. /// Class EntityResolutionHelper
  14. /// </summary>
  15. public static class EntityResolutionHelper
  16. {
  17. /// <summary>
  18. /// Any extension in this list is considered a video file - can be added to at runtime for extensibility
  19. /// </summary>
  20. public static List<string> VideoFileExtensions = new List<string>
  21. {
  22. ".mkv",
  23. ".m2t",
  24. ".m2ts",
  25. ".img",
  26. ".iso",
  27. ".mk3d",
  28. ".ts",
  29. ".rmvb",
  30. ".mov",
  31. ".avi",
  32. ".mpg",
  33. ".mpeg",
  34. ".wmv",
  35. ".mp4",
  36. ".divx",
  37. ".dvr-ms",
  38. ".wtv",
  39. ".ogm",
  40. ".ogv",
  41. ".asf",
  42. ".m4v",
  43. ".flv",
  44. ".f4v",
  45. ".3gp",
  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="fileSystem">The file system.</param>
  116. /// <param name="item">The item.</param>
  117. /// <param name="args">The args.</param>
  118. /// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
  119. public static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime)
  120. {
  121. if (!Path.IsPathRooted(item.Path))
  122. {
  123. return;
  124. }
  125. // See if a different path came out of the resolver than what went in
  126. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  127. {
  128. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  129. if (childData != null)
  130. {
  131. if (includeCreationTime)
  132. {
  133. item.DateCreated = fileSystem.GetCreationTimeUtc(childData);
  134. }
  135. item.DateModified = fileSystem.GetLastWriteTimeUtc(childData);
  136. }
  137. else
  138. {
  139. var fileData = fileSystem.GetFileSystemInfo(item.Path);
  140. if (fileData.Exists)
  141. {
  142. if (includeCreationTime)
  143. {
  144. item.DateCreated = fileSystem.GetCreationTimeUtc(fileData);
  145. }
  146. item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData);
  147. }
  148. }
  149. }
  150. else
  151. {
  152. if (includeCreationTime)
  153. {
  154. item.DateCreated = fileSystem.GetCreationTimeUtc(args.FileInfo);
  155. }
  156. item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo);
  157. }
  158. }
  159. }
  160. }