BaseDynamicImageProvider.cs 12 KB

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