ImageFromMediaLocationProvider.cs 20 KB

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