BaseDynamicImageProvider.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.Common.IO;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Controller.Entities.Audio;
  18. using MediaBrowser.Controller.IO;
  19. using MediaBrowser.Model.Configuration;
  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(IHasImages item)
  37. {
  38. return true;
  39. }
  40. public virtual IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  41. {
  42. return new List<ImageType>
  43. {
  44. ImageType.Primary,
  45. ImageType.Thumb
  46. };
  47. }
  48. private IEnumerable<ImageType> GetEnabledImages(IHasImages 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. private bool IsEnabled(MetadataOptions options, ImageType type, IHasImages item)
  55. {
  56. if (type == ImageType.Backdrop)
  57. {
  58. if (item.LockedFields.Contains(MetadataFields.Backdrops))
  59. {
  60. return false;
  61. }
  62. }
  63. else if (type == ImageType.Screenshot)
  64. {
  65. if (item.LockedFields.Contains(MetadataFields.Screenshots))
  66. {
  67. return false;
  68. }
  69. }
  70. else
  71. {
  72. if (item.LockedFields.Contains(MetadataFields.Images))
  73. {
  74. return false;
  75. }
  76. }
  77. return options.IsEnabled(type);
  78. }
  79. public async Task<ItemUpdateType> FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  80. {
  81. if (!Supports(item))
  82. {
  83. return ItemUpdateType.None;
  84. }
  85. var updateType = ItemUpdateType.None;
  86. var supportedImages = GetEnabledImages(item).ToList();
  87. if (supportedImages.Contains(ImageType.Primary))
  88. {
  89. var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false);
  90. updateType = updateType | primaryResult;
  91. }
  92. if (supportedImages.Contains(ImageType.Thumb))
  93. {
  94. var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false);
  95. updateType = updateType | thumbResult;
  96. }
  97. return updateType;
  98. }
  99. protected async Task<ItemUpdateType> FetchAsync(IHasImages item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
  100. {
  101. var image = item.GetImageInfo(imageType, 0);
  102. if (image != null)
  103. {
  104. if (!image.IsLocalFile)
  105. {
  106. return ItemUpdateType.None;
  107. }
  108. if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
  109. {
  110. return ItemUpdateType.None;
  111. }
  112. }
  113. var items = await GetItemsWithImages(item).ConfigureAwait(false);
  114. return await FetchToFileInternal(item, items, imageType, cancellationToken).ConfigureAwait(false);
  115. }
  116. protected async Task<ItemUpdateType> FetchToFileInternal(IHasImages item,
  117. List<BaseItem> itemsWithImages,
  118. ImageType imageType,
  119. CancellationToken cancellationToken)
  120. {
  121. var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N"));
  122. FileSystem.CreateDirectory(Path.GetDirectoryName(outputPathWithoutExtension));
  123. string outputPath = await CreateImage(item, itemsWithImages, outputPathWithoutExtension, imageType, 0).ConfigureAwait(false);
  124. if (string.IsNullOrWhiteSpace(outputPath))
  125. {
  126. return ItemUpdateType.None;
  127. }
  128. await ProviderManager.SaveImage(item, outputPath, "image/png", imageType, null, false, cancellationToken).ConfigureAwait(false);
  129. return ItemUpdateType.ImageUpdate;
  130. }
  131. protected abstract Task<List<BaseItem>> GetItemsWithImages(IHasImages item);
  132. protected Task<string> CreateThumbCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
  133. {
  134. return CreateCollage(primaryItem, items, outputPath, 640, 360);
  135. }
  136. protected virtual IEnumerable<string> GetStripCollageImagePaths(IHasImages primaryItem, IEnumerable<BaseItem> items)
  137. {
  138. return items
  139. .Select(i =>
  140. {
  141. var image = i.GetImageInfo(ImageType.Primary, 0);
  142. if (image != null && image.IsLocalFile)
  143. {
  144. return image.Path;
  145. }
  146. image = i.GetImageInfo(ImageType.Thumb, 0);
  147. if (image != null && image.IsLocalFile)
  148. {
  149. return image.Path;
  150. }
  151. return null;
  152. })
  153. .Where(i => !string.IsNullOrWhiteSpace(i));
  154. }
  155. protected Task<string> CreatePosterCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
  156. {
  157. return CreateCollage(primaryItem, items, outputPath, 400, 600);
  158. }
  159. protected Task<string> CreateSquareCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
  160. {
  161. return CreateCollage(primaryItem, items, outputPath, 600, 600);
  162. }
  163. protected Task<string> CreateThumbCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, int width, int height)
  164. {
  165. return CreateCollage(primaryItem, items, outputPath, width, height);
  166. }
  167. private async Task<string> CreateCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, int width, int height)
  168. {
  169. FileSystem.CreateDirectory(Path.GetDirectoryName(outputPath));
  170. var options = new ImageCollageOptions
  171. {
  172. Height = height,
  173. Width = width,
  174. OutputPath = outputPath,
  175. InputPaths = GetStripCollageImagePaths(primaryItem, items).ToArray()
  176. };
  177. if (options.InputPaths.Length == 0)
  178. {
  179. return null;
  180. }
  181. if (!ImageProcessor.SupportsImageCollageCreation)
  182. {
  183. return null;
  184. }
  185. await ImageProcessor.CreateImageCollage(options).ConfigureAwait(false);
  186. return outputPath;
  187. }
  188. public string Name
  189. {
  190. get { return "Dynamic Image Provider"; }
  191. }
  192. protected virtual async Task<string> CreateImage(IHasImages item,
  193. List<BaseItem> itemsWithImages,
  194. string outputPathWithoutExtension,
  195. ImageType imageType,
  196. int imageIndex)
  197. {
  198. if (itemsWithImages.Count == 0)
  199. {
  200. return null;
  201. }
  202. string outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png");
  203. if (imageType == ImageType.Thumb)
  204. {
  205. return await CreateThumbCollage(item, itemsWithImages, outputPath).ConfigureAwait(false);
  206. }
  207. if (imageType == ImageType.Primary)
  208. {
  209. if (item is UserView)
  210. {
  211. return await CreateSquareCollage(item, itemsWithImages, outputPath).ConfigureAwait(false);
  212. }
  213. if (item is Playlist || item is MusicGenre)
  214. {
  215. return await CreateSquareCollage(item, itemsWithImages, outputPath).ConfigureAwait(false);
  216. }
  217. return await CreatePosterCollage(item, itemsWithImages, outputPath).ConfigureAwait(false);
  218. }
  219. throw new ArgumentException("Unexpected image type");
  220. }
  221. protected virtual int MaxImageAgeDays
  222. {
  223. get { return 7; }
  224. }
  225. public bool HasChanged(IHasMetadata item, IDirectoryService directoryServicee)
  226. {
  227. if (!Supports(item))
  228. {
  229. return false;
  230. }
  231. var supportedImages = GetEnabledImages(item).ToList();
  232. if (supportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary))
  233. {
  234. return true;
  235. }
  236. if (supportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb))
  237. {
  238. return true;
  239. }
  240. return false;
  241. }
  242. protected bool HasChanged(IHasImages item, ImageType type)
  243. {
  244. var image = item.GetImageInfo(type, 0);
  245. if (image != null)
  246. {
  247. if (!image.IsLocalFile)
  248. {
  249. return false;
  250. }
  251. if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
  252. {
  253. return false;
  254. }
  255. var age = DateTime.UtcNow - image.DateModified;
  256. if (age.TotalDays <= MaxImageAgeDays)
  257. {
  258. return false;
  259. }
  260. }
  261. return true;
  262. }
  263. protected List<BaseItem> GetFinalItems(List<BaseItem> items)
  264. {
  265. return GetFinalItems(items, 4);
  266. }
  267. protected virtual List<BaseItem> GetFinalItems(List<BaseItem> items, int limit)
  268. {
  269. // Rotate the images once every x days
  270. var random = DateTime.Now.DayOfYear % MaxImageAgeDays;
  271. return items
  272. .OrderBy(i => (random + string.Empty + items.IndexOf(i)).GetMD5())
  273. .Take(limit)
  274. .OrderBy(i => i.Name)
  275. .ToList();
  276. }
  277. public int Order
  278. {
  279. get
  280. {
  281. // Run before the default image provider which will download placeholders
  282. return 0;
  283. }
  284. }
  285. protected async Task<string> CreateSingleImage(List<BaseItem> itemsWithImages, string outputPathWithoutExtension, ImageType imageType)
  286. {
  287. var image = itemsWithImages
  288. .Where(i => i.HasImage(imageType) && i.GetImageInfo(imageType, 0).IsLocalFile && Path.HasExtension(i.GetImagePath(imageType)))
  289. .Select(i => i.GetImagePath(imageType))
  290. .FirstOrDefault();
  291. if (string.IsNullOrWhiteSpace(image))
  292. {
  293. return null;
  294. }
  295. var ext = Path.GetExtension(image);
  296. var outputPath = Path.ChangeExtension(outputPathWithoutExtension, ext);
  297. FileSystem.CopyFile(image, outputPath, true);
  298. return outputPath;
  299. }
  300. }
  301. }