ImagesByNameProvider.cs 3.7 KB

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