ImageFromMediaLocationProvider.cs 7.6 KB

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