2
0

BaseDynamicImageProvider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Controller.Drawing;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Playlists;
  7. using MediaBrowser.Controller.Providers;
  8. using MediaBrowser.Model.Entities;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Controller.Entities.Audio;
  17. using MediaBrowser.Controller.IO;
  18. using MediaBrowser.Model.Configuration;
  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 : IHasMetadata
  24. {
  25. protected IFileSystem FileSystem { get; private set; }
  26. protected IProviderManager ProviderManager { get; private set; }
  27. protected IApplicationPaths ApplicationPaths { get; private set; }
  28. protected IImageProcessor ImageProcessor { get; set; }
  29. protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths applicationPaths, IImageProcessor imageProcessor)
  30. {
  31. ApplicationPaths = applicationPaths;
  32. ProviderManager = providerManager;
  33. FileSystem = fileSystem;
  34. ImageProcessor = imageProcessor;
  35. }
  36. protected virtual bool Supports(IHasMetadata item)
  37. {
  38. return true;
  39. }
  40. public virtual IEnumerable<ImageType> GetSupportedImages(IHasMetadata item)
  41. {
  42. return new List<ImageType>
  43. {
  44. ImageType.Primary,
  45. ImageType.Thumb
  46. };
  47. }
  48. private IEnumerable<ImageType> GetEnabledImages(IHasMetadata item)
  49. {
  50. //var options = ProviderManager.GetMetadataOptions(item);
  51. return GetSupportedImages(item);
  52. //return GetSupportedImages(item).Where(i => IsEnabled(options, i, item)).ToList();
  53. }
  54. public async Task<ItemUpdateType> FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  55. {
  56. if (!Supports(item))
  57. {
  58. return ItemUpdateType.None;
  59. }
  60. var updateType = ItemUpdateType.None;
  61. var supportedImages = GetEnabledImages(item).ToList();
  62. if (supportedImages.Contains(ImageType.Primary))
  63. {
  64. var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false);
  65. updateType = updateType | primaryResult;
  66. }
  67. if (supportedImages.Contains(ImageType.Thumb))
  68. {
  69. var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false);
  70. updateType = updateType | thumbResult;
  71. }
  72. return updateType;
  73. }
  74. protected async Task<ItemUpdateType> FetchAsync(IHasMetadata item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
  75. {
  76. var image = item.GetImageInfo(imageType, 0);
  77. if (image != null)
  78. {
  79. if (!image.IsLocalFile)
  80. {
  81. return ItemUpdateType.None;
  82. }
  83. if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
  84. {
  85. return ItemUpdateType.None;
  86. }
  87. }
  88. var items = GetItemsWithImages(item);
  89. return await FetchToFileInternal(item, items, imageType, cancellationToken).ConfigureAwait(false);
  90. }
  91. protected async Task<ItemUpdateType> FetchToFileInternal(IHasMetadata item,
  92. List<BaseItem> itemsWithImages,
  93. ImageType imageType,
  94. CancellationToken cancellationToken)
  95. {
  96. var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N"));
  97. FileSystem.CreateDirectory(FileSystem.GetDirectoryName(outputPathWithoutExtension));
  98. string outputPath = CreateImage(item, itemsWithImages, outputPathWithoutExtension, imageType, 0);
  99. if (string.IsNullOrWhiteSpace(outputPath))
  100. {
  101. return ItemUpdateType.None;
  102. }
  103. var mimeType = MimeTypes.GetMimeType(outputPath);
  104. if (string.Equals(mimeType, "application/octet-stream", StringComparison.OrdinalIgnoreCase))
  105. {
  106. mimeType = "image/png";
  107. }
  108. await ProviderManager.SaveImage(item, outputPath, mimeType, imageType, null, false, cancellationToken).ConfigureAwait(false);
  109. return ItemUpdateType.ImageUpdate;
  110. }
  111. protected abstract List<BaseItem> GetItemsWithImages(IHasMetadata item);
  112. protected string CreateThumbCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath)
  113. {
  114. return CreateCollage(primaryItem, items, outputPath, 640, 360);
  115. }
  116. protected virtual IEnumerable<string> GetStripCollageImagePaths(IHasMetadata primaryItem, IEnumerable<BaseItem> items)
  117. {
  118. return items
  119. .Select(i =>
  120. {
  121. var image = i.GetImageInfo(ImageType.Primary, 0);
  122. if (image != null && image.IsLocalFile)
  123. {
  124. return image.Path;
  125. }
  126. image = i.GetImageInfo(ImageType.Thumb, 0);
  127. if (image != null && image.IsLocalFile)
  128. {
  129. return image.Path;
  130. }
  131. return null;
  132. })
  133. .Where(i => !string.IsNullOrWhiteSpace(i));
  134. }
  135. protected string CreatePosterCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath)
  136. {
  137. return CreateCollage(primaryItem, items, outputPath, 400, 600);
  138. }
  139. protected string CreateSquareCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath)
  140. {
  141. return CreateCollage(primaryItem, items, outputPath, 600, 600);
  142. }
  143. protected string CreateThumbCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath, int width, int height)
  144. {
  145. return CreateCollage(primaryItem, items, outputPath, width, height);
  146. }
  147. private string CreateCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath, int width, int height)
  148. {
  149. FileSystem.CreateDirectory(FileSystem.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.Length == 0)
  158. {
  159. return null;
  160. }
  161. if (!ImageProcessor.SupportsImageCollageCreation)
  162. {
  163. return null;
  164. }
  165. ImageProcessor.CreateImageCollage(options);
  166. return outputPath;
  167. }
  168. public string Name
  169. {
  170. get { return "Dynamic Image Provider"; }
  171. }
  172. protected virtual string CreateImage(IHasMetadata item,
  173. List<BaseItem> itemsWithImages,
  174. string outputPathWithoutExtension,
  175. ImageType imageType,
  176. int imageIndex)
  177. {
  178. if (itemsWithImages.Count == 0)
  179. {
  180. return null;
  181. }
  182. string outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png");
  183. if (imageType == ImageType.Thumb)
  184. {
  185. return CreateThumbCollage(item, itemsWithImages, outputPath);
  186. }
  187. if (imageType == ImageType.Primary)
  188. {
  189. if (item is UserView)
  190. {
  191. return CreateSquareCollage(item, itemsWithImages, outputPath);
  192. }
  193. if (item is Playlist || item is MusicGenre || item is Genre || item is GameGenre || item is PhotoAlbum)
  194. {
  195. return CreateSquareCollage(item, itemsWithImages, outputPath);
  196. }
  197. return CreatePosterCollage(item, itemsWithImages, outputPath);
  198. }
  199. throw new ArgumentException("Unexpected image type");
  200. }
  201. protected virtual int MaxImageAgeDays
  202. {
  203. get { return 7; }
  204. }
  205. public bool HasChanged(IHasMetadata item, IDirectoryService directoryServicee)
  206. {
  207. if (!Supports(item))
  208. {
  209. return false;
  210. }
  211. var supportedImages = GetEnabledImages(item).ToList();
  212. if (supportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary))
  213. {
  214. return true;
  215. }
  216. if (supportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb))
  217. {
  218. return true;
  219. }
  220. return false;
  221. }
  222. protected bool HasChanged(IHasMetadata item, ImageType type)
  223. {
  224. var image = item.GetImageInfo(type, 0);
  225. if (image != null)
  226. {
  227. if (!image.IsLocalFile)
  228. {
  229. return false;
  230. }
  231. if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
  232. {
  233. return false;
  234. }
  235. var age = DateTime.UtcNow - image.DateModified;
  236. if (age.TotalDays <= MaxImageAgeDays)
  237. {
  238. return false;
  239. }
  240. }
  241. return true;
  242. }
  243. protected List<BaseItem> GetFinalItems(IEnumerable<BaseItem> items)
  244. {
  245. return GetFinalItems(items, 4);
  246. }
  247. protected virtual List<BaseItem> GetFinalItems(IEnumerable<BaseItem> items, int limit)
  248. {
  249. return items
  250. .OrderBy(i => Guid.NewGuid())
  251. .Take(limit)
  252. .ToList();
  253. }
  254. public int Order
  255. {
  256. get
  257. {
  258. // Run before the default image provider which will download placeholders
  259. return 0;
  260. }
  261. }
  262. protected string CreateSingleImage(List<BaseItem> itemsWithImages, string outputPathWithoutExtension, ImageType imageType)
  263. {
  264. var image = itemsWithImages
  265. .Where(i => i.HasImage(imageType) && i.GetImageInfo(imageType, 0).IsLocalFile && Path.HasExtension(i.GetImagePath(imageType)))
  266. .Select(i => i.GetImagePath(imageType))
  267. .FirstOrDefault();
  268. if (string.IsNullOrWhiteSpace(image))
  269. {
  270. return null;
  271. }
  272. var ext = Path.GetExtension(image);
  273. var outputPath = Path.ChangeExtension(outputPathWithoutExtension, ext);
  274. FileSystem.CopyFile(image, outputPath, true);
  275. return outputPath;
  276. }
  277. }
  278. }