2
0

EntityResolutionHelper.cs 7.4 KB

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