jellyfin/MediaBrowser.Server.Implementations/Photos/BaseDynamicImageProvider.cs

286 lines
10 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
2014-10-29 18:01:02 -04:00
using MediaBrowser.Common.IO;
2015-04-08 10:38:02 -04:00
using MediaBrowser.Controller.Drawing;
2014-10-29 18:01:02 -04:00
using MediaBrowser.Controller.Entities;
2015-03-14 13:02:51 -04:00
using MediaBrowser.Controller.Library;
2015-04-02 13:44:44 -04:00
using MediaBrowser.Controller.Playlists;
2014-10-29 18:01:02 -04:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Photos
{
2015-03-14 13:02:51 -04:00
public abstract class BaseDynamicImageProvider<T> : IHasChangeMonitor, IForcedProvider, ICustomMetadataProvider<T>, IHasOrder
2015-03-13 15:16:34 -04:00
where T : IHasMetadata
2014-10-29 18:01:02 -04:00
{
protected IFileSystem FileSystem { get; private set; }
protected IProviderManager ProviderManager { get; private set; }
protected IApplicationPaths ApplicationPaths { get; private set; }
2015-04-08 10:38:02 -04:00
protected IImageProcessor ImageProcessor { get; set; }
2014-10-29 18:01:02 -04:00
2015-04-08 10:38:02 -04:00
protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths applicationPaths, IImageProcessor imageProcessor)
2014-10-29 18:01:02 -04:00
{
ApplicationPaths = applicationPaths;
2014-10-29 18:01:02 -04:00
ProviderManager = providerManager;
FileSystem = fileSystem;
2015-04-08 10:38:02 -04:00
ImageProcessor = imageProcessor;
2014-10-29 18:01:02 -04:00
}
2015-03-13 15:37:19 -04:00
public virtual bool Supports(IHasImages item)
2014-10-29 18:01:02 -04:00
{
2015-03-14 13:02:51 -04:00
return true;
2014-10-29 18:01:02 -04:00
}
2015-03-14 00:50:23 -04:00
public virtual IEnumerable<ImageType> GetSupportedImages(IHasImages item)
2014-10-29 18:01:02 -04:00
{
2015-03-13 15:37:19 -04:00
return new List<ImageType>
{
ImageType.Primary,
ImageType.Thumb
};
2014-10-29 18:01:02 -04:00
}
2015-03-14 13:02:51 -04:00
public async Task<ItemUpdateType> FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
if (!Supports(item))
{
return ItemUpdateType.None;
}
var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false);
var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false);
return primaryResult | thumbResult;
}
protected async Task<ItemUpdateType> FetchAsync(IHasImages item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
var items = await GetItemsWithImages(item).ConfigureAwait(false);
2015-03-15 21:48:25 -04:00
var cacheKey = GetConfigurationCacheKey(items, item.Name);
2015-03-14 13:02:51 -04:00
if (!HasChanged(item, imageType, cacheKey))
{
return ItemUpdateType.None;
}
return await FetchToFileInternal(item, items, imageType, cacheKey, cancellationToken).ConfigureAwait(false);
}
protected async Task<ItemUpdateType> FetchToFileInternal(IHasImages item,
List<BaseItem> itemsWithImages,
ImageType imageType,
string cacheKey,
CancellationToken cancellationToken)
{
2015-04-08 11:45:30 -04:00
var outputPath = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid() + ".png");
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
var imageCreated = await CreateImage(item, itemsWithImages, outputPath, imageType, 0).ConfigureAwait(false);
2015-03-14 13:02:51 -04:00
2015-04-08 11:45:30 -04:00
if (!imageCreated)
2015-03-14 13:02:51 -04:00
{
return ItemUpdateType.None;
}
2015-04-08 11:45:30 -04:00
await ProviderManager.SaveImage(item, outputPath, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false);
2015-03-14 13:02:51 -04:00
return ItemUpdateType.ImageUpdate;
}
2015-03-13 15:37:19 -04:00
protected abstract Task<List<BaseItem>> GetItemsWithImages(IHasImages item);
2015-04-17 00:53:47 -04:00
private const string Version = "32";
2015-03-15 21:48:25 -04:00
protected string GetConfigurationCacheKey(List<BaseItem> items, string itemName)
2015-03-13 15:37:19 -04:00
{
2015-03-23 22:26:19 -04:00
var parts = Version + "_" + (itemName ?? string.Empty) + "_" +
string.Join(",", items.Select(i => i.Id.ToString("N")).ToArray());
return parts.GetMD5().ToString("N");
2014-10-29 18:01:02 -04:00
}
2015-05-11 12:32:15 -04:00
protected Task<bool> CreateThumbCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, bool drawText)
2014-10-29 18:01:02 -04:00
{
return CreateCollage(primaryItem, items, outputPath, 960, 540, drawText, primaryItem.Name);
2014-10-29 18:01:02 -04:00
}
2015-04-08 11:45:30 -04:00
protected virtual IEnumerable<string> GetStripCollageImagePaths(IHasImages primaryItem, IEnumerable<BaseItem> items)
2014-10-29 18:01:02 -04:00
{
2015-04-02 12:58:52 -04:00
return items
2015-03-03 02:03:17 -05:00
.Select(i => i.GetImagePath(ImageType.Primary) ?? i.GetImagePath(ImageType.Thumb))
2015-04-02 12:58:52 -04:00
.Where(i => !string.IsNullOrWhiteSpace(i));
}
2015-05-11 12:32:15 -04:00
protected Task<bool> CreatePosterCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
2015-04-02 13:44:44 -04:00
{
return CreateCollage(primaryItem, items, outputPath, 600, 900, true, primaryItem.Name);
2015-04-08 10:38:02 -04:00
}
2015-05-11 12:32:15 -04:00
protected Task<bool> CreateSquareCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, bool drawText)
2015-04-08 10:38:02 -04:00
{
return CreateCollage(primaryItem, items, outputPath, 800, 800, drawText, primaryItem.Name);
2015-04-08 10:38:02 -04:00
}
2015-04-02 13:44:44 -04:00
2015-05-11 12:32:15 -04:00
protected Task<bool> CreateThumbCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, int width, int height, bool drawText, string text)
2015-04-08 10:38:02 -04:00
{
return CreateCollage(primaryItem, items, outputPath, width, height, drawText, text);
2015-04-02 13:44:44 -04:00
}
2015-05-11 12:32:15 -04:00
private Task<bool> CreateCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, int width, int height, bool drawText, string text)
2015-04-02 12:58:52 -04:00
{
2015-04-08 10:38:02 -04:00
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
var options = new ImageCollageOptions
{
Height = height,
Width = width,
OutputPath = outputPath,
Text = drawText ? text : null,
InputPaths = GetStripCollageImagePaths(primaryItem, items).ToArray()
};
2015-05-11 12:32:15 -04:00
if (options.InputPaths.Length == 0)
{
return Task.FromResult(false);
}
ImageProcessor.CreateImageCollage(options);
return Task.FromResult(true);
2014-10-29 18:01:02 -04:00
}
public string Name
{
get { return "Dynamic Image Provider"; }
}
protected virtual async Task<bool> CreateImage(IHasImages item,
2014-10-29 18:01:02 -04:00
List<BaseItem> itemsWithImages,
2015-04-08 11:45:30 -04:00
string outputPath,
2015-03-13 15:37:19 -04:00
ImageType imageType,
2014-10-29 18:01:02 -04:00
int imageIndex)
{
if (itemsWithImages.Count == 0)
{
2015-04-08 11:45:30 -04:00
return false;
2014-10-29 18:01:02 -04:00
}
2015-04-16 10:59:39 -04:00
var drawText = !(item is UserView);
2015-04-02 13:44:44 -04:00
if (imageType == ImageType.Thumb)
{
2015-05-11 12:32:15 -04:00
return await CreateThumbCollage(item, itemsWithImages, outputPath, drawText).ConfigureAwait(false);
2015-04-02 13:44:44 -04:00
}
if (imageType == ImageType.Primary)
{
2015-04-16 10:59:39 -04:00
if (item is UserView)
{
2015-05-11 12:32:15 -04:00
return await CreateSquareCollage(item, itemsWithImages, outputPath, drawText).ConfigureAwait(false);
2015-04-16 10:59:39 -04:00
}
else if (item is PhotoAlbum || item is Playlist)
2015-04-08 11:45:30 -04:00
{
2015-05-11 12:32:15 -04:00
return await CreateSquareCollage(item, itemsWithImages, outputPath, drawText).ConfigureAwait(false);
2015-04-08 11:45:30 -04:00
}
else
{
2015-05-11 12:32:15 -04:00
return await CreatePosterCollage(item, itemsWithImages, outputPath).ConfigureAwait(false);
2015-04-08 11:45:30 -04:00
}
2015-04-02 13:44:44 -04:00
}
throw new ArgumentException("Unexpected image type");
2014-10-29 18:01:02 -04:00
}
2015-05-05 11:25:00 -04:00
private const int MaxImageAgeDays = 7;
2014-10-29 18:01:02 -04:00
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
{
if (!Supports(item))
{
return false;
}
2015-05-05 11:25:00 -04:00
if (item is UserView)
{
2015-07-10 22:23:28 -04:00
return HasChanged(item, ImageType.Primary);
2015-05-05 11:25:00 -04:00
}
2014-10-29 18:01:02 -04:00
var items = GetItemsWithImages(item).Result;
2015-03-15 21:48:25 -04:00
var cacheKey = GetConfigurationCacheKey(items, item.Name);
2014-10-29 18:01:02 -04:00
return HasChanged(item, ImageType.Primary, cacheKey) || HasChanged(item, ImageType.Thumb, cacheKey);
}
protected bool HasChanged(IHasImages item, ImageType type, string cacheKey)
{
var image = item.GetImageInfo(type, 0);
if (image != null)
{
if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
{
return false;
}
var currentPathCacheKey = (Path.GetFileNameWithoutExtension(image.Path) ?? string.Empty).Split('_').LastOrDefault();
if (string.Equals(cacheKey, currentPathCacheKey, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
return true;
}
2015-05-05 11:25:00 -04:00
protected bool HasChanged(IHasImages item, ImageType type)
{
var image = item.GetImageInfo(type, 0);
if (image != null)
{
if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
{
return false;
}
var age = DateTime.UtcNow - image.DateModified;
if (age.TotalDays <= MaxImageAgeDays)
{
return false;
}
}
return true;
}
2014-10-29 18:01:02 -04:00
protected List<BaseItem> GetFinalItems(List<BaseItem> items)
2015-03-13 15:52:49 -04:00
{
return GetFinalItems(items, 4);
}
2015-03-14 13:02:51 -04:00
protected virtual List<BaseItem> GetFinalItems(List<BaseItem> items, int limit)
2014-10-29 18:01:02 -04:00
{
2015-03-26 19:10:34 -04:00
// Rotate the images once every x days
2015-05-05 11:25:00 -04:00
var random = DateTime.Now.DayOfYear % MaxImageAgeDays;
2014-10-29 18:01:02 -04:00
return items
2015-03-23 22:26:19 -04:00
.OrderBy(i => (random + "" + items.IndexOf(i)).GetMD5())
2015-03-13 15:52:49 -04:00
.Take(limit)
2014-10-29 18:01:02 -04:00
.OrderBy(i => i.Name)
.ToList();
}
2015-03-13 15:37:19 -04:00
public int Order
{
get
{
// Run before the default image provider which will download placeholders
return 0;
}
}
2014-10-29 18:01:02 -04:00
}
}