ImagesByNameProvider.cs 3.6 KB

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