ImageFromMediaLocationProvider.cs 7.7 KB

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