ImagesByNameProvider.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.IO;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  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 generic types by looking for standard images in the IBN
  14. /// </summary>
  15. public class ImagesByNameProvider : ImageFromMediaLocationProvider
  16. {
  17. public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
  18. : base(logManager, configurationManager)
  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. //only run for these generic types since we are expensive in file i/o
  29. return item is IndexFolder || item is BasePluginFolder || item is CollectionFolder;
  30. }
  31. /// <summary>
  32. /// Gets the priority.
  33. /// </summary>
  34. /// <value>The priority.</value>
  35. public override MetadataProviderPriority Priority
  36. {
  37. get
  38. {
  39. return MetadataProviderPriority.Last;
  40. }
  41. }
  42. /// <summary>
  43. /// Needses the refresh internal.
  44. /// </summary>
  45. /// <param name="item">The item.</param>
  46. /// <param name="providerInfo">The provider info.</param>
  47. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  48. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  49. {
  50. // Force a refresh if the IBN path changed
  51. if (!string.Equals(providerInfo.CustomData, ConfigurationManager.ApplicationPaths.ItemsByNamePath, StringComparison.OrdinalIgnoreCase))
  52. {
  53. return true;
  54. }
  55. return base.NeedsRefreshInternal(item, providerInfo);
  56. }
  57. /// <summary>
  58. /// Gets a value indicating whether [refresh on file system stamp change].
  59. /// </summary>
  60. /// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
  61. protected override bool RefreshOnFileSystemStampChange
  62. {
  63. get
  64. {
  65. return false;
  66. }
  67. }
  68. /// <summary>
  69. /// Override this to return the date that should be compared to the last refresh date
  70. /// to determine if this provider should be re-fetched.
  71. /// </summary>
  72. /// <param name="item">The item.</param>
  73. /// <returns>DateTime.</returns>
  74. protected override DateTime CompareDate(BaseItem item)
  75. {
  76. // If the IBN location exists return the last modified date of any file in it
  77. var location = GetLocation(item);
  78. if (!Directory.Exists(location))
  79. {
  80. return DateTime.MinValue;
  81. }
  82. var files = new DirectoryInfo(location).EnumerateFiles().ToList();
  83. if (files.Count == 0)
  84. {
  85. return DateTime.MinValue;
  86. }
  87. return files.Select(f =>
  88. {
  89. var lastWriteTime = FileSystem.GetLastWriteTimeUtc(f, Logger);
  90. var creationTime = FileSystem.GetCreationTimeUtc(f, Logger);
  91. return creationTime > lastWriteTime ? creationTime : lastWriteTime;
  92. }).Max();
  93. }
  94. /// <summary>
  95. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  96. /// </summary>
  97. /// <param name="item">The item.</param>
  98. /// <param name="force">if set to <c>true</c> [force].</param>
  99. /// <param name="cancellationToken">The cancellation token.</param>
  100. /// <returns>Task{System.Boolean}.</returns>
  101. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  102. {
  103. var result = await base.FetchAsync(item, force, cancellationToken).ConfigureAwait(false);
  104. BaseProviderInfo data;
  105. if (item.ProviderData.TryGetValue(Id, out data))
  106. {
  107. data.CustomData = ConfigurationManager.ApplicationPaths.ItemsByNamePath;
  108. }
  109. return result;
  110. }
  111. /// <summary>
  112. /// Gets the location.
  113. /// </summary>
  114. /// <param name="item">The item.</param>
  115. /// <returns>System.String.</returns>
  116. protected string GetLocation(BaseItem item)
  117. {
  118. var name = FileSystem.GetValidFilename(item.Name);
  119. return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
  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 override FileSystemInfo GetImage(BaseItem item, string filenameWithoutExtension)
  128. {
  129. var location = GetLocation(item);
  130. var result = new FileInfo(Path.Combine(location, filenameWithoutExtension + ".png"));
  131. if (!result.Exists)
  132. result = new FileInfo(Path.Combine(location, filenameWithoutExtension + ".jpg"));
  133. if (result.Exists)
  134. {
  135. return result;
  136. }
  137. return null;
  138. }
  139. }
  140. }