2
0

EntityResolutionHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Win32;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  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 metadata file - can be added to at runtime for extensibility
  18. /// </summary>
  19. public static List<string> MetaExtensions = new List<string>
  20. {
  21. ".xml",
  22. ".jpg",
  23. ".png",
  24. ".json",
  25. ".data"
  26. };
  27. /// <summary>
  28. /// Any extension in this list is considered a video file - can be added to at runtime for extensibility
  29. /// </summary>
  30. public static List<string> VideoFileExtensions = new List<string>
  31. {
  32. ".mkv",
  33. ".m2t",
  34. ".m2ts",
  35. ".img",
  36. ".iso",
  37. ".ts",
  38. ".rmvb",
  39. ".mov",
  40. ".avi",
  41. ".mpg",
  42. ".mpeg",
  43. ".wmv",
  44. ".mp4",
  45. ".divx",
  46. ".dvr-ms",
  47. ".wtv",
  48. ".ogm",
  49. ".ogv",
  50. ".asf",
  51. ".m4v",
  52. ".flv",
  53. ".f4v",
  54. ".3gp",
  55. ".webm"
  56. };
  57. /// <summary>
  58. /// Determines whether [is video file] [the specified path].
  59. /// </summary>
  60. /// <param name="path">The path.</param>
  61. /// <returns><c>true</c> if [is video file] [the specified path]; otherwise, <c>false</c>.</returns>
  62. public static bool IsVideoFile(string path)
  63. {
  64. var extension = Path.GetExtension(path) ?? string.Empty;
  65. return VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  66. }
  67. /// <summary>
  68. /// The audio file extensions
  69. /// </summary>
  70. public static readonly string[] AudioFileExtensions = new[] {
  71. ".mp3",
  72. ".flac",
  73. ".wma",
  74. ".aac",
  75. ".acc",
  76. ".m4a",
  77. ".m4b",
  78. ".wav",
  79. ".ape",
  80. ".ogg",
  81. ".oga"
  82. };
  83. /// <summary>
  84. /// Determines whether [is audio file] [the specified args].
  85. /// </summary>
  86. /// <param name="args">The args.</param>
  87. /// <returns><c>true</c> if [is audio file] [the specified args]; otherwise, <c>false</c>.</returns>
  88. public static bool IsAudioFile(ItemResolveArgs args)
  89. {
  90. return AudioFileExtensions.Contains(Path.GetExtension(args.Path), StringComparer.OrdinalIgnoreCase);
  91. }
  92. /// <summary>
  93. /// Determines whether [is audio file] [the specified file].
  94. /// </summary>
  95. /// <param name="file">The file.</param>
  96. /// <returns><c>true</c> if [is audio file] [the specified file]; otherwise, <c>false</c>.</returns>
  97. public static bool IsAudioFile(WIN32_FIND_DATA file)
  98. {
  99. return AudioFileExtensions.Contains(Path.GetExtension(file.Path), StringComparer.OrdinalIgnoreCase);
  100. }
  101. /// <summary>
  102. /// Determine if the supplied file data points to a music album
  103. /// </summary>
  104. /// <param name="data">The data.</param>
  105. /// <returns><c>true</c> if [is music album] [the specified data]; otherwise, <c>false</c>.</returns>
  106. public static bool IsMusicAlbum(WIN32_FIND_DATA data)
  107. {
  108. return ContainsMusic(FileSystem.GetFiles(data.Path));
  109. }
  110. /// <summary>
  111. /// Determine if the supplied reslove args should be considered a music album
  112. /// </summary>
  113. /// <param name="args">The args.</param>
  114. /// <returns><c>true</c> if [is music album] [the specified args]; otherwise, <c>false</c>.</returns>
  115. public static bool IsMusicAlbum(ItemResolveArgs args)
  116. {
  117. // Args points to an album if parent is an Artist folder or it directly contains music
  118. if (args.IsDirectory)
  119. {
  120. //if (args.Parent is MusicArtist) return true; //saves us from testing children twice
  121. if (ContainsMusic(args.FileSystemChildren)) return true;
  122. }
  123. return false;
  124. }
  125. /// <summary>
  126. /// Determine if the supplied list contains what we should consider music
  127. /// </summary>
  128. /// <param name="list">The list.</param>
  129. /// <returns><c>true</c> if the specified list contains music; otherwise, <c>false</c>.</returns>
  130. public static bool ContainsMusic(IEnumerable<WIN32_FIND_DATA> list)
  131. {
  132. // If list contains at least 2 audio files or at least one and no video files consider it to contain music
  133. var foundAudio = 0;
  134. var foundVideo = 0;
  135. foreach (var file in list)
  136. {
  137. if (IsAudioFile(file)) foundAudio++;
  138. if (foundAudio >= 2)
  139. {
  140. return true;
  141. }
  142. if (IsVideoFile(file.Path)) foundVideo++;
  143. }
  144. // or a single audio file and no video files
  145. if (foundAudio > 0 && foundVideo == 0) return true;
  146. return false;
  147. }
  148. /// <summary>
  149. /// Determines whether a path should be ignored based on its contents - called after the contents have been read
  150. /// </summary>
  151. /// <param name="args">The args.</param>
  152. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  153. public static bool ShouldResolvePathContents(ItemResolveArgs args)
  154. {
  155. // Ignore any folders containing a file called .ignore
  156. return !args.ContainsFileSystemEntryByName(".ignore");
  157. }
  158. /// <summary>
  159. /// Ensures DateCreated and DateModified have values
  160. /// </summary>
  161. /// <param name="item">The item.</param>
  162. /// <param name="args">The args.</param>
  163. public static void EnsureDates(BaseItem item, ItemResolveArgs args)
  164. {
  165. if (!Path.IsPathRooted(item.Path))
  166. {
  167. return;
  168. }
  169. // See if a different path came out of the resolver than what went in
  170. if (!args.Path.Equals(item.Path, StringComparison.OrdinalIgnoreCase))
  171. {
  172. var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
  173. if (childData.HasValue)
  174. {
  175. item.DateCreated = childData.Value.CreationTimeUtc;
  176. item.DateModified = childData.Value.LastWriteTimeUtc;
  177. }
  178. else
  179. {
  180. var fileData = FileSystem.GetFileData(item.Path);
  181. if (fileData.HasValue)
  182. {
  183. item.DateCreated = fileData.Value.CreationTimeUtc;
  184. item.DateModified = fileData.Value.LastWriteTimeUtc;
  185. }
  186. }
  187. }
  188. else
  189. {
  190. item.DateCreated = args.FileInfo.CreationTimeUtc;
  191. item.DateModified = args.FileInfo.LastWriteTimeUtc;
  192. }
  193. }
  194. }
  195. }