ImageFromMediaLocationProvider.cs 14 KB

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