EntityResolutionHelper.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. ".webm",
  46. ".mts"
  47. };
  48. private static readonly Dictionary<string, string> VideoFileExtensionsDictionary = VideoFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  49. private static readonly Regex MultiFileRegex = new Regex(
  50. @"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)(.*?)(\.[^.]+)$",
  51. RegexOptions.Compiled | RegexOptions.IgnoreCase);
  52. private static readonly Regex MultiFolderRegex = new Regex(
  53. @"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)$",
  54. RegexOptions.Compiled | RegexOptions.IgnoreCase);
  55. /// <summary>
  56. /// Determines whether [is multi part file] [the specified path].
  57. /// </summary>
  58. /// <param name="path">The path.</param>
  59. /// <returns><c>true</c> if [is multi part file] [the specified path]; otherwise, <c>false</c>.</returns>
  60. public static bool IsMultiPartFile(string path)
  61. {
  62. return MultiFileRegex.Match(path).Success || MultiFolderRegex.Match(path).Success;
  63. }
  64. /// <summary>
  65. /// The audio file extensions
  66. /// </summary>
  67. public static readonly string[] AudioFileExtensions = new[]
  68. {
  69. ".mp3",
  70. ".flac",
  71. ".wma",
  72. ".aac",
  73. ".acc",
  74. ".m4a",
  75. ".m4b",
  76. ".wav",
  77. ".ape",
  78. ".ogg",
  79. ".oga"
  80. };
  81. private static readonly Dictionary<string, string> AudioFileExtensionsDictionary = AudioFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  82. /// <summary>
  83. /// Determines whether [is audio file] [the specified args].
  84. /// </summary>
  85. /// <param name="path">The path.</param>
  86. /// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
  87. public static bool IsAudioFile(string path)
  88. {
  89. var extension = Path.GetExtension(path);
  90. if (string.IsNullOrEmpty(extension))
  91. {
  92. return false;
  93. }
  94. return AudioFileExtensionsDictionary.ContainsKey(extension);
  95. }
  96. /// <summary>
  97. /// Determines whether [is video file] [the specified path].
  98. /// </summary>
  99. /// <param name="path">The path.</param>
  100. /// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
  101. public static bool IsVideoFile(string path)
  102. {
  103. var extension = Path.GetExtension(path);
  104. if (string.IsNullOrEmpty(extension))
  105. {
  106. return false;
  107. }
  108. return VideoFileExtensionsDictionary.ContainsKey(extension);
  109. }
  110. /// <summary>
  111. /// Ensures DateCreated and DateModified have values
  112. /// </summary>
  113. /// <param name="item">The item.</param>
  114. /// <param name="args">The args.</param>
  115. public static void EnsureDates(BaseItem item, ItemResolveArgs args)
  116. {
  117. if (!Path.IsPathRooted(item.Path))
  118. {
  119. return;
  120. }
  121. // See if a different path came out of the resolver than what went in
  122. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  123. {
  124. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  125. if (childData != null)
  126. {
  127. item.DateCreated = childData.CreationTimeUtc;
  128. item.DateModified = childData.LastWriteTimeUtc;
  129. }
  130. else
  131. {
  132. var fileData = FileSystem.GetFileSystemInfo(item.Path);
  133. if (fileData.Exists)
  134. {
  135. item.DateCreated = fileData.CreationTimeUtc;
  136. item.DateModified = fileData.LastWriteTimeUtc;
  137. }
  138. }
  139. }
  140. else
  141. {
  142. item.DateCreated = args.FileInfo.CreationTimeUtc;
  143. item.DateModified = args.FileInfo.LastWriteTimeUtc;
  144. }
  145. }
  146. }
  147. }