2
0

ImageFromMediaLocationProvider.cs 11 KB

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