ImagesByNameProvider.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Collections.Generic;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Model.Logging;
  8. using System;
  9. using System.IO;
  10. using System.Linq;
  11. namespace MediaBrowser.Providers
  12. {
  13. /// <summary>
  14. /// Provides images for generic types by looking for standard images in the IBN
  15. /// </summary>
  16. public class ImagesByNameProvider : ImageFromMediaLocationProvider
  17. {
  18. public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem)
  19. : base(logManager, configurationManager, fileSystem)
  20. {
  21. }
  22. /// <summary>
  23. /// Supportses the specified item.
  24. /// </summary>
  25. /// <param name="item">The item.</param>
  26. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  27. public override bool Supports(BaseItem item)
  28. {
  29. // Only run for these generic types since we are expensive in file i/o
  30. return item is BasePluginFolder || item is CollectionFolder;
  31. }
  32. /// <summary>
  33. /// Gets the priority.
  34. /// </summary>
  35. /// <value>The priority.</value>
  36. public override MetadataProviderPriority Priority
  37. {
  38. get
  39. {
  40. return MetadataProviderPriority.Last;
  41. }
  42. }
  43. /// <summary>
  44. /// Gets the location.
  45. /// </summary>
  46. /// <param name="item">The item.</param>
  47. /// <returns>System.String.</returns>
  48. protected string GetLocation(BaseItem item)
  49. {
  50. var name = FileSystem.GetValidFilename(item.Name);
  51. return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
  52. }
  53. /// <summary>
  54. /// Gets the image.
  55. /// </summary>
  56. /// <param name="item">The item.</param>
  57. /// <param name="args">The args.</param>
  58. /// <param name="filenameWithoutExtension">The filename without extension.</param>
  59. /// <returns>FileSystemInfo.</returns>
  60. protected override FileSystemInfo GetImage(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension)
  61. {
  62. var location = GetLocation(item);
  63. return GetImageFromLocation(location, filenameWithoutExtension);
  64. }
  65. protected override Guid GetFileSystemStamp(IEnumerable<BaseItem> items)
  66. {
  67. var location = GetLocation(items.First());
  68. try
  69. {
  70. var files = new DirectoryInfo(location)
  71. .EnumerateFiles("*", SearchOption.TopDirectoryOnly)
  72. .Where(i =>
  73. {
  74. var ext = i.Extension;
  75. return !string.IsNullOrEmpty(ext) &&
  76. BaseItem.SupportedImageExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
  77. })
  78. .ToList();
  79. return GetFileSystemStamp(files);
  80. }
  81. catch (DirectoryNotFoundException)
  82. {
  83. // User doesn't have the folder. No need to log or blow up
  84. return Guid.Empty;
  85. }
  86. }
  87. }
  88. }