ImagesByNameProvider.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Logging;
  7. using System;
  8. using System.IO;
  9. using System.Linq;
  10. namespace MediaBrowser.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, IFileSystem fileSystem)
  18. : base(logManager, configurationManager, fileSystem)
  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 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. /// Gets the location.
  44. /// </summary>
  45. /// <param name="item">The item.</param>
  46. /// <returns>System.String.</returns>
  47. protected string GetLocation(BaseItem item)
  48. {
  49. var name = FileSystem.GetValidFilename(item.Name);
  50. return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
  51. }
  52. /// <summary>
  53. /// Gets the image.
  54. /// </summary>
  55. /// <param name="item">The item.</param>
  56. /// <param name="args">The args.</param>
  57. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  58. /// <returns>FileSystemInfo.</returns>
  59. protected override FileSystemInfo GetImage(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension)
  60. {
  61. var location = GetLocation(item);
  62. return GetImageFromLocation(location, filenameWithoutExtension);
  63. }
  64. protected override Guid GetFileSystemStamp(BaseItem item)
  65. {
  66. var location = GetLocation(item);
  67. try
  68. {
  69. var files = new DirectoryInfo(location)
  70. .EnumerateFiles("*", SearchOption.TopDirectoryOnly)
  71. .Where(i =>
  72. {
  73. var ext = i.Extension;
  74. return !string.IsNullOrEmpty(ext) &&
  75. BaseItem.SupportedImageExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  76. })
  77. .ToList();
  78. return GetFileSystemStamp(files);
  79. }
  80. catch (DirectoryNotFoundException)
  81. {
  82. // User doesn't have the folder. No need to log or blow up
  83. return Guid.Empty;
  84. }
  85. }
  86. }
  87. }