ImagesByNameProvider.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.IO;
  6. using System;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using MediaBrowser.Model.Logging;
  11. namespace MediaBrowser.Controller.Providers
  12. {
  13. /// <summary>
  14. /// Provides images for generic types by looking for standard images in the IBN
  15. /// </summary>
  16. public class ImagesByNameProvider : ImageFromMediaLocationProvider
  17. {
  18. public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager) : 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 = FileSystem.GetFiles(location).ToList();
  83. if (files.Count == 0)
  84. {
  85. return DateTime.MinValue;
  86. }
  87. return files.Select(f => f.CreationTimeUtc > f.LastWriteTimeUtc ? f.CreationTimeUtc : f.LastWriteTimeUtc).Max();
  88. }
  89. /// <summary>
  90. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  91. /// </summary>
  92. /// <param name="item">The item.</param>
  93. /// <param name="force">if set to <c>true</c> [force].</param>
  94. /// <param name="cancellationToken">The cancellation token.</param>
  95. /// <returns>Task{System.Boolean}.</returns>
  96. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  97. {
  98. var result = await base.FetchAsync(item, force, cancellationToken).ConfigureAwait(false);
  99. BaseProviderInfo data;
  100. if (item.ProviderData.TryGetValue(Id, out data))
  101. {
  102. data.CustomData = ConfigurationManager.ApplicationPaths.ItemsByNamePath;
  103. }
  104. return result;
  105. }
  106. /// <summary>
  107. /// Gets the location.
  108. /// </summary>
  109. /// <param name="item">The item.</param>
  110. /// <returns>System.String.</returns>
  111. protected string GetLocation(BaseItem item)
  112. {
  113. var name = FileSystem.GetValidFilename(item.Name);
  114. return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
  115. }
  116. /// <summary>
  117. /// Gets the image.
  118. /// </summary>
  119. /// <param name="item">The item.</param>
  120. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  121. /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
  122. protected override WIN32_FIND_DATA? GetImage(BaseItem item, string filenameWithoutExtension)
  123. {
  124. var location = GetLocation(item);
  125. var result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".png"));
  126. if (!result.HasValue)
  127. result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".jpg"));
  128. return result;
  129. }
  130. }
  131. }