2
0

BaseDynamicImageProvider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 : BaseItem
  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(BaseItem item)
  37. {
  38. return true;
  39. }
  40. public virtual ImageType[] GetSupportedImages(BaseItem item)
  41. {
  42. return new ImageType[]
  43. {
  44. ImageType.Primary
  45. };
  46. }
  47. public async Task<ItemUpdateType> FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  48. {
  49. if (!Supports(item))
  50. {
  51. return ItemUpdateType.None;
  52. }
  53. var updateType = ItemUpdateType.None;
  54. var supportedImages = GetSupportedImages(item);
  55. if (supportedImages.Contains(ImageType.Primary))
  56. {
  57. var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false);
  58. updateType = updateType | primaryResult;
  59. }
  60. if (supportedImages.Contains(ImageType.Thumb))
  61. {
  62. var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false);
  63. updateType = updateType | thumbResult;
  64. }
  65. return updateType;
  66. }
  67. protected Task<ItemUpdateType> FetchAsync(BaseItem item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
  68. {
  69. var image = item.GetImageInfo(imageType, 0);
  70. if (image != null)
  71. {
  72. if (!image.IsLocalFile)
  73. {
  74. return Task.FromResult(ItemUpdateType.None);
  75. }
  76. if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
  77. {
  78. return Task.FromResult(ItemUpdateType.None);
  79. }
  80. }
  81. var items = GetItemsWithImages(item);
  82. return FetchToFileInternal(item, items, imageType, cancellationToken);
  83. }
  84. protected async Task<ItemUpdateType> FetchToFileInternal(BaseItem item,
  85. List<BaseItem> itemsWithImages,
  86. ImageType imageType,
  87. CancellationToken cancellationToken)
  88. {
  89. var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N"));
  90. FileSystem.CreateDirectory(FileSystem.GetDirectoryName(outputPathWithoutExtension));
  91. string outputPath = CreateImage(item, itemsWithImages, outputPathWithoutExtension, imageType, 0);
  92. if (string.IsNullOrEmpty(outputPath))
  93. {
  94. return ItemUpdateType.None;
  95. }
  96. var mimeType = MimeTypes.GetMimeType(outputPath);
  97. if (string.Equals(mimeType, "application/octet-stream", StringComparison.OrdinalIgnoreCase))
  98. {
  99. mimeType = "image/png";
  100. }
  101. await ProviderManager.SaveImage(item, outputPath, mimeType, imageType, null, false, cancellationToken).ConfigureAwait(false);
  102. return ItemUpdateType.ImageUpdate;
  103. }
  104. protected abstract List<BaseItem> GetItemsWithImages(BaseItem item);
  105. protected string CreateThumbCollage(BaseItem primaryItem, List<BaseItem> items, string outputPath)
  106. {
  107. return CreateCollage(primaryItem, items, outputPath, 640, 360);
  108. }
  109. protected virtual IEnumerable<string> GetStripCollageImagePaths(BaseItem primaryItem, IEnumerable<BaseItem> items)
  110. {
  111. return items
  112. .Select(i =>
  113. {
  114. var image = i.GetImageInfo(ImageType.Primary, 0);
  115. if (image != null && image.IsLocalFile)
  116. {
  117. return image.Path;
  118. }
  119. image = i.GetImageInfo(ImageType.Thumb, 0);
  120. if (image != null && image.IsLocalFile)
  121. {
  122. return image.Path;
  123. }
  124. return null;
  125. })
  126. .Where(i => !string.IsNullOrEmpty(i));
  127. }
  128. protected string CreatePosterCollage(BaseItem primaryItem, List<BaseItem> items, string outputPath)
  129. {
  130. return CreateCollage(primaryItem, items, outputPath, 400, 600);
  131. }
  132. protected string CreateSquareCollage(BaseItem primaryItem, List<BaseItem> items, string outputPath)
  133. {
  134. return CreateCollage(primaryItem, items, outputPath, 600, 600);
  135. }
  136. protected string CreateThumbCollage(BaseItem primaryItem, List<BaseItem> items, string outputPath, int width, int height)
  137. {
  138. return CreateCollage(primaryItem, items, outputPath, width, height);
  139. }
  140. private string CreateCollage(BaseItem primaryItem, List<BaseItem> items, string outputPath, int width, int height)
  141. {
  142. FileSystem.CreateDirectory(FileSystem.GetDirectoryName(outputPath));
  143. var options = new ImageCollageOptions
  144. {
  145. Height = height,
  146. Width = width,
  147. OutputPath = outputPath,
  148. InputPaths = GetStripCollageImagePaths(primaryItem, items).ToArray()
  149. };
  150. if (options.InputPaths.Length == 0)
  151. {
  152. return null;
  153. }
  154. if (!ImageProcessor.SupportsImageCollageCreation)
  155. {
  156. return null;
  157. }
  158. ImageProcessor.CreateImageCollage(options);
  159. return outputPath;
  160. }
  161. public string Name
  162. {
  163. get { return "Dynamic Image Provider"; }
  164. }
  165. protected virtual string CreateImage(BaseItem item,
  166. List<BaseItem> itemsWithImages,
  167. string outputPathWithoutExtension,
  168. ImageType imageType,
  169. int imageIndex)
  170. {
  171. if (itemsWithImages.Count == 0)
  172. {
  173. return null;
  174. }
  175. string outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png");
  176. if (imageType == ImageType.Thumb)
  177. {
  178. return CreateThumbCollage(item, itemsWithImages, outputPath);
  179. }
  180. if (imageType == ImageType.Primary)
  181. {
  182. if (item is UserView)
  183. {
  184. return CreateSquareCollage(item, itemsWithImages, outputPath);
  185. }
  186. if (item is Playlist || item is MusicGenre || item is Genre || item is GameGenre || item is PhotoAlbum)
  187. {
  188. return CreateSquareCollage(item, itemsWithImages, outputPath);
  189. }
  190. return CreatePosterCollage(item, itemsWithImages, outputPath);
  191. }
  192. throw new ArgumentException("Unexpected image type");
  193. }
  194. protected virtual int MaxImageAgeDays
  195. {
  196. get { return 7; }
  197. }
  198. public bool HasChanged(BaseItem item, IDirectoryService directoryServicee)
  199. {
  200. if (!Supports(item))
  201. {
  202. return false;
  203. }
  204. var supportedImages = GetSupportedImages(item);
  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 != 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. if (age.TotalDays <= MaxImageAgeDays)
  239. {
  240. return false;
  241. }
  242. return true;
  243. }
  244. public int Order
  245. {
  246. get
  247. {
  248. // Run before the default image provider which will download placeholders
  249. return 0;
  250. }
  251. }
  252. protected string CreateSingleImage(List<BaseItem> itemsWithImages, string outputPathWithoutExtension, ImageType imageType)
  253. {
  254. var image = itemsWithImages
  255. .Where(i => i.HasImage(imageType) && i.GetImageInfo(imageType, 0).IsLocalFile && Path.HasExtension(i.GetImagePath(imageType)))
  256. .Select(i => i.GetImagePath(imageType))
  257. .FirstOrDefault();
  258. if (string.IsNullOrEmpty(image))
  259. {
  260. return null;
  261. }
  262. var ext = Path.GetExtension(image);
  263. var outputPath = Path.ChangeExtension(outputPathWithoutExtension, ext);
  264. FileSystem.CopyFile(image, outputPath, true);
  265. return outputPath;
  266. }
  267. }
  268. }