ImageFromMediaLocationProvider.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. var isFileSystemItem = item.LocationType == LocationType.FileSystem;
  208. // Support plex/xbmc convention
  209. if (image == null && item is Season && item.IndexNumber.HasValue && isFileSystemItem)
  210. {
  211. var seasonMarker = item.IndexNumber.Value == 0
  212. ? "-specials"
  213. : item.IndexNumber.Value.ToString("00", _usCulture);
  214. // Get this one directly from the file system since we have to go up a level
  215. var filename = "season" + seasonMarker + "-poster";
  216. var path = Path.GetDirectoryName(item.Path);
  217. path = Path.Combine(path, filename);
  218. image = new FileInfo(path);
  219. if (!image.Exists)
  220. {
  221. image = null;
  222. }
  223. }
  224. // Support plex/xbmc convention
  225. if (image == null && (item is Movie || item is MusicVideo || item is AdultVideo))
  226. {
  227. image = GetImage(item, args, "movie");
  228. }
  229. // Look for a file with the same name as the item
  230. if (image == null && isFileSystemItem)
  231. {
  232. var name = Path.GetFileNameWithoutExtension(item.Path);
  233. if (!string.IsNullOrEmpty(name))
  234. {
  235. image = GetImage(item, args, name) ??
  236. GetImage(item, args, name + "-poster");
  237. }
  238. }
  239. if (image != null)
  240. {
  241. item.SetImage(ImageType.Primary, image.FullName);
  242. }
  243. }
  244. /// <summary>
  245. /// Populates the banner.
  246. /// </summary>
  247. /// <param name="item">The item.</param>
  248. /// <param name="args">The args.</param>
  249. private void PopulateBanner(BaseItem item, ItemResolveArgs args)
  250. {
  251. // Banner Image
  252. var image = GetImage(item, args, "banner");
  253. if (image == null)
  254. {
  255. // Supprt xbmc conventions
  256. if (item is Season && item.IndexNumber.HasValue && item.LocationType == LocationType.FileSystem)
  257. {
  258. var seasonMarker = item.IndexNumber.Value == 0
  259. ? "-specials"
  260. : item.IndexNumber.Value.ToString("00", _usCulture);
  261. // Get this one directly from the file system since we have to go up a level
  262. var filename = "season" + seasonMarker + "-banner";
  263. var path = Path.GetDirectoryName(item.Path);
  264. path = Path.Combine(path, filename);
  265. image = new FileInfo(path);
  266. if (!image.Exists)
  267. {
  268. image = null;
  269. }
  270. }
  271. }
  272. if (image != null)
  273. {
  274. item.SetImage(ImageType.Banner, image.FullName);
  275. }
  276. }
  277. /// <summary>
  278. /// Populates the thumb.
  279. /// </summary>
  280. /// <param name="item">The item.</param>
  281. /// <param name="args">The args.</param>
  282. private void PopulateThumb(BaseItem item, ItemResolveArgs args)
  283. {
  284. // Thumbnail Image
  285. var image = GetImage(item, args, "thumb");
  286. if (image == null)
  287. {
  288. // Supprt xbmc conventions
  289. if (item is Season && item.IndexNumber.HasValue && item.LocationType == LocationType.FileSystem)
  290. {
  291. var seasonMarker = item.IndexNumber.Value == 0
  292. ? "-specials"
  293. : item.IndexNumber.Value.ToString("00", _usCulture);
  294. // Get this one directly from the file system since we have to go up a level
  295. var filename = "season" + seasonMarker + "-landscape";
  296. var path = Path.GetDirectoryName(item.Path);
  297. path = Path.Combine(path, filename);
  298. image = new FileInfo(path);
  299. if (!image.Exists)
  300. {
  301. image = null;
  302. }
  303. }
  304. }
  305. if (image != null)
  306. {
  307. item.SetImage(ImageType.Thumb, image.FullName);
  308. }
  309. }
  310. /// <summary>
  311. /// Populates the backdrops.
  312. /// </summary>
  313. /// <param name="item">The item.</param>
  314. /// <param name="args">The args.</param>
  315. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args)
  316. {
  317. var backdropFiles = new List<string>();
  318. PopulateBackdrops(item, args, backdropFiles, "backdrop", "backdrop");
  319. // Support plex/xbmc conventions
  320. PopulateBackdrops(item, args, backdropFiles, "fanart", "fanart-");
  321. PopulateBackdrops(item, args, backdropFiles, "background", "background-");
  322. PopulateBackdrops(item, args, backdropFiles, "art", "art-");
  323. var isFileSystemItem = item.LocationType == LocationType.FileSystem;
  324. if (item is Season && item.IndexNumber.HasValue && isFileSystemItem)
  325. {
  326. var seasonMarker = item.IndexNumber.Value == 0
  327. ? "-specials"
  328. : item.IndexNumber.Value.ToString("00", _usCulture);
  329. // Get this one directly from the file system since we have to go up a level
  330. var filename = "season" + seasonMarker + "-fanart";
  331. var path = Path.GetDirectoryName(item.Path);
  332. path = Path.Combine(path, filename);
  333. var image = new FileInfo(path);
  334. if (image.Exists)
  335. {
  336. backdropFiles.Add(image.FullName);
  337. }
  338. }
  339. if (isFileSystemItem)
  340. {
  341. PopulateBackdropsFromExtraFanart(args, backdropFiles);
  342. }
  343. if (backdropFiles.Count > 0)
  344. {
  345. item.BackdropImagePaths = backdropFiles;
  346. }
  347. }
  348. /// <summary>
  349. /// Populates the backdrops from extra fanart.
  350. /// </summary>
  351. /// <param name="args">The args.</param>
  352. /// <param name="backdrops">The backdrops.</param>
  353. private void PopulateBackdropsFromExtraFanart(ItemResolveArgs args, List<string> backdrops)
  354. {
  355. if (!args.IsDirectory)
  356. {
  357. return;
  358. }
  359. if (args.ContainsFileSystemEntryByName("extrafanart"))
  360. {
  361. var path = Path.Combine(args.Path, "extrafanart");
  362. var imageFiles = Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)
  363. .Where(i =>
  364. {
  365. var extension = Path.GetExtension(i);
  366. if (string.IsNullOrEmpty(extension))
  367. {
  368. return false;
  369. }
  370. return BaseItem.SupportedImageExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  371. })
  372. .ToList();
  373. backdrops.AddRange(imageFiles);
  374. }
  375. }
  376. /// <summary>
  377. /// Populates the backdrops.
  378. /// </summary>
  379. /// <param name="item">The item.</param>
  380. /// <param name="args">The args.</param>
  381. /// <param name="backdropFiles">The backdrop files.</param>
  382. /// <param name="filename">The filename.</param>
  383. /// <param name="numberedSuffix">The numbered suffix.</param>
  384. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args, List<string> backdropFiles, string filename, string numberedSuffix)
  385. {
  386. var image = GetImage(item, args, filename);
  387. if (image != null)
  388. {
  389. backdropFiles.Add(image.FullName);
  390. }
  391. var unfound = 0;
  392. for (var i = 1; i <= 20; i++)
  393. {
  394. // Backdrop Image
  395. image = GetImage(item, args, numberedSuffix + i);
  396. if (image != null)
  397. {
  398. backdropFiles.Add(image.FullName);
  399. }
  400. else
  401. {
  402. unfound++;
  403. if (unfound >= 3)
  404. {
  405. break;
  406. }
  407. }
  408. }
  409. }
  410. /// <summary>
  411. /// Populates the screenshots.
  412. /// </summary>
  413. /// <param name="item">The item.</param>
  414. /// <param name="args">The args.</param>
  415. private void PopulateScreenshots(BaseItem item, ItemResolveArgs args)
  416. {
  417. // Screenshot Image
  418. var image = GetImage(item, args, "screenshot");
  419. var screenshotFiles = new List<string>();
  420. if (image != null)
  421. {
  422. screenshotFiles.Add(image.FullName);
  423. }
  424. var unfound = 0;
  425. for (var i = 1; i <= 20; i++)
  426. {
  427. // Screenshot Image
  428. image = GetImage(item, args, "screenshot" + i);
  429. if (image != null)
  430. {
  431. screenshotFiles.Add(image.FullName);
  432. }
  433. else
  434. {
  435. unfound++;
  436. if (unfound >= 3)
  437. {
  438. break;
  439. }
  440. }
  441. }
  442. if (screenshotFiles.Count > 0)
  443. {
  444. item.ScreenshotImagePaths = screenshotFiles;
  445. }
  446. }
  447. }
  448. }