ImageFromMediaLocationProvider.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 IsInSameDirectory(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 => IsInSameDirectory(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 IsInSameDirectory(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. // Primary Image
  142. var image = GetImage(item, "folder");
  143. if (image.HasValue)
  144. {
  145. item.SetImage(ImageType.Primary, image.Value.Path);
  146. }
  147. // Logo Image
  148. image = GetImage(item, "logo");
  149. if (image.HasValue)
  150. {
  151. item.SetImage(ImageType.Logo, image.Value.Path);
  152. }
  153. // Banner Image
  154. image = GetImage(item, "banner");
  155. if (image.HasValue)
  156. {
  157. item.SetImage(ImageType.Banner, image.Value.Path);
  158. }
  159. // Clearart
  160. image = GetImage(item, "clearart");
  161. if (image.HasValue)
  162. {
  163. item.SetImage(ImageType.Art, image.Value.Path);
  164. }
  165. // Thumbnail Image
  166. image = GetImage(item, "thumb");
  167. if (image.HasValue)
  168. {
  169. item.SetImage(ImageType.Thumb, image.Value.Path);
  170. }
  171. // Backdrop Image
  172. image = GetImage(item, "backdrop");
  173. if (image.HasValue)
  174. {
  175. backdropFiles.Add(image.Value.Path);
  176. }
  177. var unfound = 0;
  178. for (var i = 1; i <= 20; i++)
  179. {
  180. // Backdrop Image
  181. image = GetImage(item, "backdrop" + i);
  182. if (image.HasValue)
  183. {
  184. backdropFiles.Add(image.Value.Path);
  185. }
  186. else
  187. {
  188. unfound++;
  189. if (unfound >= 3)
  190. {
  191. break;
  192. }
  193. }
  194. }
  195. if (backdropFiles.Count > 0)
  196. {
  197. item.BackdropImagePaths = backdropFiles;
  198. }
  199. }
  200. }
  201. }