EntityResolutionHelper.cs 4.6 KB

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