2014-10-20 03:04:45 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
2014-08-12 02:59:51 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2014-10-20 03:04:45 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-08-12 02:59:51 +00:00
|
|
|
|
using MediaBrowser.Controller.Playlists;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MoreLinq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2014-10-20 03:04:45 +00:00
|
|
|
|
using System.Threading;
|
2014-08-12 02:59:51 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Playlists
|
|
|
|
|
{
|
2014-10-20 03:04:45 +00:00
|
|
|
|
public class PlaylistImageProvider : ICustomMetadataProvider<Playlist>, IHasChangeMonitor
|
2014-08-12 02:59:51 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-10-20 03:04:45 +00:00
|
|
|
|
private readonly IProviderManager _provider;
|
2014-08-12 02:59:51 +00:00
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
public PlaylistImageProvider(IFileSystem fileSystem, IProviderManager provider)
|
2014-08-12 02:59:51 +00:00
|
|
|
|
{
|
|
|
|
|
_fileSystem = fileSystem;
|
2014-10-20 03:04:45 +00:00
|
|
|
|
_provider = provider;
|
2014-08-12 02:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
public async Task<ItemUpdateType> FetchAsync(Playlist item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
2014-08-12 02:59:51 +00:00
|
|
|
|
{
|
2014-10-20 03:04:45 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ItemUpdateType> FetchAsync(PhotoAlbum item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
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;
|
2014-08-12 02:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
private Task<ItemUpdateType> FetchAsync(IHasImages item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
2014-08-12 02:59:51 +00:00
|
|
|
|
{
|
2014-10-20 03:04:45 +00:00
|
|
|
|
var items = GetItemsWithImages(item);
|
|
|
|
|
var cacheKey = GetConfigurationCacheKey(items);
|
|
|
|
|
|
|
|
|
|
if (!HasChanged(item, imageType, cacheKey))
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(ItemUpdateType.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FetchAsyncInternal(item, imageType, cacheKey, options, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<ItemUpdateType> FetchAsyncInternal(IHasImages item, ImageType imageType, string cacheKey, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var img = await CreateImageAsync(item, imageType, 0).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (img == null)
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
img.Save(ms, ImageFormat.Png);
|
|
|
|
|
|
|
|
|
|
ms.Position = 0;
|
|
|
|
|
|
|
|
|
|
await _provider.SaveImage(item, ms, "image/png", imageType, null, cacheKey, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const string Version = "3";
|
|
|
|
|
|
|
|
|
|
public string GetConfigurationCacheKey(List<BaseItem> items)
|
|
|
|
|
{
|
|
|
|
|
return (Version + "_" + string.Join(",", items.Select(i => i.Id.ToString("N")).ToArray())).GetMD5().ToString("N");
|
2014-08-12 02:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<BaseItem> GetItemsWithImages(IHasImages item)
|
|
|
|
|
{
|
2014-10-20 03:04:45 +00:00
|
|
|
|
var photoAlbum = item as PhotoAlbum;
|
|
|
|
|
if (photoAlbum != null)
|
|
|
|
|
{
|
|
|
|
|
return GetFinalItems(photoAlbum.RecursiveChildren.Where(i => i is Photo).ToList());
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-12 02:59:51 +00:00
|
|
|
|
var playlist = (Playlist)item;
|
|
|
|
|
|
|
|
|
|
var items = playlist.GetManageableItems()
|
|
|
|
|
.Select(i =>
|
|
|
|
|
{
|
|
|
|
|
var subItem = i.Item2;
|
|
|
|
|
|
|
|
|
|
var episode = subItem as Episode;
|
|
|
|
|
|
|
|
|
|
if (episode != null)
|
|
|
|
|
{
|
|
|
|
|
var series = episode.Series;
|
|
|
|
|
if (series != null && series.HasImage(ImageType.Primary))
|
|
|
|
|
{
|
|
|
|
|
return series;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (subItem.HasImage(ImageType.Primary))
|
|
|
|
|
{
|
|
|
|
|
return subItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parent = subItem.Parent;
|
|
|
|
|
|
|
|
|
|
if (parent != null && parent.HasImage(ImageType.Primary))
|
|
|
|
|
{
|
|
|
|
|
if (parent is MusicAlbum)
|
|
|
|
|
{
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
})
|
|
|
|
|
.Where(i => i != null)
|
|
|
|
|
.DistinctBy(i => i.Id)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
return GetFinalItems(items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<BaseItem> GetFinalItems(List<BaseItem> items)
|
|
|
|
|
{
|
2014-08-14 13:24:30 +00:00
|
|
|
|
// Rotate the images no more than once per day
|
|
|
|
|
var random = new Random(DateTime.Now.DayOfYear).Next();
|
2014-08-12 02:59:51 +00:00
|
|
|
|
|
2014-08-14 13:24:30 +00:00
|
|
|
|
return items
|
|
|
|
|
.OrderBy(i => random - items.IndexOf(i))
|
|
|
|
|
.Take(4)
|
|
|
|
|
.OrderBy(i => i.Name)
|
|
|
|
|
.ToList();
|
2014-08-12 02:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
public async Task<Image> CreateImageAsync(IHasImages item, ImageType imageType, int imageIndex)
|
2014-08-12 02:59:51 +00:00
|
|
|
|
{
|
|
|
|
|
var items = GetItemsWithImages(item);
|
|
|
|
|
|
|
|
|
|
if (items.Count == 0)
|
|
|
|
|
{
|
2014-10-20 03:04:45 +00:00
|
|
|
|
return null;
|
2014-08-12 02:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
return imageType == ImageType.Thumb ?
|
2014-08-27 03:25:39 +00:00
|
|
|
|
await GetThumbCollage(items).ConfigureAwait(false) :
|
|
|
|
|
await GetSquareCollage(items).ConfigureAwait(false);
|
2014-08-12 02:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 03:25:39 +00:00
|
|
|
|
private Task<Image> GetThumbCollage(List<BaseItem> items)
|
|
|
|
|
{
|
|
|
|
|
return GetThumbCollage(items.Select(i => i.GetImagePath(ImageType.Primary)).ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task<Image> GetSquareCollage(List<BaseItem> items)
|
|
|
|
|
{
|
|
|
|
|
return GetSquareCollage(items.Select(i => i.GetImagePath(ImageType.Primary)).ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Image> GetThumbCollage(List<string> files)
|
2014-08-12 02:59:51 +00:00
|
|
|
|
{
|
2014-08-27 03:25:39 +00:00
|
|
|
|
if (files.Count < 3)
|
|
|
|
|
{
|
|
|
|
|
return await GetSingleImage(files).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int rows = 1;
|
|
|
|
|
const int cols = 3;
|
|
|
|
|
|
|
|
|
|
const int cellWidth = 2 * (ThumbImageWidth / 3);
|
|
|
|
|
const int cellHeight = ThumbImageHeight;
|
|
|
|
|
var index = 0;
|
|
|
|
|
|
|
|
|
|
var img = new Bitmap(ThumbImageWidth, ThumbImageHeight, PixelFormat.Format32bppPArgb);
|
|
|
|
|
|
|
|
|
|
using (var graphics = Graphics.FromImage(img))
|
|
|
|
|
{
|
|
|
|
|
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
|
|
|
|
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
|
|
|
|
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
|
|
|
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
|
|
|
|
graphics.CompositingMode = CompositingMode.SourceCopy;
|
|
|
|
|
|
|
|
|
|
for (var row = 0; row < rows; row++)
|
|
|
|
|
{
|
|
|
|
|
for (var col = 0; col < cols; col++)
|
|
|
|
|
{
|
|
|
|
|
var x = col * (cellWidth / 2);
|
|
|
|
|
var y = row * cellHeight;
|
|
|
|
|
|
|
|
|
|
if (files.Count > index)
|
|
|
|
|
{
|
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
|
|
|
|
{
|
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
memoryStream.Position = 0;
|
|
|
|
|
|
|
|
|
|
using (var imgtemp = Image.FromStream(memoryStream, true, false))
|
|
|
|
|
{
|
|
|
|
|
graphics.DrawImage(imgtemp, x, y, cellWidth, cellHeight);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return img;
|
2014-08-12 02:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 03:04:45 +00:00
|
|
|
|
private const int SquareImageSize = 800;
|
|
|
|
|
private const int ThumbImageWidth = 1600;
|
|
|
|
|
private const int ThumbImageHeight = 900;
|
|
|
|
|
|
2014-08-27 03:25:39 +00:00
|
|
|
|
private async Task<Image> GetSquareCollage(List<string> files)
|
2014-08-12 02:59:51 +00:00
|
|
|
|
{
|
|
|
|
|
if (files.Count < 4)
|
|
|
|
|
{
|
|
|
|
|
return await GetSingleImage(files).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int rows = 2;
|
|
|
|
|
const int cols = 2;
|
|
|
|
|
|
2014-08-27 03:25:39 +00:00
|
|
|
|
const int singleSize = SquareImageSize / 2;
|
2014-08-12 02:59:51 +00:00
|
|
|
|
var index = 0;
|
|
|
|
|
|
2014-08-27 03:25:39 +00:00
|
|
|
|
var img = new Bitmap(SquareImageSize, SquareImageSize, PixelFormat.Format32bppPArgb);
|
2014-08-12 02:59:51 +00:00
|
|
|
|
|
|
|
|
|
using (var graphics = Graphics.FromImage(img))
|
|
|
|
|
{
|
|
|
|
|
graphics.CompositingQuality = CompositingQuality.HighQuality;
|
|
|
|
|
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
|
|
|
|
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
|
|
|
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
|
|
|
|
graphics.CompositingMode = CompositingMode.SourceCopy;
|
|
|
|
|
|
|
|
|
|
for (var row = 0; row < rows; row++)
|
|
|
|
|
{
|
|
|
|
|
for (var col = 0; col < cols; col++)
|
|
|
|
|
{
|
|
|
|
|
var x = col * singleSize;
|
|
|
|
|
var y = row * singleSize;
|
|
|
|
|
|
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
|
|
|
|
{
|
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
memoryStream.Position = 0;
|
|
|
|
|
|
|
|
|
|
using (var imgtemp = Image.FromStream(memoryStream, true, false))
|
|
|
|
|
{
|
|
|
|
|
graphics.DrawImage(imgtemp, x, y, singleSize, singleSize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return img;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task<Image> GetSingleImage(List<string> files)
|
|
|
|
|
{
|
|
|
|
|
return GetImage(files[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Image> GetImage(string file)
|
|
|
|
|
{
|
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
|
|
|
|
{
|
|
|
|
|
var memoryStream = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
memoryStream.Position = 0;
|
|
|
|
|
|
|
|
|
|
return Image.FromStream(memoryStream, true, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-20 03:04:45 +00:00
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Dynamic Image Provider"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
|
|
|
|
{
|
|
|
|
|
var items = GetItemsWithImages(item);
|
|
|
|
|
var cacheKey = GetConfigurationCacheKey(items);
|
|
|
|
|
|
|
|
|
|
return HasChanged(item, ImageType.Primary, cacheKey) || HasChanged(item, ImageType.Thumb, cacheKey);
|
|
|
|
|
}
|
2014-08-12 02:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|