ImageFromMediaLocationProvider.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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)
  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.ResolveArgs.IsDirectory && item.LocationType == LocationType.FileSystem;
  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. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  53. /// </summary>
  54. /// <param name="item">The item.</param>
  55. /// <param name="force">if set to <c>true</c> [force].</param>
  56. /// <param name="cancellationToken">The cancellation token.</param>
  57. /// <returns>Task{System.Boolean}.</returns>
  58. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  59. {
  60. cancellationToken.ThrowIfCancellationRequested();
  61. // Make sure current image paths still exist
  62. ValidateImages(item);
  63. cancellationToken.ThrowIfCancellationRequested();
  64. // Make sure current backdrop paths still exist
  65. ValidateBackdrops(item);
  66. cancellationToken.ThrowIfCancellationRequested();
  67. PopulateBaseItemImages(item);
  68. SetLastRefreshed(item, DateTime.UtcNow);
  69. return TrueTaskResult;
  70. }
  71. /// <summary>
  72. /// Validates that images within the item are still on the file system
  73. /// </summary>
  74. /// <param name="item">The item.</param>
  75. private void ValidateImages(BaseItem item)
  76. {
  77. if (item.Images == null)
  78. {
  79. return;
  80. }
  81. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  82. var deletedKeys = item.Images.Keys.Where(image =>
  83. {
  84. var path = item.Images[image];
  85. return IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null;
  86. }).ToList();
  87. // Now remove them from the dictionary
  88. foreach (var key in deletedKeys)
  89. {
  90. item.Images.Remove(key);
  91. }
  92. }
  93. /// <summary>
  94. /// Validates that backdrops within the item are still on the file system
  95. /// </summary>
  96. /// <param name="item">The item.</param>
  97. private void ValidateBackdrops(BaseItem item)
  98. {
  99. if (item.BackdropImagePaths == null)
  100. {
  101. return;
  102. }
  103. // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
  104. var deletedImages = item.BackdropImagePaths.Where(path => IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null).ToList();
  105. // Now remove them from the dictionary
  106. foreach (var path in deletedImages)
  107. {
  108. item.BackdropImagePaths.Remove(path);
  109. }
  110. }
  111. /// <summary>
  112. /// Determines whether [is in same directory] [the specified item].
  113. /// </summary>
  114. /// <param name="item">The item.</param>
  115. /// <param name="path">The path.</param>
  116. /// <returns><c>true</c> if [is in same directory] [the specified item]; otherwise, <c>false</c>.</returns>
  117. private bool IsInMetaLocation(BaseItem item, string path)
  118. {
  119. return string.Equals(Path.GetDirectoryName(path), item.MetaLocation, StringComparison.OrdinalIgnoreCase);
  120. }
  121. /// <summary>
  122. /// Gets the image.
  123. /// </summary>
  124. /// <param name="item">The item.</param>
  125. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  126. /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
  127. protected virtual FileSystemInfo GetImage(BaseItem item, string filenameWithoutExtension)
  128. {
  129. return item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.ResolveArgs.Path, filenameWithoutExtension + ".png")) ?? item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.ResolveArgs.Path, filenameWithoutExtension + ".jpg"));
  130. }
  131. /// <summary>
  132. /// Fills in image paths based on files win the folder
  133. /// </summary>
  134. /// <param name="item">The item.</param>
  135. private void PopulateBaseItemImages(BaseItem item)
  136. {
  137. var backdropFiles = new List<string>();
  138. var screenshotFiles = new List<string>();
  139. // Primary Image
  140. var image = GetImage(item, "folder");
  141. if (image != null)
  142. {
  143. item.SetImage(ImageType.Primary, image.FullName);
  144. }
  145. // Logo Image
  146. image = GetImage(item, "logo");
  147. if (image != null)
  148. {
  149. item.SetImage(ImageType.Logo, image.FullName);
  150. }
  151. // Banner Image
  152. image = GetImage(item, "banner");
  153. if (image != null)
  154. {
  155. item.SetImage(ImageType.Banner, image.FullName);
  156. }
  157. // Clearart
  158. image = GetImage(item, "clearart");
  159. if (image != null)
  160. {
  161. item.SetImage(ImageType.Art, image.FullName);
  162. }
  163. // Thumbnail Image
  164. image = GetImage(item, "thumb");
  165. if (image != null)
  166. {
  167. item.SetImage(ImageType.Thumb, image.FullName);
  168. }
  169. // Thumbnail Image
  170. image = GetImage(item, "box");
  171. if (image != null)
  172. {
  173. item.SetImage(ImageType.Box, image.FullName);
  174. }
  175. // Thumbnail Image
  176. image = GetImage(item, "menu");
  177. if (image != null)
  178. {
  179. item.SetImage(ImageType.Menu, image.FullName);
  180. }
  181. // Backdrop Image
  182. image = GetImage(item, "backdrop");
  183. if (image != null)
  184. {
  185. backdropFiles.Add(image.FullName);
  186. }
  187. var unfound = 0;
  188. for (var i = 1; i <= 20; i++)
  189. {
  190. // Backdrop Image
  191. image = GetImage(item, "backdrop" + i);
  192. if (image != null)
  193. {
  194. backdropFiles.Add(image.FullName);
  195. }
  196. else
  197. {
  198. unfound++;
  199. if (unfound >= 3)
  200. {
  201. break;
  202. }
  203. }
  204. }
  205. if (backdropFiles.Count > 0)
  206. {
  207. item.BackdropImagePaths = backdropFiles;
  208. }
  209. // Screenshot Image
  210. image = GetImage(item, "screenshot");
  211. if (image != null)
  212. {
  213. screenshotFiles.Add(image.FullName);
  214. }
  215. unfound = 0;
  216. for (var i = 1; i <= 20; i++)
  217. {
  218. // Screenshot Image
  219. image = GetImage(item, "screenshot" + i);
  220. if (image != null)
  221. {
  222. screenshotFiles.Add(image.FullName);
  223. }
  224. else
  225. {
  226. unfound++;
  227. if (unfound >= 3)
  228. {
  229. break;
  230. }
  231. }
  232. }
  233. if (screenshotFiles.Count > 0)
  234. {
  235. item.ScreenshotImagePaths = screenshotFiles;
  236. }
  237. }
  238. }
  239. }