ImageFromMediaLocationProvider.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.TV;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Providers
  15. {
  16. /// <summary>
  17. /// Provides images for all types by looking for standard images - folder, backdrop, logo, etc.
  18. /// </summary>
  19. public class ImageFromMediaLocationProvider : BaseMetadataProvider
  20. {
  21. public ImageFromMediaLocationProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
  22. : base(logManager, configurationManager)
  23. {
  24. }
  25. public override ItemUpdateType ItemUpdateType
  26. {
  27. get
  28. {
  29. return ItemUpdateType.ImageUpdate;
  30. }
  31. }
  32. /// <summary>
  33. /// Supportses the specified item.
  34. /// </summary>
  35. /// <param name="item">The item.</param>
  36. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  37. public override bool Supports(BaseItem item)
  38. {
  39. if (item.LocationType == LocationType.FileSystem)
  40. {
  41. if (item.ResolveArgs.IsDirectory)
  42. {
  43. return true;
  44. }
  45. return item.IsInMixedFolder && item.Parent != null && !(item is Episode);
  46. }
  47. return false;
  48. }
  49. /// <summary>
  50. /// Gets the priority.
  51. /// </summary>
  52. /// <value>The priority.</value>
  53. public override MetadataProviderPriority Priority
  54. {
  55. get { return MetadataProviderPriority.First; }
  56. }
  57. /// <summary>
  58. /// Returns true or false indicating if the provider should refresh when the contents of it's directory changes
  59. /// </summary>
  60. /// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
  61. protected override bool RefreshOnFileSystemStampChange
  62. {
  63. get
  64. {
  65. return true;
  66. }
  67. }
  68. /// <summary>
  69. /// Gets the filestamp extensions.
  70. /// </summary>
  71. /// <value>The filestamp extensions.</value>
  72. protected override string[] FilestampExtensions
  73. {
  74. get
  75. {
  76. return BaseItem.SupportedImageExtensions;
  77. }
  78. }
  79. /// <summary>
  80. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  81. /// </summary>
  82. /// <param name="item">The item.</param>
  83. /// <param name="force">if set to <c>true</c> [force].</param>
  84. /// <param name="cancellationToken">The cancellation token.</param>
  85. /// <returns>Task{System.Boolean}.</returns>
  86. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  87. {
  88. cancellationToken.ThrowIfCancellationRequested();
  89. var args = GetResolveArgsContainingImages(item);
  90. // Make sure current image paths still exist
  91. ValidateImages(item);
  92. cancellationToken.ThrowIfCancellationRequested();
  93. // Make sure current backdrop paths still exist
  94. ValidateBackdrops(item);
  95. ValidateScreenshots(item, args);
  96. cancellationToken.ThrowIfCancellationRequested();
  97. PopulateBaseItemImages(item, args);
  98. SetLastRefreshed(item, DateTime.UtcNow);
  99. return TrueTaskResult;
  100. }
  101. private ItemResolveArgs GetResolveArgsContainingImages(BaseItem item)
  102. {
  103. if (item.IsInMixedFolder)
  104. {
  105. if (item.Parent == null)
  106. {
  107. return item.ResolveArgs;
  108. }
  109. return item.Parent.ResolveArgs;
  110. }
  111. return item.ResolveArgs;
  112. }
  113. /// <summary>
  114. /// Validates that images within the item are still on the file system
  115. /// </summary>
  116. /// <param name="item">The item.</param>
  117. internal static void ValidateImages(BaseItem item)
  118. {
  119. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  120. var deletedKeys = item.Images
  121. .ToList()
  122. .Where(image => !File.Exists(image.Value))
  123. .Select(i => i.Key)
  124. .ToList();
  125. // Now remove them from the dictionary
  126. foreach (var key in deletedKeys)
  127. {
  128. item.Images.Remove(key);
  129. }
  130. }
  131. /// <summary>
  132. /// Validates that backdrops within the item are still on the file system
  133. /// </summary>
  134. /// <param name="item">The item.</param>
  135. internal static void ValidateBackdrops(BaseItem item)
  136. {
  137. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  138. var deletedImages = item.BackdropImagePaths
  139. .Where(path => !File.Exists(path))
  140. .ToList();
  141. // Now remove them from the dictionary
  142. foreach (var path in deletedImages)
  143. {
  144. item.BackdropImagePaths.Remove(path);
  145. }
  146. }
  147. /// <summary>
  148. /// Validates the screenshots.
  149. /// </summary>
  150. /// <param name="item">The item.</param>
  151. /// <param name="args">The args.</param>
  152. private void ValidateScreenshots(BaseItem item, ItemResolveArgs args)
  153. {
  154. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  155. var deletedImages = item.ScreenshotImagePaths
  156. .Where(path => !File.Exists(path))
  157. .ToList();
  158. // Now remove them from the dictionary
  159. foreach (var path in deletedImages)
  160. {
  161. item.ScreenshotImagePaths.Remove(path);
  162. }
  163. }
  164. /// <summary>
  165. /// Determines whether [is in same directory] [the specified item].
  166. /// </summary>
  167. /// <param name="item">The item.</param>
  168. /// <param name="path">The path.</param>
  169. /// <returns><c>true</c> if [is in same directory] [the specified item]; otherwise, <c>false</c>.</returns>
  170. private bool IsInMetaLocation(BaseItem item, string path)
  171. {
  172. return string.Equals(Path.GetDirectoryName(path), item.MetaLocation, StringComparison.OrdinalIgnoreCase);
  173. }
  174. /// <summary>
  175. /// Gets the image.
  176. /// </summary>
  177. /// <param name="item">The item.</param>
  178. /// <param name="args">The args.</param>
  179. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  180. /// <returns>FileSystemInfo.</returns>
  181. protected virtual FileSystemInfo GetImage(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension)
  182. {
  183. return BaseItem.SupportedImageExtensions
  184. .Select(i => args.GetMetaFileByPath(GetFullImagePath(item, args, filenameWithoutExtension, i)))
  185. .FirstOrDefault(i => i != null);
  186. }
  187. protected virtual string GetFullImagePath(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension, string extension)
  188. {
  189. var path = item.MetaLocation;
  190. if (item.IsInMixedFolder)
  191. {
  192. var pathFilenameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path);
  193. // If the image filename and path file name match, just look for an image using the same full path as the item
  194. if (string.Equals(pathFilenameWithoutExtension, filenameWithoutExtension))
  195. {
  196. return Path.ChangeExtension(item.Path, extension);
  197. }
  198. return Path.Combine(path, pathFilenameWithoutExtension + "-" + filenameWithoutExtension + extension);
  199. }
  200. return Path.Combine(path, filenameWithoutExtension + extension);
  201. }
  202. /// <summary>
  203. /// Fills in image paths based on files win the folder
  204. /// </summary>
  205. /// <param name="item">The item.</param>
  206. /// <param name="args">The args.</param>
  207. private void PopulateBaseItemImages(BaseItem item, ItemResolveArgs args)
  208. {
  209. // Primary Image
  210. var image = GetImage(item, args, "folder") ??
  211. GetImage(item, args, "poster") ??
  212. GetImage(item, args, "cover") ??
  213. GetImage(item, args, "default");
  214. // Look for a file with the same name as the item
  215. if (image == null)
  216. {
  217. var name = Path.GetFileNameWithoutExtension(item.Path);
  218. if (!string.IsNullOrEmpty(name))
  219. {
  220. image = GetImage(item, args, name);
  221. }
  222. }
  223. if (image != null)
  224. {
  225. item.SetImage(ImageType.Primary, image.FullName);
  226. }
  227. // Logo Image
  228. image = GetImage(item, args, "logo");
  229. if (image != null)
  230. {
  231. item.SetImage(ImageType.Logo, image.FullName);
  232. }
  233. // Banner Image
  234. image = GetImage(item, args, "banner");
  235. if (image != null)
  236. {
  237. item.SetImage(ImageType.Banner, image.FullName);
  238. }
  239. // Clearart
  240. image = GetImage(item, args, "clearart");
  241. if (image != null)
  242. {
  243. item.SetImage(ImageType.Art, image.FullName);
  244. }
  245. // Disc
  246. image = GetImage(item, args, "disc") ??
  247. GetImage(item, args, "cdart");
  248. if (image != null)
  249. {
  250. item.SetImage(ImageType.Disc, image.FullName);
  251. }
  252. // Thumbnail Image
  253. image = GetImage(item, args, "thumb");
  254. if (image != null)
  255. {
  256. item.SetImage(ImageType.Thumb, image.FullName);
  257. }
  258. // Box Image
  259. image = GetImage(item, args, "box");
  260. if (image != null)
  261. {
  262. item.SetImage(ImageType.Box, image.FullName);
  263. }
  264. // BoxRear Image
  265. image = GetImage(item, args, "boxrear");
  266. if (image != null)
  267. {
  268. item.SetImage(ImageType.BoxRear, image.FullName);
  269. }
  270. // Thumbnail Image
  271. image = GetImage(item, args, "menu");
  272. if (image != null)
  273. {
  274. item.SetImage(ImageType.Menu, image.FullName);
  275. }
  276. // Backdrop Image
  277. PopulateBackdrops(item, args);
  278. // Screenshot Image
  279. image = GetImage(item, args, "screenshot");
  280. var screenshotFiles = new List<string>();
  281. if (image != null)
  282. {
  283. screenshotFiles.Add(image.FullName);
  284. }
  285. var unfound = 0;
  286. for (var i = 1; i <= 20; i++)
  287. {
  288. // Screenshot Image
  289. image = GetImage(item, args, "screenshot" + i);
  290. if (image != null)
  291. {
  292. screenshotFiles.Add(image.FullName);
  293. }
  294. else
  295. {
  296. unfound++;
  297. if (unfound >= 3)
  298. {
  299. break;
  300. }
  301. }
  302. }
  303. if (screenshotFiles.Count > 0)
  304. {
  305. item.ScreenshotImagePaths = screenshotFiles;
  306. }
  307. }
  308. /// <summary>
  309. /// Populates the backdrops.
  310. /// </summary>
  311. /// <param name="item">The item.</param>
  312. /// <param name="args">The args.</param>
  313. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args)
  314. {
  315. var backdropFiles = new List<string>();
  316. PopulateBackdrops(item, args, backdropFiles, "backdrop", "backdrop");
  317. // Support plex/xbmc conventions
  318. PopulateBackdrops(item, args, backdropFiles, "fanart", "fanart-");
  319. PopulateBackdrops(item, args, backdropFiles, "background", "background-");
  320. if (backdropFiles.Count > 0)
  321. {
  322. item.BackdropImagePaths = backdropFiles;
  323. }
  324. }
  325. /// <summary>
  326. /// Populates the backdrops.
  327. /// </summary>
  328. /// <param name="item">The item.</param>
  329. /// <param name="args">The args.</param>
  330. /// <param name="backdropFiles">The backdrop files.</param>
  331. /// <param name="filename">The filename.</param>
  332. /// <param name="numberedSuffix">The numbered suffix.</param>
  333. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args, List<string> backdropFiles, string filename, string numberedSuffix)
  334. {
  335. var image = GetImage(item, args, filename);
  336. if (image != null)
  337. {
  338. backdropFiles.Add(image.FullName);
  339. }
  340. var unfound = 0;
  341. for (var i = 1; i <= 20; i++)
  342. {
  343. // Backdrop Image
  344. image = GetImage(item, args, numberedSuffix + i);
  345. if (image != null)
  346. {
  347. backdropFiles.Add(image.FullName);
  348. }
  349. else
  350. {
  351. unfound++;
  352. if (unfound >= 3)
  353. {
  354. break;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }