EntityResolutionHelper.cs 7.4 KB

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