BaseDynamicImageProvider.cs 12 KB

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