ImagesByNameProvider.cs 6.2 KB

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