ImagesByNameProvider.cs 3.7 KB

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