2
0

ImageFromMediaLocationProvider.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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.SetImagePath(ImageType.Logo, image.FullName);
  183. }
  184. // Clearart
  185. image = GetImage(item, args, "clearart");
  186. if (image != null)
  187. {
  188. item.SetImagePath(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.SetImagePath(ImageType.Disc, image.FullName);
  196. }
  197. // Box Image
  198. image = GetImage(item, args, "box");
  199. if (image != null)
  200. {
  201. item.SetImagePath(ImageType.Box, image.FullName);
  202. }
  203. // BoxRear Image
  204. image = GetImage(item, args, "boxrear");
  205. if (image != null)
  206. {
  207. item.SetImagePath(ImageType.BoxRear, image.FullName);
  208. }
  209. // Thumbnail Image
  210. image = GetImage(item, args, "menu");
  211. if (image != null)
  212. {
  213. item.SetImagePath(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.SetImagePath(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.SetImagePath(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. GetImage(item, args, "landscape");
  298. if (image == null)
  299. {
  300. var isFileSystemItem = item.LocationType == LocationType.FileSystem;
  301. // Supprt xbmc conventions
  302. var season = item as Season;
  303. if (season != null && item.IndexNumber.HasValue && isFileSystemItem)
  304. {
  305. image = GetSeasonImageFromSeriesFolder(season, "-landscape");
  306. }
  307. }
  308. if (image != null)
  309. {
  310. item.SetImagePath(ImageType.Thumb, image.FullName);
  311. }
  312. }
  313. /// <summary>
  314. /// Populates the backdrops.
  315. /// </summary>
  316. /// <param name="item">The item.</param>
  317. /// <param name="args">The args.</param>
  318. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args)
  319. {
  320. var isFileSystemItem = item.LocationType == LocationType.FileSystem;
  321. var backdropFiles = new List<string>();
  322. PopulateBackdrops(item, args, backdropFiles, "backdrop", "backdrop");
  323. // Support {name}-fanart.ext
  324. if (isFileSystemItem)
  325. {
  326. var name = Path.GetFileNameWithoutExtension(item.Path);
  327. if (!string.IsNullOrEmpty(name))
  328. {
  329. var image = GetImage(item, args, name + "-fanart");
  330. if (image != null)
  331. {
  332. backdropFiles.Add(image.FullName);
  333. }
  334. }
  335. }
  336. // Support plex/xbmc conventions
  337. PopulateBackdrops(item, args, backdropFiles, "fanart", "fanart-");
  338. PopulateBackdrops(item, args, backdropFiles, "background", "background-");
  339. PopulateBackdrops(item, args, backdropFiles, "art", "art-");
  340. var season = item as Season;
  341. if (season != null && item.IndexNumber.HasValue && isFileSystemItem)
  342. {
  343. var image = GetSeasonImageFromSeriesFolder(season, "-fanart");
  344. if (image != null)
  345. {
  346. backdropFiles.Add(image.FullName);
  347. }
  348. }
  349. if (isFileSystemItem)
  350. {
  351. PopulateBackdropsFromExtraFanart(args, backdropFiles);
  352. }
  353. if (backdropFiles.Count > 0)
  354. {
  355. item.BackdropImagePaths = backdropFiles;
  356. }
  357. }
  358. private FileSystemInfo GetSeasonImageFromSeriesFolder(Season season, string imageSuffix)
  359. {
  360. var series = season.Series;
  361. var seriesFolderArgs = series.ResolveArgs;
  362. var seasonNumber = season.IndexNumber;
  363. string filename = null;
  364. FileSystemInfo image;
  365. if (seasonNumber.HasValue)
  366. {
  367. var seasonMarker = seasonNumber.Value == 0
  368. ? "-specials"
  369. : seasonNumber.Value.ToString("00", _usCulture);
  370. // Get this one directly from the file system since we have to go up a level
  371. filename = "season" + seasonMarker + imageSuffix;
  372. image = GetImage(series, seriesFolderArgs, filename);
  373. if (image != null && image.Exists)
  374. {
  375. return image;
  376. }
  377. }
  378. var previousFilename = filename;
  379. // Try using the season name
  380. filename = season.Name.ToLower().Replace(" ", string.Empty) + imageSuffix;
  381. if (!string.Equals(previousFilename, filename))
  382. {
  383. image = GetImage(series, seriesFolderArgs, filename);
  384. if (image != null && image.Exists)
  385. {
  386. return image;
  387. }
  388. }
  389. return null;
  390. }
  391. /// <summary>
  392. /// Populates the backdrops from extra fanart.
  393. /// </summary>
  394. /// <param name="args">The args.</param>
  395. /// <param name="backdrops">The backdrops.</param>
  396. private void PopulateBackdropsFromExtraFanart(ItemResolveArgs args, List<string> backdrops)
  397. {
  398. if (!args.IsDirectory)
  399. {
  400. return;
  401. }
  402. if (args.ContainsFileSystemEntryByName("extrafanart"))
  403. {
  404. var path = Path.Combine(args.Path, "extrafanart");
  405. var imageFiles = Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)
  406. .Where(i =>
  407. {
  408. var extension = Path.GetExtension(i);
  409. if (string.IsNullOrEmpty(extension))
  410. {
  411. return false;
  412. }
  413. return BaseItem.SupportedImageExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  414. })
  415. .ToList();
  416. backdrops.AddRange(imageFiles);
  417. }
  418. }
  419. /// <summary>
  420. /// Populates the backdrops.
  421. /// </summary>
  422. /// <param name="item">The item.</param>
  423. /// <param name="args">The args.</param>
  424. /// <param name="backdropFiles">The backdrop files.</param>
  425. /// <param name="filename">The filename.</param>
  426. /// <param name="numberedSuffix">The numbered suffix.</param>
  427. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args, List<string> backdropFiles, string filename, string numberedSuffix)
  428. {
  429. var image = GetImage(item, args, filename);
  430. if (image != null)
  431. {
  432. backdropFiles.Add(image.FullName);
  433. }
  434. var unfound = 0;
  435. for (var i = 1; i <= 20; i++)
  436. {
  437. // Backdrop Image
  438. image = GetImage(item, args, numberedSuffix + i);
  439. if (image != null)
  440. {
  441. backdropFiles.Add(image.FullName);
  442. }
  443. else
  444. {
  445. unfound++;
  446. if (unfound >= 3)
  447. {
  448. break;
  449. }
  450. }
  451. }
  452. }
  453. /// <summary>
  454. /// Populates the screenshots.
  455. /// </summary>
  456. /// <param name="item">The item.</param>
  457. /// <param name="args">The args.</param>
  458. private void PopulateScreenshots(BaseItem item, ItemResolveArgs args)
  459. {
  460. // Screenshot Image
  461. var image = GetImage(item, args, "screenshot");
  462. var screenshotFiles = new List<string>();
  463. if (image != null)
  464. {
  465. screenshotFiles.Add(image.FullName);
  466. }
  467. var unfound = 0;
  468. for (var i = 1; i <= 20; i++)
  469. {
  470. // Screenshot Image
  471. image = GetImage(item, args, "screenshot" + i);
  472. if (image != null)
  473. {
  474. screenshotFiles.Add(image.FullName);
  475. }
  476. else
  477. {
  478. unfound++;
  479. if (unfound >= 3)
  480. {
  481. break;
  482. }
  483. }
  484. }
  485. if (screenshotFiles.Count > 0)
  486. {
  487. var hasScreenshots = item as IHasScreenshots;
  488. if (hasScreenshots != null)
  489. {
  490. hasScreenshots.ScreenshotImagePaths = screenshotFiles;
  491. }
  492. }
  493. }
  494. protected FileSystemInfo GetImageFromLocation(string path, string filenameWithoutExtension)
  495. {
  496. try
  497. {
  498. var files = new DirectoryInfo(path)
  499. .EnumerateFiles()
  500. .Where(i =>
  501. {
  502. var fileName = Path.GetFileNameWithoutExtension(i.FullName);
  503. if (!string.Equals(fileName, filenameWithoutExtension, StringComparison.OrdinalIgnoreCase))
  504. {
  505. return false;
  506. }
  507. var ext = i.Extension;
  508. return !string.IsNullOrEmpty(ext) &&
  509. BaseItem.SupportedImageExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  510. })
  511. .ToList();
  512. return BaseItem.SupportedImageExtensions
  513. .Select(ext => files.FirstOrDefault(i => string.Equals(ext, i.Extension, StringComparison.OrdinalIgnoreCase)))
  514. .FirstOrDefault(file => file != null);
  515. }
  516. catch (DirectoryNotFoundException)
  517. {
  518. return null;
  519. }
  520. }
  521. }
  522. }