EntityResolutionHelper.cs 5.3 KB

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