ImageFromMediaLocationProvider.cs 10 KB

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