ImagesByNameProvider.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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("*", SearchOption.TopDirectoryOnly).ToList();
  83. if (files.Count == 0)
  84. {
  85. return DateTime.MinValue;
  86. }
  87. return files.Select(f =>
  88. {
  89. var lastWriteTime = GetLastWriteTimeUtc(f);
  90. var creationTime = GetCreationTimeUtc(f);
  91. return creationTime > lastWriteTime ? creationTime : lastWriteTime;
  92. }).Max();
  93. }
  94. /// <summary>
  95. /// Gets the creation time UTC.
  96. /// </summary>
  97. /// <param name="info">The info.</param>
  98. /// <returns>DateTime.</returns>
  99. private DateTime GetLastWriteTimeUtc(FileSystemInfo info)
  100. {
  101. // This could throw an error on some file systems that have dates out of range
  102. try
  103. {
  104. return info.LastAccessTimeUtc;
  105. }
  106. catch (Exception ex)
  107. {
  108. Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
  109. return DateTime.MinValue;
  110. }
  111. }
  112. /// <summary>
  113. /// Gets the creation time UTC.
  114. /// </summary>
  115. /// <param name="info">The info.</param>
  116. /// <returns>DateTime.</returns>
  117. private DateTime GetCreationTimeUtc(FileSystemInfo info)
  118. {
  119. // This could throw an error on some file systems that have dates out of range
  120. try
  121. {
  122. return info.CreationTimeUtc;
  123. }
  124. catch (Exception ex)
  125. {
  126. Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
  127. return DateTime.MinValue;
  128. }
  129. }
  130. /// <summary>
  131. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  132. /// </summary>
  133. /// <param name="item">The item.</param>
  134. /// <param name="force">if set to <c>true</c> [force].</param>
  135. /// <param name="cancellationToken">The cancellation token.</param>
  136. /// <returns>Task{System.Boolean}.</returns>
  137. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  138. {
  139. var result = await base.FetchAsync(item, force, cancellationToken).ConfigureAwait(false);
  140. BaseProviderInfo data;
  141. if (item.ProviderData.TryGetValue(Id, out data))
  142. {
  143. data.CustomData = ConfigurationManager.ApplicationPaths.ItemsByNamePath;
  144. }
  145. return result;
  146. }
  147. /// <summary>
  148. /// Gets the location.
  149. /// </summary>
  150. /// <param name="item">The item.</param>
  151. /// <returns>System.String.</returns>
  152. protected string GetLocation(BaseItem item)
  153. {
  154. var name = FileSystem.GetValidFilename(item.Name);
  155. return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
  156. }
  157. /// <summary>
  158. /// Gets the image.
  159. /// </summary>
  160. /// <param name="item">The item.</param>
  161. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  162. /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
  163. protected override WIN32_FIND_DATA? GetImage(BaseItem item, string filenameWithoutExtension)
  164. {
  165. var location = GetLocation(item);
  166. var result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".png"));
  167. if (!result.HasValue)
  168. result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".jpg"));
  169. return result;
  170. }
  171. }
  172. }