EntityResolutionHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. private static readonly Dictionary<string,string> AudioFileExtensions = new[] {
  68. ".mp3",
  69. ".flac",
  70. ".wma",
  71. ".aac",
  72. ".acc",
  73. ".m4a",
  74. ".m4b",
  75. ".wav",
  76. ".ape",
  77. ".ogg",
  78. ".oga"
  79. }.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. var extension = Path.GetExtension(path);
  88. if (string.IsNullOrEmpty(extension))
  89. {
  90. return false;
  91. }
  92. return AudioFileExtensions.ContainsKey(extension);
  93. }
  94. /// <summary>
  95. /// Determines whether [is video file] [the specified path].
  96. /// </summary>
  97. /// <param name="path">The path.</param>
  98. /// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
  99. public static bool IsVideoFile(string path)
  100. {
  101. var extension = Path.GetExtension(path);
  102. if (string.IsNullOrEmpty(extension))
  103. {
  104. return false;
  105. }
  106. return VideoFileExtensionsDictionary.ContainsKey(extension);
  107. }
  108. /// <summary>
  109. /// Ensures DateCreated and DateModified have values
  110. /// </summary>
  111. /// <param name="item">The item.</param>
  112. /// <param name="args">The args.</param>
  113. public static void EnsureDates(BaseItem item, ItemResolveArgs args)
  114. {
  115. if (!Path.IsPathRooted(item.Path))
  116. {
  117. return;
  118. }
  119. // See if a different path came out of the resolver than what went in
  120. if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
  121. {
  122. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  123. if (childData != null)
  124. {
  125. item.DateCreated = childData.CreationTimeUtc;
  126. item.DateModified = childData.LastWriteTimeUtc;
  127. }
  128. else
  129. {
  130. var fileData = FileSystem.GetFileSystemInfo(item.Path);
  131. if (fileData.Exists)
  132. {
  133. item.DateCreated = fileData.CreationTimeUtc;
  134. item.DateModified = fileData.LastWriteTimeUtc;
  135. }
  136. }
  137. }
  138. else
  139. {
  140. item.DateCreated = args.FileInfo.CreationTimeUtc;
  141. item.DateModified = args.FileInfo.LastWriteTimeUtc;
  142. }
  143. }
  144. }
  145. }