ImageFromMediaLocationProvider.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. var args = GetResolveArgsContainingImages(item);
  95. // Make sure current image paths still exist
  96. item.ValidateImages();
  97. cancellationToken.ThrowIfCancellationRequested();
  98. // Make sure current backdrop paths still exist
  99. item.ValidateBackdrops();
  100. item.ValidateScreenshots();
  101. cancellationToken.ThrowIfCancellationRequested();
  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 backdropFiles = new List<string>();
  321. PopulateBackdrops(item, args, backdropFiles, "backdrop", "backdrop");
  322. // Support plex/xbmc conventions
  323. PopulateBackdrops(item, args, backdropFiles, "fanart", "fanart-");
  324. PopulateBackdrops(item, args, backdropFiles, "background", "background-");
  325. PopulateBackdrops(item, args, backdropFiles, "art", "art-");
  326. var isFileSystemItem = item.LocationType == LocationType.FileSystem;
  327. if (item is Season && item.IndexNumber.HasValue && isFileSystemItem)
  328. {
  329. var seasonMarker = item.IndexNumber.Value == 0
  330. ? "-specials"
  331. : item.IndexNumber.Value.ToString("00", _usCulture);
  332. // Get this one directly from the file system since we have to go up a level
  333. var filename = "season" + seasonMarker + "-fanart";
  334. var path = Path.GetDirectoryName(item.Path);
  335. path = Path.Combine(path, filename);
  336. var image = new FileInfo(path);
  337. if (image.Exists)
  338. {
  339. backdropFiles.Add(image.FullName);
  340. }
  341. }
  342. if (isFileSystemItem)
  343. {
  344. PopulateBackdropsFromExtraFanart(args, backdropFiles);
  345. }
  346. if (backdropFiles.Count > 0)
  347. {
  348. item.BackdropImagePaths = backdropFiles;
  349. }
  350. }
  351. /// <summary>
  352. /// Populates the backdrops from extra fanart.
  353. /// </summary>
  354. /// <param name="args">The args.</param>
  355. /// <param name="backdrops">The backdrops.</param>
  356. private void PopulateBackdropsFromExtraFanart(ItemResolveArgs args, List<string> backdrops)
  357. {
  358. if (!args.IsDirectory)
  359. {
  360. return;
  361. }
  362. if (args.ContainsFileSystemEntryByName("extrafanart"))
  363. {
  364. var path = Path.Combine(args.Path, "extrafanart");
  365. var imageFiles = Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)
  366. .Where(i =>
  367. {
  368. var extension = Path.GetExtension(i);
  369. if (string.IsNullOrEmpty(extension))
  370. {
  371. return false;
  372. }
  373. return BaseItem.SupportedImageExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
  374. })
  375. .ToList();
  376. backdrops.AddRange(imageFiles);
  377. }
  378. }
  379. /// <summary>
  380. /// Populates the backdrops.
  381. /// </summary>
  382. /// <param name="item">The item.</param>
  383. /// <param name="args">The args.</param>
  384. /// <param name="backdropFiles">The backdrop files.</param>
  385. /// <param name="filename">The filename.</param>
  386. /// <param name="numberedSuffix">The numbered suffix.</param>
  387. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args, List<string> backdropFiles, string filename, string numberedSuffix)
  388. {
  389. var image = GetImage(item, args, filename);
  390. if (image != null)
  391. {
  392. backdropFiles.Add(image.FullName);
  393. }
  394. var unfound = 0;
  395. for (var i = 1; i <= 20; i++)
  396. {
  397. // Backdrop Image
  398. image = GetImage(item, args, numberedSuffix + i);
  399. if (image != null)
  400. {
  401. backdropFiles.Add(image.FullName);
  402. }
  403. else
  404. {
  405. unfound++;
  406. if (unfound >= 3)
  407. {
  408. break;
  409. }
  410. }
  411. }
  412. }
  413. /// <summary>
  414. /// Populates the screenshots.
  415. /// </summary>
  416. /// <param name="item">The item.</param>
  417. /// <param name="args">The args.</param>
  418. private void PopulateScreenshots(BaseItem item, ItemResolveArgs args)
  419. {
  420. // Screenshot Image
  421. var image = GetImage(item, args, "screenshot");
  422. var screenshotFiles = new List<string>();
  423. if (image != null)
  424. {
  425. screenshotFiles.Add(image.FullName);
  426. }
  427. var unfound = 0;
  428. for (var i = 1; i <= 20; i++)
  429. {
  430. // Screenshot Image
  431. image = GetImage(item, args, "screenshot" + i);
  432. if (image != null)
  433. {
  434. screenshotFiles.Add(image.FullName);
  435. }
  436. else
  437. {
  438. unfound++;
  439. if (unfound >= 3)
  440. {
  441. break;
  442. }
  443. }
  444. }
  445. if (screenshotFiles.Count > 0)
  446. {
  447. item.ScreenshotImagePaths = screenshotFiles;
  448. }
  449. }
  450. protected FileSystemInfo GetImageFromLocation(string path, string filenameWithoutExtension)
  451. {
  452. try
  453. {
  454. var files = new DirectoryInfo(path)
  455. .EnumerateFiles()
  456. .Where(i =>
  457. {
  458. var fileName = Path.GetFileNameWithoutExtension(i.FullName);
  459. if (!string.Equals(fileName, filenameWithoutExtension, StringComparison.OrdinalIgnoreCase))
  460. {
  461. return false;
  462. }
  463. var ext = i.Extension;
  464. return !string.IsNullOrEmpty(ext) &&
  465. BaseItem.SupportedImageExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  466. })
  467. .ToList();
  468. return BaseItem.SupportedImageExtensions
  469. .Select(ext => files.FirstOrDefault(i => string.Equals(ext, i.Extension, StringComparison.OrdinalIgnoreCase)))
  470. .FirstOrDefault(file => file != null);
  471. }
  472. catch (DirectoryNotFoundException)
  473. {
  474. return null;
  475. }
  476. }
  477. }
  478. }