2015-02-19 19:12:33 -07:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2015-04-08 07:38:02 -07:00
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
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;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-25 23:48:54 -07:00
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-07-22 22:44:46 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
2016-02-22 12:32:35 -07:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2016-11-11 21:02:35 -07:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
|
2016-11-03 00:14:14 -07:00
|
|
|
|
namespace Emby.Server.Implementations.Images
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2016-04-27 18:59:09 -07:00
|
|
|
|
public abstract class BaseDynamicImageProvider<T> : IHasItemChangeMonitor, 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; }
|
2015-04-08 07:38:02 -07:00
|
|
|
|
protected IImageProcessor ImageProcessor { get; set; }
|
2014-10-29 15:01:02 -07:00
|
|
|
|
|
2015-04-08 07:38:02 -07:00
|
|
|
|
protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths applicationPaths, IImageProcessor imageProcessor)
|
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-04-08 07:38:02 -07:00
|
|
|
|
ImageProcessor = imageProcessor;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected virtual bool Supports(IHasMetadata 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
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 09:39:52 -07:00
|
|
|
|
public virtual ImageType[] GetSupportedImages(IHasMetadata item)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2017-10-21 09:39:52 -07:00
|
|
|
|
return new ImageType[]
|
2015-03-13 12:37:19 -07:00
|
|
|
|
{
|
2017-10-21 09:39:52 -07:00
|
|
|
|
ImageType.Primary
|
2015-03-13 12:37:19 -07:00
|
|
|
|
};
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-26 22:03:34 -07:00
|
|
|
|
var updateType = ItemUpdateType.None;
|
2017-10-21 09:39:52 -07:00
|
|
|
|
var supportedImages = GetSupportedImages(item);
|
2015-03-14 10:02:51 -07:00
|
|
|
|
|
2015-07-26 22:03:34 -07: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 10:02:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected async Task<ItemUpdateType> FetchAsync(IHasMetadata item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
2015-03-14 10:02:51 -07:00
|
|
|
|
{
|
2016-05-07 10:47:41 -07:00
|
|
|
|
var image = item.GetImageInfo(imageType, 0);
|
|
|
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
|
{
|
|
|
|
|
if (!image.IsLocalFile)
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-25 23:48:54 -07:00
|
|
|
|
var items = GetItemsWithImages(item);
|
2015-03-14 10:02:51 -07:00
|
|
|
|
|
2015-10-30 09:45:22 -07:00
|
|
|
|
return await FetchToFileInternal(item, items, imageType, cancellationToken).ConfigureAwait(false);
|
2015-03-14 10:02:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected async Task<ItemUpdateType> FetchToFileInternal(IHasMetadata item,
|
2015-03-14 10:02:51 -07:00
|
|
|
|
List<BaseItem> itemsWithImages,
|
|
|
|
|
ImageType imageType,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-10-25 22:29:32 -07:00
|
|
|
|
var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N"));
|
2017-05-04 11:14:45 -07:00
|
|
|
|
FileSystem.CreateDirectory(FileSystem.GetDirectoryName(outputPathWithoutExtension));
|
2017-05-19 10:09:37 -07:00
|
|
|
|
string outputPath = CreateImage(item, itemsWithImages, outputPathWithoutExtension, imageType, 0);
|
2015-03-14 10:02:51 -07:00
|
|
|
|
|
2015-10-25 22:29:32 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(outputPath))
|
2015-03-14 10:02:51 -07:00
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.None;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-11 21:02:35 -07:00
|
|
|
|
var mimeType = MimeTypes.GetMimeType(outputPath);
|
|
|
|
|
|
2016-12-02 13:45:11 -07:00
|
|
|
|
if (string.Equals(mimeType, "application/octet-stream", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
mimeType = "image/png";
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-11 21:02:35 -07:00
|
|
|
|
await ProviderManager.SaveImage(item, outputPath, mimeType, imageType, null, false, cancellationToken).ConfigureAwait(false);
|
2015-03-14 10:02:51 -07:00
|
|
|
|
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected abstract List<BaseItem> GetItemsWithImages(IHasMetadata item);
|
2015-03-13 12:37:19 -07:00
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected string CreateThumbCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2015-08-14 10:24:07 -07:00
|
|
|
|
return CreateCollage(primaryItem, items, outputPath, 640, 360);
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected virtual IEnumerable<string> GetStripCollageImagePaths(IHasMetadata primaryItem, IEnumerable<BaseItem> items)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
2015-04-02 09:58:52 -07:00
|
|
|
|
return items
|
2015-10-16 10:06:31 -07: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 09:58:52 -07:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i));
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected string CreatePosterCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath)
|
2015-04-02 10:44:44 -07:00
|
|
|
|
{
|
2015-08-14 10:24:07 -07:00
|
|
|
|
return CreateCollage(primaryItem, items, outputPath, 400, 600);
|
2015-04-08 07:38:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected string CreateSquareCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath)
|
2015-04-08 07:38:02 -07:00
|
|
|
|
{
|
2015-08-14 10:24:07 -07:00
|
|
|
|
return CreateCollage(primaryItem, items, outputPath, 600, 600);
|
2015-04-08 07:38:02 -07:00
|
|
|
|
}
|
2015-04-02 10:44:44 -07:00
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected string CreateThumbCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath, int width, int height)
|
2015-04-08 07:38:02 -07:00
|
|
|
|
{
|
2015-08-14 10:24:07 -07:00
|
|
|
|
return CreateCollage(primaryItem, items, outputPath, width, height);
|
2015-04-02 10:44:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
private string CreateCollage(IHasMetadata primaryItem, List<BaseItem> items, string outputPath, int width, int height)
|
2015-04-02 09:58:52 -07:00
|
|
|
|
{
|
2017-05-04 11:14:45 -07:00
|
|
|
|
FileSystem.CreateDirectory(FileSystem.GetDirectoryName(outputPath));
|
2015-04-08 07:38:02 -07:00
|
|
|
|
|
|
|
|
|
var options = new ImageCollageOptions
|
|
|
|
|
{
|
|
|
|
|
Height = height,
|
|
|
|
|
Width = width,
|
|
|
|
|
OutputPath = outputPath,
|
|
|
|
|
InputPaths = GetStripCollageImagePaths(primaryItem, items).ToArray()
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-11 09:32:15 -07:00
|
|
|
|
if (options.InputPaths.Length == 0)
|
|
|
|
|
{
|
2015-10-25 22:29:32 -07:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ImageProcessor.SupportsImageCollageCreation)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
2015-05-11 09:32:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 10:09:37 -07:00
|
|
|
|
ImageProcessor.CreateImageCollage(options);
|
2015-10-25 22:29:32 -07:00
|
|
|
|
return outputPath;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "Dynamic Image Provider"; }
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected virtual string CreateImage(IHasMetadata item,
|
2014-10-29 15:01:02 -07:00
|
|
|
|
List<BaseItem> itemsWithImages,
|
2015-10-25 22:29:32 -07:00
|
|
|
|
string outputPathWithoutExtension,
|
2015-03-13 12:37:19 -07:00
|
|
|
|
ImageType imageType,
|
2014-10-29 15:01:02 -07:00
|
|
|
|
int imageIndex)
|
|
|
|
|
{
|
|
|
|
|
if (itemsWithImages.Count == 0)
|
|
|
|
|
{
|
2015-10-25 22:29:32 -07:00
|
|
|
|
return null;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-25 22:29:32 -07:00
|
|
|
|
string outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png");
|
|
|
|
|
|
2015-04-02 10:44:44 -07:00
|
|
|
|
if (imageType == ImageType.Thumb)
|
|
|
|
|
{
|
2017-05-19 10:09:37 -07:00
|
|
|
|
return CreateThumbCollage(item, itemsWithImages, outputPath);
|
2015-04-02 10:44:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imageType == ImageType.Primary)
|
|
|
|
|
{
|
2015-04-16 07:59:39 -07:00
|
|
|
|
if (item is UserView)
|
|
|
|
|
{
|
2017-05-19 10:09:37 -07:00
|
|
|
|
return CreateSquareCollage(item, itemsWithImages, outputPath);
|
2015-04-16 07:59:39 -07:00
|
|
|
|
}
|
2017-05-09 11:51:26 -07:00
|
|
|
|
if (item is Playlist || item is MusicGenre || item is Genre || item is GameGenre || item is PhotoAlbum)
|
2015-04-08 08:45:30 -07:00
|
|
|
|
{
|
2017-05-19 10:09:37 -07:00
|
|
|
|
return CreateSquareCollage(item, itemsWithImages, outputPath);
|
2015-04-08 08:45:30 -07:00
|
|
|
|
}
|
2017-05-19 10:09:37 -07:00
|
|
|
|
return CreatePosterCollage(item, itemsWithImages, outputPath);
|
2015-04-02 10:44:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Unexpected image type");
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 09:45:22 -07:00
|
|
|
|
protected virtual int MaxImageAgeDays
|
|
|
|
|
{
|
|
|
|
|
get { return 7; }
|
|
|
|
|
}
|
2015-05-05 08:25:00 -07:00
|
|
|
|
|
2016-04-27 18:59:09 -07:00
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryServicee)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
|
|
|
|
if (!Supports(item))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 09:39:52 -07:00
|
|
|
|
var supportedImages = GetSupportedImages(item);
|
2015-07-26 22:03:34 -07:00
|
|
|
|
|
2015-10-30 09:45:22 -07:00
|
|
|
|
if (supportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary))
|
2015-07-26 22:03:34 -07:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-10-30 09:45:22 -07:00
|
|
|
|
if (supportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb))
|
2015-07-26 22:03:34 -07:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 14:06:13 -07:00
|
|
|
|
protected bool HasChanged(IHasMetadata item, ImageType type)
|
2015-05-05 08:25:00 -07:00
|
|
|
|
{
|
|
|
|
|
var image = item.GetImageInfo(type, 0);
|
|
|
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
|
{
|
2015-10-15 08:51:00 -07:00
|
|
|
|
if (!image.IsLocalFile)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-05 08:25:00 -07:00
|
|
|
|
if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 09:39:52 -07:00
|
|
|
|
if (!HasChangedByDate(item, image))
|
2015-05-05 08:25:00 -07:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-21 09:39:52 -07:00
|
|
|
|
protected virtual bool HasChangedByDate(IHasMetadata item, ItemImageInfo image)
|
|
|
|
|
{
|
|
|
|
|
var age = DateTime.UtcNow - image.DateModified;
|
|
|
|
|
if (age.TotalDays <= MaxImageAgeDays)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 12:56:38 -07:00
|
|
|
|
protected List<BaseItem> GetFinalItems(IEnumerable<BaseItem> items)
|
2015-03-13 12:52:49 -07:00
|
|
|
|
{
|
|
|
|
|
return GetFinalItems(items, 4);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 12:56:38 -07:00
|
|
|
|
protected virtual List<BaseItem> GetFinalItems(IEnumerable<BaseItem> items, int limit)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
{
|
|
|
|
|
return items
|
2017-08-09 12:56:38 -07:00
|
|
|
|
.OrderBy(i => Guid.NewGuid())
|
2015-03-13 12:52:49 -07:00
|
|
|
|
.Take(limit)
|
2014-10-29 15:01:02 -07:00
|
|
|
|
.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-14 09:58:01 -07:00
|
|
|
|
|
2017-05-19 10:09:37 -07:00
|
|
|
|
protected string CreateSingleImage(List<BaseItem> itemsWithImages, string outputPathWithoutExtension, ImageType imageType)
|
2015-11-14 09:58:01 -07:00
|
|
|
|
{
|
|
|
|
|
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);
|
2016-11-03 00:14:14 -07:00
|
|
|
|
FileSystem.CopyFile(image, outputPath, true);
|
2015-11-14 09:58:01 -07:00
|
|
|
|
|
|
|
|
|
return outputPath;
|
|
|
|
|
}
|
2014-10-29 15:01:02 -07:00
|
|
|
|
}
|
|
|
|
|
}
|