ImageFromMediaLocationProvider.cs 20 KB

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