2015-02-19 19:12:33 -07:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2015-03-14 10:02:51 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2015-04-02 10:44:44 -07:00
|
|
|
|
using MediaBrowser.Controller.Playlists;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2015-03-13 12:37:19 -07:00
|
|
|
|
using MediaBrowser.Model.Drawing;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2015-04-02 10:44:44 -07:00
|
|
|
|
using MediaBrowser.Server.Implementations.UserViews;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
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 10:02:51 -07:00
|
|
|
|
public abstract class BaseDynamicImageProvider<T> : IHasChangeMonitor, IForcedProvider, ICustomMetadataProvider<T>, IHasOrder
|
2015-03-13 12:16:34 -07:00
|
|
|
|
where T : IHasMetadata
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
|
|
|
|
protected IFileSystem FileSystem { get; private set; }
|
|
|
|
|
protected IProviderManager ProviderManager { get; private set; }
|
2015-02-19 19:12:33 -07:00
|
|
|
|
protected IApplicationPaths ApplicationPaths { get; private set; }
|
2014-10-29 15:01:02 -07:00
|
|
|
|
|
2015-02-19 19:12:33 -07:00
|
|
|
|
protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths applicationPaths)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2015-02-19 19:12:33 -07:00
|
|
|
|
ApplicationPaths = applicationPaths;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
ProviderManager = providerManager;
|
|
|
|
|
FileSystem = fileSystem;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 12:37:19 -07:00
|
|
|
|
public virtual bool Supports(IHasImages item)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2015-03-14 10:02:51 -07:00
|
|
|
|
return true;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 21:50:23 -07:00
|
|
|
|
public virtual IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2015-03-13 12:37:19 -07:00
|
|
|
|
return new List<ImageType>
|
|
|
|
|
{
|
|
|
|
|
ImageType.Primary,
|
|
|
|
|
ImageType.Thumb
|
|
|
|
|
};
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-14 10:02:51 -07: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 18:48:25 -07:00
|
|
|
|
var cacheKey = GetConfigurationCacheKey(items, item.Name);
|
2015-03-14 10:02:51 -07: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)
|
|
|
|
|
{
|
|
|
|
|
var stream = await CreateImageAsync(item, itemsWithImages, imageType, 0).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (stream == null)
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stream is MemoryStream)
|
|
|
|
|
{
|
|
|
|
|
using (stream)
|
|
|
|
|
{
|
|
|
|
|
stream.Position = 0;
|
|
|
|
|
|
|
|
|
|
await ProviderManager.SaveImage(item, stream, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
await stream.CopyToAsync(ms).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
ms.Position = 0;
|
|
|
|
|
|
|
|
|
|
await ProviderManager.SaveImage(item, ms, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 12:37:19 -07:00
|
|
|
|
public async Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
|
|
|
|
var items = await GetItemsWithImages(item).ConfigureAwait(false);
|
2015-03-15 18:48:25 -07:00
|
|
|
|
var cacheKey = GetConfigurationCacheKey(items, item.Name);
|
2014-10-29 15:01:02 -07:00
|
|
|
|
|
2015-03-14 10:02:51 -07:00
|
|
|
|
var result = await CreateImageAsync(item, items, type, 0).ConfigureAwait(false);
|
2015-03-13 12:37:19 -07:00
|
|
|
|
|
|
|
|
|
return new DynamicImageResponse
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2015-03-13 12:37:19 -07:00
|
|
|
|
HasImage = result != null,
|
|
|
|
|
Stream = result,
|
|
|
|
|
InternalCacheKey = cacheKey,
|
|
|
|
|
Format = ImageFormat.Png
|
|
|
|
|
};
|
|
|
|
|
}
|
2014-10-29 15:01:02 -07:00
|
|
|
|
|
2015-03-13 12:37:19 -07:00
|
|
|
|
protected abstract Task<List<BaseItem>> GetItemsWithImages(IHasImages item);
|
|
|
|
|
|
2015-04-06 13:55:37 -07:00
|
|
|
|
private const string Version = "19";
|
2015-03-15 18:48:25 -07:00
|
|
|
|
protected string GetConfigurationCacheKey(List<BaseItem> items, string itemName)
|
2015-03-13 12:37:19 -07:00
|
|
|
|
{
|
2015-03-23 19:26:19 -07: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 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 09:58:52 -07:00
|
|
|
|
protected Task<Stream> GetThumbCollage(IHasImages primaryItem, List<BaseItem> items)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2015-04-02 10:44:44 -07:00
|
|
|
|
var stream = new StripCollageBuilder(ApplicationPaths).BuildThumbCollage(GetStripCollageImagePaths(items), 960, 540, true, primaryItem.Name);
|
2015-04-02 09:58:52 -07:00
|
|
|
|
|
|
|
|
|
return Task.FromResult(stream);
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 09:58:52 -07:00
|
|
|
|
private IEnumerable<String> GetStripCollageImagePaths(IEnumerable<BaseItem> items)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2015-04-02 09:58:52 -07:00
|
|
|
|
return items
|
2015-03-03 00:03:17 -07:00
|
|
|
|
.Select(i => i.GetImagePath(ImageType.Primary) ?? i.GetImagePath(ImageType.Thumb))
|
2015-04-02 09:58:52 -07:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i));
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 10:44:44 -07:00
|
|
|
|
protected Task<Stream> GetPosterCollage(IHasImages primaryItem, List<BaseItem> items)
|
|
|
|
|
{
|
2015-04-02 14:01:42 -07:00
|
|
|
|
var stream = new StripCollageBuilder(ApplicationPaths).BuildPosterCollage(GetStripCollageImagePaths(items), 600, 900, true, primaryItem.Name);
|
2015-04-02 10:44:44 -07:00
|
|
|
|
|
|
|
|
|
return Task.FromResult(stream);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 09:58:52 -07:00
|
|
|
|
protected Task<Stream> GetSquareCollage(IHasImages primaryItem, List<BaseItem> items)
|
|
|
|
|
{
|
2015-04-02 10:44:44 -07:00
|
|
|
|
var stream = new StripCollageBuilder(ApplicationPaths).BuildSquareCollage(GetStripCollageImagePaths(items), 800, 800, true, primaryItem.Name);
|
2015-03-03 00:03:17 -07:00
|
|
|
|
|
2015-04-02 09:58:52 -07:00
|
|
|
|
return Task.FromResult(stream);
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Dynamic Image Provider"; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 12:52:49 -07:00
|
|
|
|
protected virtual async Task<Stream> CreateImageAsync(IHasImages item,
|
2014-10-29 15:01:02 -07:00
|
|
|
|
List<BaseItem> itemsWithImages,
|
2015-03-13 12:37:19 -07:00
|
|
|
|
ImageType imageType,
|
2014-10-29 15:01:02 -07:00
|
|
|
|
int imageIndex)
|
|
|
|
|
{
|
|
|
|
|
if (itemsWithImages.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 10:44:44 -07:00
|
|
|
|
if (imageType == ImageType.Thumb)
|
|
|
|
|
{
|
|
|
|
|
return await GetThumbCollage(item, itemsWithImages).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imageType == ImageType.Primary)
|
|
|
|
|
{
|
|
|
|
|
return item is PhotoAlbum || item is Playlist ?
|
|
|
|
|
await GetSquareCollage(item, itemsWithImages).ConfigureAwait(false) :
|
|
|
|
|
await GetPosterCollage(item, itemsWithImages).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Unexpected image type");
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
|
|
|
|
{
|
|
|
|
|
if (!Supports(item))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var items = GetItemsWithImages(item).Result;
|
2015-03-15 18:48:25 -07:00
|
|
|
|
var cacheKey = GetConfigurationCacheKey(items, item.Name);
|
2014-10-29 15:01:02 -07: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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected List<BaseItem> GetFinalItems(List<BaseItem> items)
|
2015-03-13 12:52:49 -07:00
|
|
|
|
{
|
|
|
|
|
// Rotate the images no more than once per week
|
|
|
|
|
return GetFinalItems(items, 4);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-14 10:02:51 -07:00
|
|
|
|
protected virtual List<BaseItem> GetFinalItems(List<BaseItem> items, int limit)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2015-03-26 16:10:34 -07:00
|
|
|
|
// Rotate the images once every x days
|
|
|
|
|
var random = DateTime.Now.DayOfYear % 4;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
|
|
|
|
|
return items
|
2015-03-23 19:26:19 -07:00
|
|
|
|
.OrderBy(i => (random + "" + items.IndexOf(i)).GetMD5())
|
2015-03-13 12:52:49 -07:00
|
|
|
|
.Take(limit)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
.OrderBy(i => i.Name)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-13 12:37:19 -07:00
|
|
|
|
public int Order
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// Run before the default image provider which will download placeholders
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
}
|