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

310 lines
11 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
2014-10-29 22:01:02 +00:00
using MediaBrowser.Common.IO;
2015-04-08 14:38:02 +00:00
using MediaBrowser.Controller.Drawing;
2014-10-29 22:01:02 +00:00
using MediaBrowser.Controller.Entities;
2015-03-14 17:02:51 +00:00
using MediaBrowser.Controller.Library;
2015-04-02 17:44:44 +00:00
using MediaBrowser.Controller.Playlists;
2014-10-29 22:01:02 +00: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;
2015-10-04 04:23:11 +00:00
using CommonIO;
2014-10-29 22:01:02 +00:00
namespace MediaBrowser.Server.Implementations.Photos
{
2015-03-14 17:02:51 +00:00
public abstract class BaseDynamicImageProvider<T> : IHasChangeMonitor, IForcedProvider, ICustomMetadataProvider<T>, IHasOrder
2015-03-13 19:16:34 +00:00
where T : IHasMetadata
2014-10-29 22:01:02 +00:00
{
protected IFileSystem FileSystem { get; private set; }
protected IProviderManager ProviderManager { get; private set; }
protected IApplicationPaths ApplicationPaths { get; private set; }
2015-04-08 14:38:02 +00:00
protected IImageProcessor ImageProcessor { get; set; }
2014-10-29 22:01:02 +00:00
2015-04-08 14:38:02 +00:00
protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths applicationPaths, IImageProcessor imageProcessor)
2014-10-29 22:01:02 +00:00
{
ApplicationPaths = applicationPaths;
2014-10-29 22:01:02 +00:00
ProviderManager = providerManager;
FileSystem = fileSystem;
2015-04-08 14:38:02 +00:00
ImageProcessor = imageProcessor;
2014-10-29 22:01:02 +00:00
}
2015-10-14 05:02:30 +00:00
protected virtual bool Supports(IHasImages item)
2014-10-29 22:01:02 +00:00
{
2015-03-14 17:02:51 +00:00
return true;
2014-10-29 22:01:02 +00:00
}
2015-03-14 04:50:23 +00:00
public virtual IEnumerable<ImageType> GetSupportedImages(IHasImages item)
2014-10-29 22:01:02 +00:00
{
2015-03-13 19:37:19 +00:00
return new List<ImageType>
{
ImageType.Primary,
ImageType.Thumb
};
2014-10-29 22:01:02 +00:00
}
2015-03-14 17:02:51 +00:00
public async Task<ItemUpdateType> FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
if (!Supports(item))
{
return ItemUpdateType.None;
}
2015-07-27 05:03:34 +00:00
var updateType = ItemUpdateType.None;
var supportedImages = GetSupportedImages(item).ToList();
2015-03-14 17:02:51 +00:00
2015-07-27 05:03:34 +00:00
if (supportedImages.Contains(ImageType.Primary))
{
var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false);
updateType = updateType | primaryResult;
}
if (supportedImages.Contains(ImageType.Thumb))
{
var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false);
updateType = updateType | thumbResult;
}
return updateType;
2015-03-14 17:02:51 +00:00
}
protected async Task<ItemUpdateType> FetchAsync(IHasImages item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
var items = await GetItemsWithImages(item).ConfigureAwait(false);
2015-10-30 16:45:22 +00:00
return await FetchToFileInternal(item, items, imageType, cancellationToken).ConfigureAwait(false);
2015-03-14 17:02:51 +00:00
}
protected async Task<ItemUpdateType> FetchToFileInternal(IHasImages item,
List<BaseItem> itemsWithImages,
ImageType imageType,
CancellationToken cancellationToken)
{
2015-10-26 05:29:32 +00:00
var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N"));
FileSystem.CreateDirectory(Path.GetDirectoryName(outputPathWithoutExtension));
string outputPath = await CreateImage(item, itemsWithImages, outputPathWithoutExtension, imageType, 0).ConfigureAwait(false);
2015-03-14 17:02:51 +00:00
2015-10-26 05:29:32 +00:00
if (string.IsNullOrWhiteSpace(outputPath))
2015-03-14 17:02:51 +00:00
{
return ItemUpdateType.None;
}
2015-10-30 16:45:22 +00:00
await ProviderManager.SaveImage(item, outputPath, "image/png", imageType, null, Guid.NewGuid().ToString("N"), cancellationToken).ConfigureAwait(false);
2015-03-14 17:02:51 +00:00
return ItemUpdateType.ImageUpdate;
}
2015-03-13 19:37:19 +00:00
protected abstract Task<List<BaseItem>> GetItemsWithImages(IHasImages item);
2015-10-26 05:29:32 +00:00
protected Task<string> CreateThumbCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
2014-10-29 22:01:02 +00:00
{
2015-08-14 17:24:07 +00:00
return CreateCollage(primaryItem, items, outputPath, 640, 360);
2014-10-29 22:01:02 +00:00
}
2015-04-08 15:45:30 +00:00
protected virtual IEnumerable<string> GetStripCollageImagePaths(IHasImages primaryItem, IEnumerable<BaseItem> items)
2014-10-29 22:01:02 +00:00
{
2015-04-02 16:58:52 +00:00
return items
2015-10-16 17:06:31 +00:00
.Select(i =>
{
var image = i.GetImageInfo(ImageType.Primary, 0);
if (image != null && image.IsLocalFile)
{
return image.Path;
}
image = i.GetImageInfo(ImageType.Thumb, 0);
if (image != null && image.IsLocalFile)
{
return image.Path;
}
return null;
})
2015-04-02 16:58:52 +00:00
.Where(i => !string.IsNullOrWhiteSpace(i));
}
2015-10-26 05:29:32 +00:00
protected Task<string> CreatePosterCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
2015-04-02 17:44:44 +00:00
{
2015-08-14 17:24:07 +00:00
return CreateCollage(primaryItem, items, outputPath, 400, 600);
2015-04-08 14:38:02 +00:00
}
2015-10-26 05:29:32 +00:00
protected Task<string> CreateSquareCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath)
2015-04-08 14:38:02 +00:00
{
2015-08-14 17:24:07 +00:00
return CreateCollage(primaryItem, items, outputPath, 600, 600);
2015-04-08 14:38:02 +00:00
}
2015-04-02 17:44:44 +00:00
2015-10-26 05:29:32 +00:00
protected Task<string> CreateThumbCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, int width, int height)
2015-04-08 14:38:02 +00:00
{
2015-08-14 17:24:07 +00:00
return CreateCollage(primaryItem, items, outputPath, width, height);
2015-04-02 17:44:44 +00:00
}
2015-10-26 05:29:32 +00:00
private async Task<string> CreateCollage(IHasImages primaryItem, List<BaseItem> items, string outputPath, int width, int height)
2015-04-02 16:58:52 +00:00
{
2015-09-13 21:32:02 +00:00
FileSystem.CreateDirectory(Path.GetDirectoryName(outputPath));
2015-04-08 14:38:02 +00:00
var options = new ImageCollageOptions
{
Height = height,
Width = width,
OutputPath = outputPath,
InputPaths = GetStripCollageImagePaths(primaryItem, items).ToArray()
};
2015-05-11 16:32:15 +00:00
if (options.InputPaths.Length == 0)
{
2015-10-26 05:29:32 +00:00
return null;
}
if (!ImageProcessor.SupportsImageCollageCreation)
{
return null;
2015-05-11 16:32:15 +00:00
}
2015-10-26 05:29:32 +00:00
await ImageProcessor.CreateImageCollage(options).ConfigureAwait(false);
return outputPath;
2014-10-29 22:01:02 +00:00
}
public string Name
{
get { return "Dynamic Image Provider"; }
}
2015-10-26 05:29:32 +00:00
protected virtual async Task<string> CreateImage(IHasImages item,
2014-10-29 22:01:02 +00:00
List<BaseItem> itemsWithImages,
2015-10-26 05:29:32 +00:00
string outputPathWithoutExtension,
2015-03-13 19:37:19 +00:00
ImageType imageType,
2014-10-29 22:01:02 +00:00
int imageIndex)
{
if (itemsWithImages.Count == 0)
{
2015-10-26 05:29:32 +00:00
return null;
2014-10-29 22:01:02 +00:00
}
2015-10-26 05:29:32 +00:00
string outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png");
2015-04-02 17:44:44 +00:00
if (imageType == ImageType.Thumb)
{
2015-08-14 17:24:07 +00:00
return await CreateThumbCollage(item, itemsWithImages, outputPath).ConfigureAwait(false);
2015-04-02 17:44:44 +00:00
}
if (imageType == ImageType.Primary)
{
2015-04-16 14:59:39 +00:00
if (item is UserView)
{
2015-08-14 17:24:07 +00:00
return await CreateSquareCollage(item, itemsWithImages, outputPath).ConfigureAwait(false);
2015-04-16 14:59:39 +00:00
}
2015-10-26 05:29:32 +00:00
if (item is Playlist)
2015-04-08 15:45:30 +00:00
{
2015-08-14 17:24:07 +00:00
return await CreateSquareCollage(item, itemsWithImages, outputPath).ConfigureAwait(false);
2015-04-08 15:45:30 +00:00
}
2015-07-27 05:03:34 +00:00
return await CreatePosterCollage(item, itemsWithImages, outputPath).ConfigureAwait(false);
2015-04-02 17:44:44 +00:00
}
throw new ArgumentException("Unexpected image type");
2014-10-29 22:01:02 +00:00
}
2015-10-30 16:45:22 +00:00
protected virtual int MaxImageAgeDays
{
get { return 7; }
}
2015-05-05 15:25:00 +00:00
2014-10-29 22:01:02 +00:00
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
{
if (!Supports(item))
{
return false;
}
2015-07-27 05:03:34 +00:00
var supportedImages = GetSupportedImages(item).ToList();
2015-10-30 16:45:22 +00:00
if (supportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary))
2015-07-27 05:03:34 +00:00
{
return true;
}
2015-10-30 16:45:22 +00:00
if (supportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb))
2015-07-27 05:03:34 +00:00
{
return true;
}
return false;
2014-10-29 22:01:02 +00:00
}
2015-05-05 15:25:00 +00:00
protected bool HasChanged(IHasImages item, ImageType type)
{
var image = item.GetImageInfo(type, 0);
if (image != null)
{
2015-10-15 15:51:00 +00:00
if (!image.IsLocalFile)
{
return false;
}
2015-05-05 15:25:00 +00:00
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 22:01:02 +00:00
protected List<BaseItem> GetFinalItems(List<BaseItem> items)
2015-03-13 19:52:49 +00:00
{
return GetFinalItems(items, 4);
}
2015-03-14 17:02:51 +00:00
protected virtual List<BaseItem> GetFinalItems(List<BaseItem> items, int limit)
2014-10-29 22:01:02 +00:00
{
2015-03-26 23:10:34 +00:00
// Rotate the images once every x days
2015-05-05 15:25:00 +00:00
var random = DateTime.Now.DayOfYear % MaxImageAgeDays;
2014-10-29 22:01:02 +00:00
return items
2015-10-26 05:29:32 +00:00
.OrderBy(i => (random + string.Empty + items.IndexOf(i)).GetMD5())
2015-03-13 19:52:49 +00:00
.Take(limit)
2014-10-29 22:01:02 +00:00
.OrderBy(i => i.Name)
.ToList();
}
2015-03-13 19:37:19 +00:00
public int Order
{
get
{
// Run before the default image provider which will download placeholders
return 0;
}
}
2015-12-03 17:03:47 +00:00
protected async Task<string> CreateSingleImage(List<BaseItem> itemsWithImages, string outputPathWithoutExtension, ImageType imageType)
{
var image = itemsWithImages
.Where(i => i.HasImage(imageType) && i.GetImageInfo(imageType, 0).IsLocalFile && Path.HasExtension(i.GetImagePath(imageType)))
.Select(i => i.GetImagePath(imageType))
.FirstOrDefault();
if (string.IsNullOrWhiteSpace(image))
{
return null;
}
var ext = Path.GetExtension(image);
var outputPath = Path.ChangeExtension(outputPathWithoutExtension, ext);
File.Copy(image, outputPath);
return outputPath;
}
2014-10-29 22:01:02 +00:00
}
}