ImageFromMediaLocationProvider.cs 20 KB

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