BaseDynamicImageProvider.cs 11 KB

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