BaseDynamicImageProvider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net.Mime;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Common.Configuration;
  12. using MediaBrowser.Controller.Drawing;
  13. using MediaBrowser.Controller.Entities;
  14. using MediaBrowser.Controller.Entities.Audio;
  15. using MediaBrowser.Controller.Library;
  16. using MediaBrowser.Controller.Playlists;
  17. using MediaBrowser.Controller.Providers;
  18. using MediaBrowser.Model.Entities;
  19. using MediaBrowser.Model.IO;
  20. using MediaBrowser.Model.Net;
  21. namespace Emby.Server.Implementations.Images
  22. {
  23. public abstract class BaseDynamicImageProvider<T> : IHasItemChangeMonitor, IForcedProvider, ICustomMetadataProvider<T>, IHasOrder
  24. where T : BaseItem
  25. {
  26. protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths applicationPaths, IImageProcessor imageProcessor)
  27. {
  28. ApplicationPaths = applicationPaths;
  29. ProviderManager = providerManager;
  30. FileSystem = fileSystem;
  31. ImageProcessor = imageProcessor;
  32. }
  33. protected IFileSystem FileSystem { get; }
  34. protected IProviderManager ProviderManager { get; }
  35. protected IApplicationPaths ApplicationPaths { get; }
  36. protected IImageProcessor ImageProcessor { get; set; }
  37. protected virtual IReadOnlyCollection<ImageType> SupportedImages { get; }
  38. = new ImageType[] { ImageType.Primary };
  39. /// <inheritdoc />
  40. public string Name => "Dynamic Image Provider";
  41. protected virtual int MaxImageAgeDays => 7;
  42. public int Order => 0;
  43. protected virtual bool Supports(BaseItem item) => true;
  44. public async Task<ItemUpdateType> FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  45. {
  46. if (!Supports(item))
  47. {
  48. return ItemUpdateType.None;
  49. }
  50. var updateType = ItemUpdateType.None;
  51. if (SupportedImages.Contains(ImageType.Primary))
  52. {
  53. var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false);
  54. updateType |= primaryResult;
  55. }
  56. if (SupportedImages.Contains(ImageType.Thumb))
  57. {
  58. var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false);
  59. updateType |= thumbResult;
  60. }
  61. return updateType;
  62. }
  63. protected Task<ItemUpdateType> FetchAsync(BaseItem item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
  64. {
  65. var image = item.GetImageInfo(imageType, 0);
  66. if (image is not null)
  67. {
  68. if (!image.IsLocalFile)
  69. {
  70. return Task.FromResult(ItemUpdateType.None);
  71. }
  72. if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
  73. {
  74. return Task.FromResult(ItemUpdateType.None);
  75. }
  76. }
  77. var items = GetItemsWithImages(item);
  78. return FetchToFileInternal(item, items, imageType, cancellationToken);
  79. }
  80. protected async Task<ItemUpdateType> FetchToFileInternal(
  81. BaseItem item,
  82. IReadOnlyList<BaseItem> itemsWithImages,
  83. ImageType imageType,
  84. CancellationToken cancellationToken)
  85. {
  86. var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
  87. Directory.CreateDirectory(Path.GetDirectoryName(outputPathWithoutExtension));
  88. string outputPath = CreateImage(item, itemsWithImages, outputPathWithoutExtension, imageType, 0);
  89. if (string.IsNullOrEmpty(outputPath))
  90. {
  91. return ItemUpdateType.None;
  92. }
  93. var mimeType = MimeTypes.GetMimeType(outputPath);
  94. if (string.Equals(mimeType, MediaTypeNames.Application.Octet, StringComparison.OrdinalIgnoreCase))
  95. {
  96. mimeType = MediaTypeNames.Image.Png;
  97. }
  98. await ProviderManager.SaveImage(item, outputPath, mimeType, imageType, null, false, cancellationToken).ConfigureAwait(false);
  99. return ItemUpdateType.ImageUpdate;
  100. }
  101. protected abstract IReadOnlyList<BaseItem> GetItemsWithImages(BaseItem item);
  102. protected string CreateThumbCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath)
  103. {
  104. return CreateCollage(primaryItem, items, outputPath, 640, 360);
  105. }
  106. protected virtual IEnumerable<string> GetStripCollageImagePaths(BaseItem primaryItem, IEnumerable<BaseItem> items)
  107. {
  108. var useBackdrop = primaryItem is CollectionFolder || primaryItem is UserView;
  109. return items
  110. .Select(i =>
  111. {
  112. // Use Backdrop instead of Primary image for Library images.
  113. if (useBackdrop)
  114. {
  115. var backdrop = i.GetImageInfo(ImageType.Backdrop, 0);
  116. if (backdrop is not null && backdrop.IsLocalFile)
  117. {
  118. return backdrop.Path;
  119. }
  120. }
  121. var image = i.GetImageInfo(ImageType.Primary, 0);
  122. if (image is not null && image.IsLocalFile)
  123. {
  124. return image.Path;
  125. }
  126. image = i.GetImageInfo(ImageType.Thumb, 0);
  127. if (image is not null && image.IsLocalFile)
  128. {
  129. return image.Path;
  130. }
  131. return null;
  132. })
  133. .Where(i => !string.IsNullOrEmpty(i));
  134. }
  135. protected string CreatePosterCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath)
  136. {
  137. return CreateCollage(primaryItem, items, outputPath, 400, 600);
  138. }
  139. protected string CreateSquareCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath)
  140. {
  141. return CreateCollage(primaryItem, items, outputPath, 600, 600);
  142. }
  143. protected string CreateThumbCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath, int width, int height)
  144. {
  145. return CreateCollage(primaryItem, items, outputPath, width, height);
  146. }
  147. private string CreateCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath, int width, int height)
  148. {
  149. Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
  150. var options = new ImageCollageOptions
  151. {
  152. Height = height,
  153. Width = width,
  154. OutputPath = outputPath,
  155. InputPaths = GetStripCollageImagePaths(primaryItem, items).ToArray()
  156. };
  157. if (options.InputPaths.Count == 0)
  158. {
  159. return null;
  160. }
  161. if (!ImageProcessor.SupportsImageCollageCreation)
  162. {
  163. return null;
  164. }
  165. ImageProcessor.CreateImageCollage(options, primaryItem.Name);
  166. return outputPath;
  167. }
  168. protected virtual string CreateImage(
  169. BaseItem item,
  170. IReadOnlyCollection<BaseItem> itemsWithImages,
  171. string outputPathWithoutExtension,
  172. ImageType imageType,
  173. int imageIndex)
  174. {
  175. if (itemsWithImages.Count == 0)
  176. {
  177. return null;
  178. }
  179. string outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png");
  180. if (imageType == ImageType.Thumb)
  181. {
  182. return CreateThumbCollage(item, itemsWithImages, outputPath);
  183. }
  184. if (imageType == ImageType.Primary)
  185. {
  186. if (item is UserView
  187. || item is Playlist
  188. || item is MusicGenre
  189. || item is Genre
  190. || item is PhotoAlbum
  191. || item is MusicArtist)
  192. {
  193. return CreateSquareCollage(item, itemsWithImages, outputPath);
  194. }
  195. return CreatePosterCollage(item, itemsWithImages, outputPath);
  196. }
  197. throw new ArgumentException("Unexpected image type", nameof(imageType));
  198. }
  199. public bool HasChanged(BaseItem item, IDirectoryService directoryService)
  200. {
  201. if (!Supports(item))
  202. {
  203. return false;
  204. }
  205. if (SupportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary))
  206. {
  207. return true;
  208. }
  209. if (SupportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb))
  210. {
  211. return true;
  212. }
  213. return false;
  214. }
  215. protected bool HasChanged(BaseItem item, ImageType type)
  216. {
  217. var image = item.GetImageInfo(type, 0);
  218. if (image is not null)
  219. {
  220. if (!image.IsLocalFile)
  221. {
  222. return false;
  223. }
  224. if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
  225. {
  226. return false;
  227. }
  228. if (!HasChangedByDate(item, image))
  229. {
  230. return false;
  231. }
  232. }
  233. return true;
  234. }
  235. protected virtual bool HasChangedByDate(BaseItem item, ItemImageInfo image)
  236. {
  237. var age = DateTime.UtcNow - image.DateModified;
  238. return age.TotalDays > MaxImageAgeDays;
  239. }
  240. protected string CreateSingleImage(IEnumerable<BaseItem> itemsWithImages, string outputPathWithoutExtension, ImageType imageType)
  241. {
  242. var image = itemsWithImages
  243. .Where(i => i.HasImage(imageType) && i.GetImageInfo(imageType, 0).IsLocalFile && Path.HasExtension(i.GetImagePath(imageType)))
  244. .Select(i => i.GetImagePath(imageType))
  245. .FirstOrDefault();
  246. if (string.IsNullOrEmpty(image))
  247. {
  248. return null;
  249. }
  250. var ext = Path.GetExtension(image);
  251. var outputPath = Path.ChangeExtension(outputPathWithoutExtension, ext);
  252. File.Copy(image, outputPath, true);
  253. return outputPath;
  254. }
  255. }
  256. }