EntityResolutionHelper.cs 4.8 KB

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