ImageFromMediaLocationProvider.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Controller.Providers
  12. {
  13. /// <summary>
  14. /// Provides images for all types by looking for standard images - folder, backdrop, logo, etc.
  15. /// </summary>
  16. public class ImageFromMediaLocationProvider : BaseMetadataProvider
  17. {
  18. public ImageFromMediaLocationProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
  19. : base(logManager, configurationManager)
  20. {
  21. }
  22. /// <summary>
  23. /// Supportses the specified item.
  24. /// </summary>
  25. /// <param name="item">The item.</param>
  26. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  27. public override bool Supports(BaseItem item)
  28. {
  29. return item.LocationType == LocationType.FileSystem && item.ResolveArgs.IsDirectory;
  30. }
  31. /// <summary>
  32. /// Gets the priority.
  33. /// </summary>
  34. /// <value>The priority.</value>
  35. public override MetadataProviderPriority Priority
  36. {
  37. get { return MetadataProviderPriority.First; }
  38. }
  39. /// <summary>
  40. /// Returns true or false indicating if the provider should refresh when the contents of it's directory changes
  41. /// </summary>
  42. /// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
  43. protected override bool RefreshOnFileSystemStampChange
  44. {
  45. get
  46. {
  47. return true;
  48. }
  49. }
  50. /// <summary>
  51. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  52. /// </summary>
  53. /// <param name="item">The item.</param>
  54. /// <param name="force">if set to <c>true</c> [force].</param>
  55. /// <param name="cancellationToken">The cancellation token.</param>
  56. /// <returns>Task{System.Boolean}.</returns>
  57. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  58. {
  59. cancellationToken.ThrowIfCancellationRequested();
  60. // Make sure current image paths still exist
  61. ValidateImages(item);
  62. cancellationToken.ThrowIfCancellationRequested();
  63. // Make sure current backdrop paths still exist
  64. ValidateBackdrops(item);
  65. cancellationToken.ThrowIfCancellationRequested();
  66. PopulateBaseItemImages(item);
  67. SetLastRefreshed(item, DateTime.UtcNow);
  68. return TrueTaskResult;
  69. }
  70. /// <summary>
  71. /// Validates that images within the item are still on the file system
  72. /// </summary>
  73. /// <param name="item">The item.</param>
  74. private void ValidateImages(BaseItem item)
  75. {
  76. if (item.Images == null)
  77. {
  78. return;
  79. }
  80. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  81. var deletedKeys = item.Images.ToList().Where(image =>
  82. {
  83. var path = image.Value;
  84. return IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null;
  85. }).Select(i => i.Key).ToList();
  86. // Now remove them from the dictionary
  87. foreach (var key in deletedKeys)
  88. {
  89. item.Images.Remove(key);
  90. }
  91. }
  92. /// <summary>
  93. /// Validates that backdrops within the item are still on the file system
  94. /// </summary>
  95. /// <param name="item">The item.</param>
  96. private void ValidateBackdrops(BaseItem item)
  97. {
  98. if (item.BackdropImagePaths == null)
  99. {
  100. return;
  101. }
  102. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  103. var deletedImages = item.BackdropImagePaths.Where(path => IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null).ToList();
  104. // Now remove them from the dictionary
  105. foreach (var path in deletedImages)
  106. {
  107. item.BackdropImagePaths.Remove(path);
  108. }
  109. }
  110. /// <summary>
  111. /// Determines whether [is in same directory] [the specified item].
  112. /// </summary>
  113. /// <param name="item">The item.</param>
  114. /// <param name="path">The path.</param>
  115. /// <returns><c>true</c> if [is in same directory] [the specified item]; otherwise, <c>false</c>.</returns>
  116. private bool IsInMetaLocation(BaseItem item, string path)
  117. {
  118. return string.Equals(Path.GetDirectoryName(path), item.MetaLocation, StringComparison.OrdinalIgnoreCase);
  119. }
  120. /// <summary>
  121. /// Gets the image.
  122. /// </summary>
  123. /// <param name="item">The item.</param>
  124. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  125. /// <returns>FileSystemInfo.</returns>
  126. protected virtual FileSystemInfo GetImage(BaseItem item, string filenameWithoutExtension)
  127. {
  128. return BaseItem.SupportedImageExtensions.Select(i => item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.ResolveArgs.Path, filenameWithoutExtension + i))).FirstOrDefault(i => i != null);
  129. }
  130. /// <summary>
  131. /// Fills in image paths based on files win the folder
  132. /// </summary>
  133. /// <param name="item">The item.</param>
  134. private void PopulateBaseItemImages(BaseItem item)
  135. {
  136. // Primary Image
  137. var image = GetImage(item, "folder") ??
  138. GetImage(item, "poster") ??
  139. GetImage(item, "cover") ??
  140. GetImage(item, "default");
  141. if (image != null)
  142. {
  143. item.SetImage(ImageType.Primary, image.FullName);
  144. }
  145. // Logo Image
  146. image = GetImage(item, "logo");
  147. if (image != null)
  148. {
  149. item.SetImage(ImageType.Logo, image.FullName);
  150. }
  151. // Banner Image
  152. image = GetImage(item, "banner");
  153. if (image != null)
  154. {
  155. item.SetImage(ImageType.Banner, image.FullName);
  156. }
  157. // Clearart
  158. image = GetImage(item, "clearart");
  159. if (image != null)
  160. {
  161. item.SetImage(ImageType.Art, image.FullName);
  162. }
  163. // Thumbnail Image
  164. image = GetImage(item, "thumb");
  165. if (image != null)
  166. {
  167. item.SetImage(ImageType.Thumb, image.FullName);
  168. }
  169. // Box Image
  170. image = GetImage(item, "box");
  171. if (image != null)
  172. {
  173. item.SetImage(ImageType.Box, image.FullName);
  174. }
  175. // BoxRear Image
  176. image = GetImage(item, "boxrear");
  177. if (image != null)
  178. {
  179. item.SetImage(ImageType.BoxRear, image.FullName);
  180. }
  181. // Thumbnail Image
  182. image = GetImage(item, "menu");
  183. if (image != null)
  184. {
  185. item.SetImage(ImageType.Menu, image.FullName);
  186. }
  187. // Backdrop Image
  188. PopulateBackdrops(item);
  189. // Screenshot Image
  190. image = GetImage(item, "screenshot");
  191. var screenshotFiles = new List<string>();
  192. if (image != null)
  193. {
  194. screenshotFiles.Add(image.FullName);
  195. }
  196. var unfound = 0;
  197. for (var i = 1; i <= 20; i++)
  198. {
  199. // Screenshot Image
  200. image = GetImage(item, "screenshot" + i);
  201. if (image != null)
  202. {
  203. screenshotFiles.Add(image.FullName);
  204. }
  205. else
  206. {
  207. unfound++;
  208. if (unfound >= 3)
  209. {
  210. break;
  211. }
  212. }
  213. }
  214. if (screenshotFiles.Count > 0)
  215. {
  216. item.ScreenshotImagePaths = screenshotFiles;
  217. }
  218. }
  219. /// <summary>
  220. /// Populates the backdrops.
  221. /// </summary>
  222. /// <param name="item">The item.</param>
  223. private void PopulateBackdrops(BaseItem item)
  224. {
  225. var backdropFiles = new List<string>();
  226. PopulateBackdrops(item, backdropFiles, "backdrop", "backdrop");
  227. // Support plex/xbmc conventions
  228. PopulateBackdrops(item, backdropFiles, "fanart", "fanart-");
  229. PopulateBackdrops(item, backdropFiles, "background", "background-");
  230. if (backdropFiles.Count > 0)
  231. {
  232. item.BackdropImagePaths = backdropFiles;
  233. }
  234. }
  235. /// <summary>
  236. /// Populates the backdrops.
  237. /// </summary>
  238. /// <param name="item">The item.</param>
  239. /// <param name="backdropFiles">The backdrop files.</param>
  240. /// <param name="filename">The filename.</param>
  241. /// <param name="numberedSuffix">The numbered suffix.</param>
  242. private void PopulateBackdrops(BaseItem item, List<string> backdropFiles, string filename, string numberedSuffix)
  243. {
  244. var image = GetImage(item, filename);
  245. if (image != null)
  246. {
  247. backdropFiles.Add(image.FullName);
  248. }
  249. var unfound = 0;
  250. for (var i = 1; i <= 20; i++)
  251. {
  252. // Backdrop Image
  253. image = GetImage(item, numberedSuffix + i);
  254. if (image != null)
  255. {
  256. backdropFiles.Add(image.FullName);
  257. }
  258. else
  259. {
  260. unfound++;
  261. if (unfound >= 3)
  262. {
  263. break;
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }