ImageFromMediaLocationProvider.cs 7.6 KB

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