ImageFromMediaLocationProvider.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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, 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. item.ValidateScreenshots();
  100. cancellationToken.ThrowIfCancellationRequested();
  101. var args = GetResolveArgsContainingImages(item);
  102. PopulateBaseItemImages(item, args);
  103. SetLastRefreshed(item, DateTime.UtcNow);
  104. return TrueTaskResult;
  105. }
  106. private ItemResolveArgs GetResolveArgsContainingImages(BaseItem item)
  107. {
  108. if (item.IsInMixedFolder)
  109. {
  110. if (item.Parent == null)
  111. {
  112. return item.ResolveArgs;
  113. }
  114. return item.Parent.ResolveArgs;
  115. }
  116. return item.ResolveArgs;
  117. }
  118. /// <summary>
  119. /// Gets the image.
  120. /// </summary>
  121. /// <param name="item">The item.</param>
  122. /// <param name="args">The args.</param>
  123. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  124. /// <returns>FileSystemInfo.</returns>
  125. protected virtual FileSystemInfo GetImage(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension)
  126. {
  127. return BaseItem.SupportedImageExtensions
  128. .Select(i => args.GetMetaFileByPath(GetFullImagePath(item, args, filenameWithoutExtension, i)))
  129. .FirstOrDefault(i => i != null);
  130. }
  131. protected virtual string GetFullImagePath(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension, string extension)
  132. {
  133. var path = item.MetaLocation;
  134. if (item.IsInMixedFolder)
  135. {
  136. var pathFilenameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path);
  137. // If the image filename and path file name match, just look for an image using the same full path as the item
  138. if (string.Equals(pathFilenameWithoutExtension, filenameWithoutExtension))
  139. {
  140. return Path.ChangeExtension(item.Path, extension);
  141. }
  142. return Path.Combine(path, pathFilenameWithoutExtension + "-" + filenameWithoutExtension + extension);
  143. }
  144. return Path.Combine(path, filenameWithoutExtension + extension);
  145. }
  146. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  147. /// <summary>
  148. /// Fills in image paths based on files win the folder
  149. /// </summary>
  150. /// <param name="item">The item.</param>
  151. /// <param name="args">The args.</param>
  152. private void PopulateBaseItemImages(BaseItem item, ItemResolveArgs args)
  153. {
  154. PopulatePrimaryImage(item, args);
  155. // Logo Image
  156. var image = GetImage(item, args, "logo");
  157. if (image != null)
  158. {
  159. item.SetImage(ImageType.Logo, image.FullName);
  160. }
  161. // Clearart
  162. image = GetImage(item, args, "clearart");
  163. if (image != null)
  164. {
  165. item.SetImage(ImageType.Art, image.FullName);
  166. }
  167. // Disc
  168. image = GetImage(item, args, "disc") ??
  169. GetImage(item, args, "cdart");
  170. if (image != null)
  171. {
  172. item.SetImage(ImageType.Disc, image.FullName);
  173. }
  174. // Box Image
  175. image = GetImage(item, args, "box");
  176. if (image != null)
  177. {
  178. item.SetImage(ImageType.Box, image.FullName);
  179. }
  180. // BoxRear Image
  181. image = GetImage(item, args, "boxrear");
  182. if (image != null)
  183. {
  184. item.SetImage(ImageType.BoxRear, image.FullName);
  185. }
  186. // Thumbnail Image
  187. image = GetImage(item, args, "menu");
  188. if (image != null)
  189. {
  190. item.SetImage(ImageType.Menu, image.FullName);
  191. }
  192. PopulateBanner(item, args);
  193. PopulateThumb(item, args);
  194. // Backdrop Image
  195. PopulateBackdrops(item, args);
  196. PopulateScreenshots(item, args);
  197. }
  198. private void PopulatePrimaryImage(BaseItem item, ItemResolveArgs args)
  199. {
  200. // Primary Image
  201. var image = GetImage(item, args, "folder") ??
  202. GetImage(item, args, "poster") ??
  203. GetImage(item, args, "cover") ??
  204. GetImage(item, args, "default");
  205. // Support plex/xbmc convention
  206. if (image == null && item is Series)
  207. {
  208. image = GetImage(item, args, "show");
  209. }
  210. var isFileSystemItem = item.LocationType == LocationType.FileSystem;
  211. // Support plex/xbmc convention
  212. if (image == null && item is Season && item.IndexNumber.HasValue && isFileSystemItem)
  213. {
  214. var seasonMarker = item.IndexNumber.Value == 0
  215. ? "-specials"
  216. : item.IndexNumber.Value.ToString("00", _usCulture);
  217. // Get this one directly from the file system since we have to go up a level
  218. var filename = "season" + seasonMarker + "-poster";
  219. var path = Path.GetDirectoryName(item.Path);
  220. path = Path.Combine(path, filename);
  221. image = new FileInfo(path);
  222. if (!image.Exists)
  223. {
  224. image = null;
  225. }
  226. }
  227. // Support plex/xbmc convention
  228. if (image == null && (item is Movie || item is MusicVideo || item is AdultVideo))
  229. {
  230. image = GetImage(item, args, "movie");
  231. }
  232. // Look for a file with the same name as the item
  233. if (image == null && isFileSystemItem)
  234. {
  235. var name = Path.GetFileNameWithoutExtension(item.Path);
  236. if (!string.IsNullOrEmpty(name))
  237. {
  238. image = GetImage(item, args, name) ??
  239. GetImage(item, args, name + "-poster");
  240. }
  241. }
  242. if (image != null)
  243. {
  244. item.SetImage(ImageType.Primary, image.FullName);
  245. }
  246. }
  247. /// <summary>
  248. /// Populates the banner.
  249. /// </summary>
  250. /// <param name="item">The item.</param>
  251. /// <param name="args">The args.</param>
  252. private void PopulateBanner(BaseItem item, ItemResolveArgs args)
  253. {
  254. // Banner Image
  255. var image = GetImage(item, args, "banner");
  256. if (image == null)
  257. {
  258. // Supprt xbmc conventions
  259. if (item is Season && item.IndexNumber.HasValue && item.LocationType == LocationType.FileSystem)
  260. {
  261. var seasonMarker = item.IndexNumber.Value == 0
  262. ? "-specials"
  263. : item.IndexNumber.Value.ToString("00", _usCulture);
  264. // Get this one directly from the file system since we have to go up a level
  265. var filename = "season" + seasonMarker + "-banner";
  266. var path = Path.GetDirectoryName(item.Path);
  267. path = Path.Combine(path, filename);
  268. image = new FileInfo(path);
  269. if (!image.Exists)
  270. {
  271. image = null;
  272. }
  273. }
  274. }
  275. if (image != null)
  276. {
  277. item.SetImage(ImageType.Banner, image.FullName);
  278. }
  279. }
  280. /// <summary>
  281. /// Populates the thumb.
  282. /// </summary>
  283. /// <param name="item">The item.</param>
  284. /// <param name="args">The args.</param>
  285. private void PopulateThumb(BaseItem item, ItemResolveArgs args)
  286. {
  287. // Thumbnail Image
  288. var image = GetImage(item, args, "thumb");
  289. if (image == null)
  290. {
  291. // Supprt xbmc conventions
  292. if (item is Season && item.IndexNumber.HasValue && item.LocationType == LocationType.FileSystem)
  293. {
  294. var seasonMarker = item.IndexNumber.Value == 0
  295. ? "-specials"
  296. : item.IndexNumber.Value.ToString("00", _usCulture);
  297. // Get this one directly from the file system since we have to go up a level
  298. var filename = "season" + seasonMarker + "-landscape";
  299. var path = Path.GetDirectoryName(item.Path);
  300. path = Path.Combine(path, filename);
  301. image = new FileInfo(path);
  302. if (!image.Exists)
  303. {
  304. image = null;
  305. }
  306. }
  307. }
  308. if (image != null)
  309. {
  310. item.SetImage(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. if (item is Season && item.IndexNumber.HasValue && isFileSystemItem)
  341. {
  342. var seasonMarker = item.IndexNumber.Value == 0
  343. ? "-specials"
  344. : item.IndexNumber.Value.ToString("00", _usCulture);
  345. // Get this one directly from the file system since we have to go up a level
  346. var filename = "season" + seasonMarker + "-fanart";
  347. var path = Path.GetDirectoryName(item.Path);
  348. path = Path.Combine(path, filename);
  349. var image = new FileInfo(path);
  350. if (image.Exists)
  351. {
  352. backdropFiles.Add(image.FullName);
  353. }
  354. }
  355. if (isFileSystemItem)
  356. {
  357. PopulateBackdropsFromExtraFanart(args, backdropFiles);
  358. }
  359. if (backdropFiles.Count > 0)
  360. {
  361. item.BackdropImagePaths = backdropFiles;
  362. }
  363. }
  364. /// <summary>
  365. /// Populates the backdrops from extra fanart.
  366. /// </summary>
  367. /// <param name="args">The args.</param>
  368. /// <param name="backdrops">The backdrops.</param>
  369. private void PopulateBackdropsFromExtraFanart(ItemResolveArgs args, List<string> backdrops)
  370. {
  371. if (!args.IsDirectory)
  372. {
  373. return;
  374. }
  375. if (args.ContainsFileSystemEntryByName("extrafanart"))
  376. {
  377. var path = Path.Combine(args.Path, "extrafanart");
  378. var imageFiles = Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)
  379. .Where(i =>
  380. {
  381. var extension = Path.GetExtension(i);
  382. if (string.IsNullOrEmpty(extension))
  383. {
  384. return false;
  385. }
  386. return BaseItem.SupportedImageExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  387. })
  388. .ToList();
  389. backdrops.AddRange(imageFiles);
  390. }
  391. }
  392. /// <summary>
  393. /// Populates the backdrops.
  394. /// </summary>
  395. /// <param name="item">The item.</param>
  396. /// <param name="args">The args.</param>
  397. /// <param name="backdropFiles">The backdrop files.</param>
  398. /// <param name="filename">The filename.</param>
  399. /// <param name="numberedSuffix">The numbered suffix.</param>
  400. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args, List<string> backdropFiles, string filename, string numberedSuffix)
  401. {
  402. var image = GetImage(item, args, filename);
  403. if (image != null)
  404. {
  405. backdropFiles.Add(image.FullName);
  406. }
  407. var unfound = 0;
  408. for (var i = 1; i <= 20; i++)
  409. {
  410. // Backdrop Image
  411. image = GetImage(item, args, numberedSuffix + i);
  412. if (image != null)
  413. {
  414. backdropFiles.Add(image.FullName);
  415. }
  416. else
  417. {
  418. unfound++;
  419. if (unfound >= 3)
  420. {
  421. break;
  422. }
  423. }
  424. }
  425. }
  426. /// <summary>
  427. /// Populates the screenshots.
  428. /// </summary>
  429. /// <param name="item">The item.</param>
  430. /// <param name="args">The args.</param>
  431. private void PopulateScreenshots(BaseItem item, ItemResolveArgs args)
  432. {
  433. // Screenshot Image
  434. var image = GetImage(item, args, "screenshot");
  435. var screenshotFiles = new List<string>();
  436. if (image != null)
  437. {
  438. screenshotFiles.Add(image.FullName);
  439. }
  440. var unfound = 0;
  441. for (var i = 1; i <= 20; i++)
  442. {
  443. // Screenshot Image
  444. image = GetImage(item, args, "screenshot" + i);
  445. if (image != null)
  446. {
  447. screenshotFiles.Add(image.FullName);
  448. }
  449. else
  450. {
  451. unfound++;
  452. if (unfound >= 3)
  453. {
  454. break;
  455. }
  456. }
  457. }
  458. if (screenshotFiles.Count > 0)
  459. {
  460. item.ScreenshotImagePaths = screenshotFiles;
  461. }
  462. }
  463. protected FileSystemInfo GetImageFromLocation(string path, string filenameWithoutExtension)
  464. {
  465. try
  466. {
  467. var files = new DirectoryInfo(path)
  468. .EnumerateFiles()
  469. .Where(i =>
  470. {
  471. var fileName = Path.GetFileNameWithoutExtension(i.FullName);
  472. if (!string.Equals(fileName, filenameWithoutExtension, StringComparison.OrdinalIgnoreCase))
  473. {
  474. return false;
  475. }
  476. var ext = i.Extension;
  477. return !string.IsNullOrEmpty(ext) &&
  478. BaseItem.SupportedImageExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  479. })
  480. .ToList();
  481. return BaseItem.SupportedImageExtensions
  482. .Select(ext => files.FirstOrDefault(i => string.Equals(ext, i.Extension, StringComparison.OrdinalIgnoreCase)))
  483. .FirstOrDefault(file => file != null);
  484. }
  485. catch (DirectoryNotFoundException)
  486. {
  487. return null;
  488. }
  489. }
  490. }
  491. }