ImageFromMediaLocationProvider.cs 11 KB

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