ImageFromMediaLocationProvider.cs 9.2 KB

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