ImageFromMediaLocationProvider.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 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, args);
  92. cancellationToken.ThrowIfCancellationRequested();
  93. // Make sure current backdrop paths still exist
  94. ValidateBackdrops(item, args);
  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. return item.Parent.ResolveArgs;
  106. }
  107. return item.ResolveArgs;
  108. }
  109. /// <summary>
  110. /// Validates that images within the item are still on the file system
  111. /// </summary>
  112. /// <param name="item">The item.</param>
  113. /// <param name="args">The args.</param>
  114. private void ValidateImages(BaseItem item, ItemResolveArgs args)
  115. {
  116. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  117. var deletedKeys = item.Images.ToList().Where(image =>
  118. {
  119. var path = image.Value;
  120. return IsInMetaLocation(item, path) && args.GetMetaFileByPath(path) == null;
  121. }).Select(i => i.Key).ToList();
  122. // Now remove them from the dictionary
  123. foreach (var key in deletedKeys)
  124. {
  125. item.Images.Remove(key);
  126. }
  127. }
  128. /// <summary>
  129. /// Validates that backdrops within the item are still on the file system
  130. /// </summary>
  131. /// <param name="item">The item.</param>
  132. /// <param name="args">The args.</param>
  133. private void ValidateBackdrops(BaseItem item, ItemResolveArgs args)
  134. {
  135. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  136. var deletedImages = item.BackdropImagePaths.Where(path => IsInMetaLocation(item, path) && args.GetMetaFileByPath(path) == null).ToList();
  137. // Now remove them from the dictionary
  138. foreach (var path in deletedImages)
  139. {
  140. item.BackdropImagePaths.Remove(path);
  141. }
  142. }
  143. /// <summary>
  144. /// Validates the screenshots.
  145. /// </summary>
  146. /// <param name="item">The item.</param>
  147. /// <param name="args">The args.</param>
  148. private void ValidateScreenshots(BaseItem item, ItemResolveArgs args)
  149. {
  150. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  151. var deletedImages = item.ScreenshotImagePaths.Where(path => IsInMetaLocation(item, path) && args.GetMetaFileByPath(path) == null).ToList();
  152. // Now remove them from the dictionary
  153. foreach (var path in deletedImages)
  154. {
  155. item.ScreenshotImagePaths.Remove(path);
  156. }
  157. }
  158. /// <summary>
  159. /// Determines whether [is in same directory] [the specified item].
  160. /// </summary>
  161. /// <param name="item">The item.</param>
  162. /// <param name="path">The path.</param>
  163. /// <returns><c>true</c> if [is in same directory] [the specified item]; otherwise, <c>false</c>.</returns>
  164. private bool IsInMetaLocation(BaseItem item, string path)
  165. {
  166. return string.Equals(Path.GetDirectoryName(path), item.MetaLocation, StringComparison.OrdinalIgnoreCase);
  167. }
  168. /// <summary>
  169. /// Gets the image.
  170. /// </summary>
  171. /// <param name="item">The item.</param>
  172. /// <param name="args">The args.</param>
  173. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  174. /// <returns>FileSystemInfo.</returns>
  175. protected virtual FileSystemInfo GetImage(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension)
  176. {
  177. return BaseItem.SupportedImageExtensions
  178. .Select(i => args.GetMetaFileByPath(GetFullImagePath(item, args, filenameWithoutExtension, i)))
  179. .FirstOrDefault(i => i != null);
  180. }
  181. protected virtual string GetFullImagePath(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension, string extension)
  182. {
  183. var path = item.MetaLocation;
  184. if (item.IsInMixedFolder)
  185. {
  186. var pathFilenameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path);
  187. // If the image filename and path file name match, just look for an image using the same full path as the item
  188. if (string.Equals(pathFilenameWithoutExtension, filenameWithoutExtension))
  189. {
  190. return Path.ChangeExtension(item.Path, extension);
  191. }
  192. return Path.Combine(path, pathFilenameWithoutExtension + "-" + filenameWithoutExtension + extension);
  193. }
  194. return Path.Combine(path, filenameWithoutExtension + extension);
  195. }
  196. /// <summary>
  197. /// Fills in image paths based on files win the folder
  198. /// </summary>
  199. /// <param name="item">The item.</param>
  200. /// <param name="args">The args.</param>
  201. private void PopulateBaseItemImages(BaseItem item, ItemResolveArgs args)
  202. {
  203. // Primary Image
  204. var image = GetImage(item, args, "folder") ??
  205. GetImage(item, args, "poster") ??
  206. GetImage(item, args, "cover") ??
  207. GetImage(item, args, "default");
  208. // Look for a file with the same name as the item
  209. if (image == null)
  210. {
  211. var name = Path.GetFileNameWithoutExtension(item.Path);
  212. if (!string.IsNullOrEmpty(name))
  213. {
  214. image = GetImage(item, args, name);
  215. }
  216. }
  217. if (image != null)
  218. {
  219. item.SetImage(ImageType.Primary, image.FullName);
  220. }
  221. // Logo Image
  222. image = GetImage(item, args, "logo");
  223. if (image != null)
  224. {
  225. item.SetImage(ImageType.Logo, image.FullName);
  226. }
  227. // Banner Image
  228. image = GetImage(item, args, "banner");
  229. if (image != null)
  230. {
  231. item.SetImage(ImageType.Banner, image.FullName);
  232. }
  233. // Clearart
  234. image = GetImage(item, args, "clearart");
  235. if (image != null)
  236. {
  237. item.SetImage(ImageType.Art, image.FullName);
  238. }
  239. // Disc
  240. image = GetImage(item, args, "disc");
  241. if (image != null)
  242. {
  243. item.SetImage(ImageType.Disc, image.FullName);
  244. }
  245. // Thumbnail Image
  246. image = GetImage(item, args, "thumb");
  247. if (image != null)
  248. {
  249. item.SetImage(ImageType.Thumb, image.FullName);
  250. }
  251. // Box Image
  252. image = GetImage(item, args, "box");
  253. if (image != null)
  254. {
  255. item.SetImage(ImageType.Box, image.FullName);
  256. }
  257. // BoxRear Image
  258. image = GetImage(item, args, "boxrear");
  259. if (image != null)
  260. {
  261. item.SetImage(ImageType.BoxRear, image.FullName);
  262. }
  263. // Thumbnail Image
  264. image = GetImage(item, args, "menu");
  265. if (image != null)
  266. {
  267. item.SetImage(ImageType.Menu, image.FullName);
  268. }
  269. // Backdrop Image
  270. PopulateBackdrops(item, args);
  271. // Screenshot Image
  272. image = GetImage(item, args, "screenshot");
  273. var screenshotFiles = new List<string>();
  274. if (image != null)
  275. {
  276. screenshotFiles.Add(image.FullName);
  277. }
  278. var unfound = 0;
  279. for (var i = 1; i <= 20; i++)
  280. {
  281. // Screenshot Image
  282. image = GetImage(item, args, "screenshot" + i);
  283. if (image != null)
  284. {
  285. screenshotFiles.Add(image.FullName);
  286. }
  287. else
  288. {
  289. unfound++;
  290. if (unfound >= 3)
  291. {
  292. break;
  293. }
  294. }
  295. }
  296. if (screenshotFiles.Count > 0)
  297. {
  298. item.ScreenshotImagePaths = screenshotFiles;
  299. }
  300. }
  301. /// <summary>
  302. /// Populates the backdrops.
  303. /// </summary>
  304. /// <param name="item">The item.</param>
  305. /// <param name="args">The args.</param>
  306. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args)
  307. {
  308. var backdropFiles = new List<string>();
  309. PopulateBackdrops(item, args, backdropFiles, "backdrop", "backdrop");
  310. // Support plex/xbmc conventions
  311. PopulateBackdrops(item, args, backdropFiles, "fanart", "fanart-");
  312. PopulateBackdrops(item, args, backdropFiles, "background", "background-");
  313. if (backdropFiles.Count > 0)
  314. {
  315. item.BackdropImagePaths = backdropFiles;
  316. }
  317. }
  318. /// <summary>
  319. /// Populates the backdrops.
  320. /// </summary>
  321. /// <param name="item">The item.</param>
  322. /// <param name="args">The args.</param>
  323. /// <param name="backdropFiles">The backdrop files.</param>
  324. /// <param name="filename">The filename.</param>
  325. /// <param name="numberedSuffix">The numbered suffix.</param>
  326. private void PopulateBackdrops(BaseItem item, ItemResolveArgs args, List<string> backdropFiles, string filename, string numberedSuffix)
  327. {
  328. var image = GetImage(item, args, filename);
  329. if (image != null)
  330. {
  331. backdropFiles.Add(image.FullName);
  332. }
  333. var unfound = 0;
  334. for (var i = 1; i <= 20; i++)
  335. {
  336. // Backdrop Image
  337. image = GetImage(item, args, numberedSuffix + i);
  338. if (image != null)
  339. {
  340. backdropFiles.Add(image.FullName);
  341. }
  342. else
  343. {
  344. unfound++;
  345. if (unfound >= 3)
  346. {
  347. break;
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }