2
0

ImageFromMediaLocationProvider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.Composition;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Controller.Providers
  9. {
  10. /// <summary>
  11. /// Provides images for all types by looking for standard images - folder, backdrop, logo, etc.
  12. /// </summary>
  13. [Export(typeof(BaseMetadataProvider))]
  14. public class ImageFromMediaLocationProvider : BaseMetadataProvider
  15. {
  16. public override bool Supports(BaseEntity item)
  17. {
  18. return true;
  19. }
  20. public override MetadataProviderPriority Priority
  21. {
  22. get { return MetadataProviderPriority.First; }
  23. }
  24. public override Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
  25. {
  26. if (args.IsDirectory)
  27. {
  28. var baseItem = item as BaseItem;
  29. if (baseItem != null)
  30. {
  31. return Task.Run(() => PopulateBaseItemImages(baseItem, args));
  32. }
  33. return Task.Run(() => PopulateImages(item, args));
  34. }
  35. return Task.FromResult<object>(null);
  36. }
  37. /// <summary>
  38. /// Fills in image paths based on files win the folder
  39. /// </summary>
  40. private void PopulateImages(BaseEntity item, ItemResolveEventArgs args)
  41. {
  42. for (int i = 0; i < args.FileSystemChildren.Length; i++)
  43. {
  44. var file = args.FileSystemChildren[i];
  45. string filePath = file.Path;
  46. string ext = Path.GetExtension(filePath);
  47. // Only support png and jpg files
  48. if (!ext.EndsWith("png", StringComparison.OrdinalIgnoreCase) && !ext.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
  49. {
  50. continue;
  51. }
  52. string name = Path.GetFileNameWithoutExtension(filePath);
  53. if (name.Equals("folder", StringComparison.OrdinalIgnoreCase))
  54. {
  55. item.PrimaryImagePath = filePath;
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Fills in image paths based on files win the folder
  61. /// </summary>
  62. private void PopulateBaseItemImages(BaseItem item, ItemResolveEventArgs args)
  63. {
  64. var backdropFiles = new List<string>();
  65. for (int i = 0; i < args.FileSystemChildren.Length; i++)
  66. {
  67. var file = args.FileSystemChildren[i];
  68. string filePath = file.Path;
  69. string ext = Path.GetExtension(filePath);
  70. // Only support png and jpg files
  71. if (!ext.EndsWith("png", StringComparison.OrdinalIgnoreCase) && !ext.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
  72. {
  73. continue;
  74. }
  75. string name = Path.GetFileNameWithoutExtension(filePath);
  76. if (name.Equals("folder", StringComparison.OrdinalIgnoreCase))
  77. {
  78. item.PrimaryImagePath = filePath;
  79. }
  80. else if (name.StartsWith("backdrop", StringComparison.OrdinalIgnoreCase))
  81. {
  82. backdropFiles.Add(filePath);
  83. }
  84. if (name.Equals("logo", StringComparison.OrdinalIgnoreCase))
  85. {
  86. item.LogoImagePath = filePath;
  87. }
  88. if (name.Equals("banner", StringComparison.OrdinalIgnoreCase))
  89. {
  90. item.BannerImagePath = filePath;
  91. }
  92. if (name.Equals("clearart", StringComparison.OrdinalIgnoreCase))
  93. {
  94. item.ArtImagePath = filePath;
  95. }
  96. if (name.Equals("thumb", StringComparison.OrdinalIgnoreCase))
  97. {
  98. item.ThumbnailImagePath = filePath;
  99. }
  100. }
  101. if (backdropFiles.Count > 0)
  102. {
  103. item.BackdropImagePaths = backdropFiles;
  104. }
  105. }
  106. }
  107. }