2
0

ImagesByNameProvider.cs 6.2 KB

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