2
0

ImageFromMediaLocationProvider.cs 11 KB

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