ImagesByNameProvider.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.IO;
  4. using System;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using MediaBrowser.Model.Logging;
  9. namespace MediaBrowser.Controller.Providers
  10. {
  11. /// <summary>
  12. /// Provides images for generic types by looking for standard images in the IBN
  13. /// </summary>
  14. public class ImagesByNameProvider : ImageFromMediaLocationProvider
  15. {
  16. public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager) : base(logManager, configurationManager)
  17. {
  18. }
  19. /// <summary>
  20. /// Supportses the specified item.
  21. /// </summary>
  22. /// <param name="item">The item.</param>
  23. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  24. public override bool Supports(BaseItem item)
  25. {
  26. //only run for these generic types since we are expensive in file i/o
  27. return item is IndexFolder || item is BasePluginFolder;
  28. }
  29. /// <summary>
  30. /// Gets the priority.
  31. /// </summary>
  32. /// <value>The priority.</value>
  33. public override MetadataProviderPriority Priority
  34. {
  35. get
  36. {
  37. return MetadataProviderPriority.Last;
  38. }
  39. }
  40. /// <summary>
  41. /// Gets a value indicating whether [refresh on file system stamp change].
  42. /// </summary>
  43. /// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
  44. protected override bool RefreshOnFileSystemStampChange
  45. {
  46. get
  47. {
  48. return false;
  49. }
  50. }
  51. /// <summary>
  52. /// Override this to return the date that should be compared to the last refresh date
  53. /// to determine if this provider should be re-fetched.
  54. /// </summary>
  55. /// <param name="item">The item.</param>
  56. /// <returns>DateTime.</returns>
  57. protected override DateTime CompareDate(BaseItem item)
  58. {
  59. // If the IBN location exists return the last modified date of any file in it
  60. var location = GetLocation(item);
  61. return Directory.Exists(location) ? FileSystem.GetFiles(location).Select(f => f.CreationTimeUtc > f.LastWriteTimeUtc ? f.CreationTimeUtc : f.LastWriteTimeUtc).Max() : DateTime.MinValue;
  62. }
  63. /// <summary>
  64. /// The us culture
  65. /// </summary>
  66. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  67. /// <summary>
  68. /// Gets the location.
  69. /// </summary>
  70. /// <param name="item">The item.</param>
  71. /// <returns>System.String.</returns>
  72. protected string GetLocation(BaseItem item)
  73. {
  74. var invalid = Path.GetInvalidFileNameChars();
  75. var name = item.Name ?? string.Empty;
  76. name = invalid.Aggregate(name, (current, c) => current.Replace(c.ToString(UsCulture), string.Empty));
  77. return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
  78. }
  79. /// <summary>
  80. /// Gets the image.
  81. /// </summary>
  82. /// <param name="item">The item.</param>
  83. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  84. /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
  85. protected override WIN32_FIND_DATA? GetImage(BaseItem item, string filenameWithoutExtension)
  86. {
  87. var location = GetLocation(item);
  88. var result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".png"));
  89. if (!result.HasValue)
  90. result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".jpg"));
  91. return result;
  92. }
  93. }
  94. }