ImagesByNameProvider.cs 3.6 KB

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