ImageFromMediaLocationProvider.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 string GetFullImagePath(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension, string extension)
  136. {
  137. var path = item.MetaLocation;
  138. if (item.IsInMixedFolder)
  139. {
  140. var pathFilenameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path);
  141. // If the image filename and path file name match, just look for an image using the same full path as the item
  142. if (string.Equals(pathFilenameWithoutExtension, filenameWithoutExtension))
  143. {
  144. return Path.ChangeExtension(item.Path, extension);
  145. }
  146. return Path.Combine(path, pathFilenameWithoutExtension + "-" + filenameWithoutExtension + extension);
  147. }
  148. return Path.Combine(path, filenameWithoutExtension + extension);
  149. }
  150. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  151. /// <summary>
  152. /// Fills in image paths based on files win the folder
  153. /// </summary>
  154. /// <param name="item">The item.</param>
  155. /// <param name="args">The args.</param>
  156. private void PopulateBaseItemImages(BaseItem item, ItemResolveArgs args)
  157. {
  158. PopulatePrimaryImage(item, args);
  159. // Logo Image
  160. var image = GetImage(item, args, "logo");
  161. if (image != null)
  162. {
  163. item.SetImage(ImageType.Logo, image.FullName);
  164. }
  165. // Clearart
  166. image = GetImage(item, args, "clearart");
  167. if (image != null)
  168. {
  169. item.SetImage(ImageType.Art, image.FullName);
  170. }
  171. // Disc
  172. image = GetImage(item, args, "disc") ??
  173. GetImage(item, args, "cdart");
  174. if (image != null)
  175. {
  176. item.SetImage(ImageType.Disc, image.FullName);
  177. }
  178. // Box Image
  179. image = GetImage(item, args, "box");
  180. if (image != null)
  181. {
  182. item.SetImage(ImageType.Box, image.FullName);
  183. }
  184. // BoxRear Image
  185. image = GetImage(item, args, "boxrear");
  186. if (image != null)
  187. {
  188. item.SetImage(ImageType.BoxRear, image.FullName);
  189. }
  190. // Thumbnail Image
  191. image = GetImage(item, args, "menu");
  192. if (image != null)
  193. {
  194. item.SetImage(ImageType.Menu, image.FullName);
  195. }
  196. PopulateBanner(item, args);
  197. PopulateThumb(item, args);
  198. // Backdrop Image
  199. PopulateBackdrops(item, args);
  200. PopulateScreenshots(item, args);
  201. }
  202. private void PopulatePrimaryImage(BaseItem item, ItemResolveArgs args)
  203. {
  204. // Primary Image
  205. var image = GetImage(item, args, "folder") ??
  206. GetImage(item, args, "poster") ??
  207. GetImage(item, args, "cover") ??
  208. GetImage(item, args, "default");
  209. // Support plex/xbmc convention
  210. if (image == null && item is Series)
  211. {
  212. image = GetImage(item, args, "show");
  213. }
  214. var isFileSystemItem = item.LocationType == LocationType.FileSystem;
  215. // Support plex/xbmc convention
  216. if (image == null && item is Season && item.IndexNumber.HasValue && isFileSystemItem)
  217. {
  218. var seasonMarker = item.IndexNumber.Value == 0
  219. ? "-specials"
  220. : item.IndexNumber.Value.ToString("00", _usCulture);
  221. // Get this one directly from the file system since we have to go up a level
  222. var filename = "season" + seasonMarker + "-poster";
  223. var path = Path.GetDirectoryName(item.Path);
  224. path = Path.Combine(path, filename);
  225. image = new FileInfo(path);
  226. if (!image.Exists)
  227. {
  228. image = null;
  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. // Supprt xbmc conventions
  263. if (item is Season && item.IndexNumber.HasValue && item.LocationType == LocationType.FileSystem)
  264. {
  265. var seasonMarker = item.IndexNumber.Value == 0
  266. ? "-specials"
  267. : item.IndexNumber.Value.ToString("00", _usCulture);
  268. // Get this one directly from the file system since we have to go up a level
  269. var filename = "season" + seasonMarker + "-banner";
  270. var path = Path.GetDirectoryName(item.Path);
  271. path = Path.Combine(path, filename);
  272. image = new FileInfo(path);
  273. if (!image.Exists)
  274. {
  275. image = null;
  276. }
  277. }
  278. }
  279. if (image != null)
  280. {
  281. item.SetImage(ImageType.Banner, image.FullName);
  282. }
  283. }
  284. /// <summary>
  285. /// Populates the thumb.
  286. /// </summary>
  287. /// <param name="item">The item.</param>
  288. /// <param name="args">The args.</param>
  289. private void PopulateThumb(BaseItem item, ItemResolveArgs args)
  290. {
  291. // Thumbnail Image
  292. var image = GetImage(item, args, "thumb");
  293. if (image == null)
  294. {
  295. // Supprt xbmc conventions
  296. if (item is Season && item.IndexNumber.HasValue && item.LocationType == LocationType.FileSystem)
  297. {
  298. var seasonMarker = item.IndexNumber.Value == 0
  299. ? "-specials"
  300. : item.IndexNumber.Value.ToString("00", _usCulture);
  301. // Get this one directly from the file system since we have to go up a level
  302. var filename = "season" + seasonMarker + "-landscape";
  303. var path = Path.GetDirectoryName(item.Path);
  304. path = Path.Combine(path, filename);
  305. image = new FileInfo(path);
  306. if (!image.Exists)
  307. {
  308. image = null;
  309. }
  310. }
  311. }
  312. if (image != null)
  313. {
  314. item.SetImage(ImageType.Thumb, image.FullName);
  315. }
  316. }
  317. /// <summary>
  318. /// Populates the backdrops.
  319. /// </summary>
  320. /// <param name="item">The item.</param>
  321. /// <param name="args">The args.</param>
  322. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args)
  323. {
  324. var isFileSystemItem = item.LocationType == LocationType.FileSystem;
  325. var backdropFiles = new List<string>();
  326. PopulateBackdrops(item, args, backdropFiles, "backdrop", "backdrop");
  327. // Support {name}-fanart.ext
  328. if (isFileSystemItem)
  329. {
  330. var name = Path.GetFileNameWithoutExtension(item.Path);
  331. if (!string.IsNullOrEmpty(name))
  332. {
  333. var image = GetImage(item, args, name + "-fanart");
  334. if (image != null)
  335. {
  336. backdropFiles.Add(image.FullName);
  337. }
  338. }
  339. }
  340. // Support plex/xbmc conventions
  341. PopulateBackdrops(item, args, backdropFiles, "fanart", "fanart-");
  342. PopulateBackdrops(item, args, backdropFiles, "background", "background-");
  343. PopulateBackdrops(item, args, backdropFiles, "art", "art-");
  344. if (item is Season && item.IndexNumber.HasValue && isFileSystemItem)
  345. {
  346. var seasonMarker = item.IndexNumber.Value == 0
  347. ? "-specials"
  348. : item.IndexNumber.Value.ToString("00", _usCulture);
  349. // Get this one directly from the file system since we have to go up a level
  350. var filename = "season" + seasonMarker + "-fanart";
  351. var path = Path.GetDirectoryName(item.Path);
  352. path = Path.Combine(path, filename);
  353. var image = new FileInfo(path);
  354. if (image.Exists)
  355. {
  356. backdropFiles.Add(image.FullName);
  357. }
  358. }
  359. if (isFileSystemItem)
  360. {
  361. PopulateBackdropsFromExtraFanart(args, backdropFiles);
  362. }
  363. if (backdropFiles.Count > 0)
  364. {
  365. item.BackdropImagePaths = backdropFiles;
  366. }
  367. }
  368. /// <summary>
  369. /// Populates the backdrops from extra fanart.
  370. /// </summary>
  371. /// <param name="args">The args.</param>
  372. /// <param name="backdrops">The backdrops.</param>
  373. private void PopulateBackdropsFromExtraFanart(ItemResolveArgs args, List<string> backdrops)
  374. {
  375. if (!args.IsDirectory)
  376. {
  377. return;
  378. }
  379. if (args.ContainsFileSystemEntryByName("extrafanart"))
  380. {
  381. var path = Path.Combine(args.Path, "extrafanart");
  382. var imageFiles = Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)
  383. .Where(i =>
  384. {
  385. var extension = Path.GetExtension(i);
  386. if (string.IsNullOrEmpty(extension))
  387. {
  388. return false;
  389. }
  390. return BaseItem.SupportedImageExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  391. })
  392. .ToList();
  393. backdrops.AddRange(imageFiles);
  394. }
  395. }
  396. /// <summary>
  397. /// Populates the backdrops.
  398. /// </summary>
  399. /// <param name="item">The item.</param>
  400. /// <param name="args">The args.</param>
  401. /// <param name="backdropFiles">The backdrop files.</param>
  402. /// <param name="filename">The filename.</param>
  403. /// <param name="numberedSuffix">The numbered suffix.</param>
  404. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args, List<string> backdropFiles, string filename, string numberedSuffix)
  405. {
  406. var image = GetImage(item, args, filename);
  407. if (image != null)
  408. {
  409. backdropFiles.Add(image.FullName);
  410. }
  411. var unfound = 0;
  412. for (var i = 1; i <= 20; i++)
  413. {
  414. // Backdrop Image
  415. image = GetImage(item, args, numberedSuffix + i);
  416. if (image != null)
  417. {
  418. backdropFiles.Add(image.FullName);
  419. }
  420. else
  421. {
  422. unfound++;
  423. if (unfound >= 3)
  424. {
  425. break;
  426. }
  427. }
  428. }
  429. }
  430. /// <summary>
  431. /// Populates the screenshots.
  432. /// </summary>
  433. /// <param name="item">The item.</param>
  434. /// <param name="args">The args.</param>
  435. private void PopulateScreenshots(BaseItem item, ItemResolveArgs args)
  436. {
  437. // Screenshot Image
  438. var image = GetImage(item, args, "screenshot");
  439. var screenshotFiles = new List<string>();
  440. if (image != null)
  441. {
  442. screenshotFiles.Add(image.FullName);
  443. }
  444. var unfound = 0;
  445. for (var i = 1; i <= 20; i++)
  446. {
  447. // Screenshot Image
  448. image = GetImage(item, args, "screenshot" + i);
  449. if (image != null)
  450. {
  451. screenshotFiles.Add(image.FullName);
  452. }
  453. else
  454. {
  455. unfound++;
  456. if (unfound >= 3)
  457. {
  458. break;
  459. }
  460. }
  461. }
  462. if (screenshotFiles.Count > 0)
  463. {
  464. var hasScreenshots = item as IHasScreenshots;
  465. if (hasScreenshots != null)
  466. {
  467. hasScreenshots.ScreenshotImagePaths = screenshotFiles;
  468. }
  469. }
  470. }
  471. protected FileSystemInfo GetImageFromLocation(string path, string filenameWithoutExtension)
  472. {
  473. try
  474. {
  475. var files = new DirectoryInfo(path)
  476. .EnumerateFiles()
  477. .Where(i =>
  478. {
  479. var fileName = Path.GetFileNameWithoutExtension(i.FullName);
  480. if (!string.Equals(fileName, filenameWithoutExtension, StringComparison.OrdinalIgnoreCase))
  481. {
  482. return false;
  483. }
  484. var ext = i.Extension;
  485. return !string.IsNullOrEmpty(ext) &&
  486. BaseItem.SupportedImageExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  487. })
  488. .ToList();
  489. return BaseItem.SupportedImageExtensions
  490. .Select(ext => files.FirstOrDefault(i => string.Equals(ext, i.Extension, StringComparison.OrdinalIgnoreCase)))
  491. .FirstOrDefault(file => file != null);
  492. }
  493. catch (DirectoryNotFoundException)
  494. {
  495. return null;
  496. }
  497. }
  498. }
  499. }