ImageFromMediaLocationProvider.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Movies;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace MediaBrowser.Providers
  17. {
  18. /// <summary>
  19. /// Provides images for all types by looking for standard images - folder, backdrop, logo, etc.
  20. /// </summary>
  21. public class ImageFromMediaLocationProvider : BaseMetadataProvider
  22. {
  23. public ImageFromMediaLocationProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
  24. : base(logManager, configurationManager)
  25. {
  26. }
  27. public override ItemUpdateType ItemUpdateType
  28. {
  29. get
  30. {
  31. return ItemUpdateType.ImageUpdate;
  32. }
  33. }
  34. /// <summary>
  35. /// Supportses the specified item.
  36. /// </summary>
  37. /// <param name="item">The item.</param>
  38. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  39. public override bool Supports(BaseItem item)
  40. {
  41. if (item.LocationType == LocationType.FileSystem)
  42. {
  43. if (item.ResolveArgs.IsDirectory)
  44. {
  45. return true;
  46. }
  47. return item.IsInMixedFolder && item.Parent != null && !(item is Episode);
  48. }
  49. return false;
  50. }
  51. /// <summary>
  52. /// Gets the priority.
  53. /// </summary>
  54. /// <value>The priority.</value>
  55. public override MetadataProviderPriority Priority
  56. {
  57. get { return MetadataProviderPriority.First; }
  58. }
  59. /// <summary>
  60. /// Returns true or false indicating if the provider should refresh when the contents of it's directory changes
  61. /// </summary>
  62. /// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
  63. protected override bool RefreshOnFileSystemStampChange
  64. {
  65. get
  66. {
  67. return true;
  68. }
  69. }
  70. /// <summary>
  71. /// Gets the filestamp extensions.
  72. /// </summary>
  73. /// <value>The filestamp extensions.</value>
  74. protected override string[] FilestampExtensions
  75. {
  76. get
  77. {
  78. return BaseItem.SupportedImageExtensions;
  79. }
  80. }
  81. /// <summary>
  82. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  83. /// </summary>
  84. /// <param name="item">The item.</param>
  85. /// <param name="force">if set to <c>true</c> [force].</param>
  86. /// <param name="cancellationToken">The cancellation token.</param>
  87. /// <returns>Task{System.Boolean}.</returns>
  88. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  89. {
  90. cancellationToken.ThrowIfCancellationRequested();
  91. var args = GetResolveArgsContainingImages(item);
  92. // Make sure current image paths still exist
  93. item.ValidateImages();
  94. cancellationToken.ThrowIfCancellationRequested();
  95. // Make sure current backdrop paths still exist
  96. item.ValidateBackdrops();
  97. item.ValidateScreenshots();
  98. cancellationToken.ThrowIfCancellationRequested();
  99. PopulateBaseItemImages(item, args);
  100. SetLastRefreshed(item, DateTime.UtcNow);
  101. return TrueTaskResult;
  102. }
  103. private ItemResolveArgs GetResolveArgsContainingImages(BaseItem item)
  104. {
  105. if (item.IsInMixedFolder)
  106. {
  107. if (item.Parent == null)
  108. {
  109. return item.ResolveArgs;
  110. }
  111. return item.Parent.ResolveArgs;
  112. }
  113. return item.ResolveArgs;
  114. }
  115. /// <summary>
  116. /// Gets the image.
  117. /// </summary>
  118. /// <param name="item">The item.</param>
  119. /// <param name="args">The args.</param>
  120. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  121. /// <returns>FileSystemInfo.</returns>
  122. protected virtual FileSystemInfo GetImage(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension)
  123. {
  124. return BaseItem.SupportedImageExtensions
  125. .Select(i => args.GetMetaFileByPath(GetFullImagePath(item, args, filenameWithoutExtension, i)))
  126. .FirstOrDefault(i => i != null);
  127. }
  128. protected virtual string GetFullImagePath(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension, string extension)
  129. {
  130. var path = item.MetaLocation;
  131. if (item.IsInMixedFolder)
  132. {
  133. var pathFilenameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path);
  134. // If the image filename and path file name match, just look for an image using the same full path as the item
  135. if (string.Equals(pathFilenameWithoutExtension, filenameWithoutExtension))
  136. {
  137. return Path.ChangeExtension(item.Path, extension);
  138. }
  139. return Path.Combine(path, pathFilenameWithoutExtension + "-" + filenameWithoutExtension + extension);
  140. }
  141. return Path.Combine(path, filenameWithoutExtension + extension);
  142. }
  143. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  144. /// <summary>
  145. /// Fills in image paths based on files win the folder
  146. /// </summary>
  147. /// <param name="item">The item.</param>
  148. /// <param name="args">The args.</param>
  149. private void PopulateBaseItemImages(BaseItem item, ItemResolveArgs args)
  150. {
  151. PopulatePrimaryImage(item, args);
  152. // Logo Image
  153. var image = GetImage(item, args, "logo");
  154. if (image != null)
  155. {
  156. item.SetImage(ImageType.Logo, image.FullName);
  157. }
  158. // Clearart
  159. image = GetImage(item, args, "clearart");
  160. if (image != null)
  161. {
  162. item.SetImage(ImageType.Art, image.FullName);
  163. }
  164. // Disc
  165. image = GetImage(item, args, "disc") ??
  166. GetImage(item, args, "cdart");
  167. if (image != null)
  168. {
  169. item.SetImage(ImageType.Disc, image.FullName);
  170. }
  171. // Box Image
  172. image = GetImage(item, args, "box");
  173. if (image != null)
  174. {
  175. item.SetImage(ImageType.Box, image.FullName);
  176. }
  177. // BoxRear Image
  178. image = GetImage(item, args, "boxrear");
  179. if (image != null)
  180. {
  181. item.SetImage(ImageType.BoxRear, image.FullName);
  182. }
  183. // Thumbnail Image
  184. image = GetImage(item, args, "menu");
  185. if (image != null)
  186. {
  187. item.SetImage(ImageType.Menu, image.FullName);
  188. }
  189. PopulateBanner(item, args);
  190. PopulateThumb(item, args);
  191. // Backdrop Image
  192. PopulateBackdrops(item, args);
  193. PopulateScreenshots(item, args);
  194. }
  195. private void PopulatePrimaryImage(BaseItem item, ItemResolveArgs args)
  196. {
  197. // Primary Image
  198. var image = GetImage(item, args, "folder") ??
  199. GetImage(item, args, "poster") ??
  200. GetImage(item, args, "cover") ??
  201. GetImage(item, args, "default");
  202. // Support plex/xbmc convention
  203. if (image == null && item is Series)
  204. {
  205. image = GetImage(item, args, "show");
  206. }
  207. // Support plex/xbmc convention
  208. if (image == null && item is Season && item.IndexNumber.HasValue)
  209. {
  210. var seasonMarker = item.IndexNumber.Value == 0
  211. ? "-specials"
  212. : item.IndexNumber.Value.ToString("00", _usCulture);
  213. // Get this one directly from the file system since we have to go up a level
  214. var filename = "season" + seasonMarker + "-poster";
  215. var path = Path.GetDirectoryName(item.Path);
  216. path = Path.Combine(path, filename);
  217. image = new FileInfo(path);
  218. if (!image.Exists)
  219. {
  220. image = null;
  221. }
  222. }
  223. // Support plex/xbmc convention
  224. if (image == null && (item is Movie || item is MusicVideo || item is AdultVideo))
  225. {
  226. image = GetImage(item, args, "movie");
  227. }
  228. // Look for a file with the same name as the item
  229. if (image == null)
  230. {
  231. var name = Path.GetFileNameWithoutExtension(item.Path);
  232. if (!string.IsNullOrEmpty(name))
  233. {
  234. image = GetImage(item, args, name) ??
  235. GetImage(item, args, name + "-poster");
  236. }
  237. }
  238. if (image != null)
  239. {
  240. item.SetImage(ImageType.Primary, image.FullName);
  241. }
  242. }
  243. /// <summary>
  244. /// Populates the banner.
  245. /// </summary>
  246. /// <param name="item">The item.</param>
  247. /// <param name="args">The args.</param>
  248. private void PopulateBanner(BaseItem item, ItemResolveArgs args)
  249. {
  250. // Banner Image
  251. var image = GetImage(item, args, "banner");
  252. if (image == null)
  253. {
  254. // Supprt xbmc conventions
  255. if (item is Season && item.IndexNumber.HasValue)
  256. {
  257. var seasonMarker = item.IndexNumber.Value == 0
  258. ? "-specials"
  259. : item.IndexNumber.Value.ToString("00", _usCulture);
  260. // Get this one directly from the file system since we have to go up a level
  261. var filename = "season" + seasonMarker + "-banner";
  262. var path = Path.GetDirectoryName(item.Path);
  263. path = Path.Combine(path, filename);
  264. image = new FileInfo(path);
  265. if (!image.Exists)
  266. {
  267. image = null;
  268. }
  269. }
  270. }
  271. if (image != null)
  272. {
  273. item.SetImage(ImageType.Banner, image.FullName);
  274. }
  275. }
  276. /// <summary>
  277. /// Populates the thumb.
  278. /// </summary>
  279. /// <param name="item">The item.</param>
  280. /// <param name="args">The args.</param>
  281. private void PopulateThumb(BaseItem item, ItemResolveArgs args)
  282. {
  283. // Thumbnail Image
  284. var image = GetImage(item, args, "thumb");
  285. if (image == null)
  286. {
  287. // Supprt xbmc conventions
  288. if (item is Season && item.IndexNumber.HasValue)
  289. {
  290. var seasonMarker = item.IndexNumber.Value == 0
  291. ? "-specials"
  292. : item.IndexNumber.Value.ToString("00", _usCulture);
  293. // Get this one directly from the file system since we have to go up a level
  294. var filename = "season" + seasonMarker + "-landscape";
  295. var path = Path.GetDirectoryName(item.Path);
  296. path = Path.Combine(path, filename);
  297. image = new FileInfo(path);
  298. if (!image.Exists)
  299. {
  300. image = null;
  301. }
  302. }
  303. }
  304. if (image != null)
  305. {
  306. item.SetImage(ImageType.Thumb, image.FullName);
  307. }
  308. }
  309. /// <summary>
  310. /// Populates the backdrops.
  311. /// </summary>
  312. /// <param name="item">The item.</param>
  313. /// <param name="args">The args.</param>
  314. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args)
  315. {
  316. var backdropFiles = new List<string>();
  317. PopulateBackdrops(item, args, backdropFiles, "backdrop", "backdrop");
  318. // Support plex/xbmc conventions
  319. PopulateBackdrops(item, args, backdropFiles, "fanart", "fanart-");
  320. PopulateBackdrops(item, args, backdropFiles, "background", "background-");
  321. PopulateBackdrops(item, args, backdropFiles, "art", "art-");
  322. if (item is Season && item.IndexNumber.HasValue)
  323. {
  324. var seasonMarker = item.IndexNumber.Value == 0
  325. ? "-specials"
  326. : item.IndexNumber.Value.ToString("00", _usCulture);
  327. // Get this one directly from the file system since we have to go up a level
  328. var filename = "season" + seasonMarker + "-fanart";
  329. var path = Path.GetDirectoryName(item.Path);
  330. path = Path.Combine(path, filename);
  331. var image = new FileInfo(path);
  332. if (image.Exists)
  333. {
  334. backdropFiles.Add(image.FullName);
  335. }
  336. }
  337. PopulateBackdropsFromExtraFanart(args, backdropFiles);
  338. if (backdropFiles.Count > 0)
  339. {
  340. item.BackdropImagePaths = backdropFiles;
  341. }
  342. }
  343. /// <summary>
  344. /// Populates the backdrops from extra fanart.
  345. /// </summary>
  346. /// <param name="args">The args.</param>
  347. /// <param name="backdrops">The backdrops.</param>
  348. private void PopulateBackdropsFromExtraFanart(ItemResolveArgs args, List<string> backdrops)
  349. {
  350. if (!args.IsDirectory)
  351. {
  352. return;
  353. }
  354. if (args.ContainsFileSystemEntryByName("extrafanart"))
  355. {
  356. var path = Path.Combine(args.Path, "extrafanart");
  357. var imageFiles = Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)
  358. .Where(i =>
  359. {
  360. var extension = Path.GetExtension(i);
  361. if (string.IsNullOrEmpty(extension))
  362. {
  363. return false;
  364. }
  365. return BaseItem.SupportedImageExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  366. })
  367. .ToList();
  368. backdrops.AddRange(imageFiles);
  369. }
  370. }
  371. /// <summary>
  372. /// Populates the backdrops.
  373. /// </summary>
  374. /// <param name="item">The item.</param>
  375. /// <param name="args">The args.</param>
  376. /// <param name="backdropFiles">The backdrop files.</param>
  377. /// <param name="filename">The filename.</param>
  378. /// <param name="numberedSuffix">The numbered suffix.</param>
  379. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args, List<string> backdropFiles, string filename, string numberedSuffix)
  380. {
  381. var image = GetImage(item, args, filename);
  382. if (image != null)
  383. {
  384. backdropFiles.Add(image.FullName);
  385. }
  386. var unfound = 0;
  387. for (var i = 1; i <= 20; i++)
  388. {
  389. // Backdrop Image
  390. image = GetImage(item, args, numberedSuffix + i);
  391. if (image != null)
  392. {
  393. backdropFiles.Add(image.FullName);
  394. }
  395. else
  396. {
  397. unfound++;
  398. if (unfound >= 3)
  399. {
  400. break;
  401. }
  402. }
  403. }
  404. }
  405. /// <summary>
  406. /// Populates the screenshots.
  407. /// </summary>
  408. /// <param name="item">The item.</param>
  409. /// <param name="args">The args.</param>
  410. private void PopulateScreenshots(BaseItem item, ItemResolveArgs args)
  411. {
  412. // Screenshot Image
  413. var image = GetImage(item, args, "screenshot");
  414. var screenshotFiles = new List<string>();
  415. if (image != null)
  416. {
  417. screenshotFiles.Add(image.FullName);
  418. }
  419. var unfound = 0;
  420. for (var i = 1; i <= 20; i++)
  421. {
  422. // Screenshot Image
  423. image = GetImage(item, args, "screenshot" + i);
  424. if (image != null)
  425. {
  426. screenshotFiles.Add(image.FullName);
  427. }
  428. else
  429. {
  430. unfound++;
  431. if (unfound >= 3)
  432. {
  433. break;
  434. }
  435. }
  436. }
  437. if (screenshotFiles.Count > 0)
  438. {
  439. item.ScreenshotImagePaths = screenshotFiles;
  440. }
  441. }
  442. }
  443. }