2021-05-20 21:28:18 +02:00
|
|
|
#nullable disable
|
|
|
|
|
2019-11-01 18:38:54 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2019-01-13 20:21:32 +01:00
|
|
|
using System.Collections.Generic;
|
2019-02-28 23:22:57 +01:00
|
|
|
using System.Globalization;
|
2019-01-13 20:21:32 +01:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2015-04-08 16:38:02 +02:00
|
|
|
using MediaBrowser.Controller.Drawing;
|
2014-10-29 23:01:02 +01:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2019-01-13 20:21:32 +01:00
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2015-03-14 18:02:51 +01:00
|
|
|
using MediaBrowser.Controller.Library;
|
2015-04-02 19:44:44 +02:00
|
|
|
using MediaBrowser.Controller.Playlists;
|
2014-10-29 23:01:02 +01:00
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2016-10-25 21:02:04 +02:00
|
|
|
using MediaBrowser.Model.IO;
|
2016-11-12 05:02:35 +01:00
|
|
|
using MediaBrowser.Model.Net;
|
2014-10-29 23:01:02 +01:00
|
|
|
|
2016-11-03 08:14:14 +01:00
|
|
|
namespace Emby.Server.Implementations.Images
|
2014-10-29 23:01:02 +01:00
|
|
|
{
|
2016-04-28 03:59:09 +02:00
|
|
|
public abstract class BaseDynamicImageProvider<T> : IHasItemChangeMonitor, IForcedProvider, ICustomMetadataProvider<T>, IHasOrder
|
2018-09-12 19:26:21 +02:00
|
|
|
where T : BaseItem
|
2014-10-29 23:01:02 +01:00
|
|
|
{
|
2015-04-08 16:38:02 +02:00
|
|
|
protected BaseDynamicImageProvider(IFileSystem fileSystem, IProviderManager providerManager, IApplicationPaths applicationPaths, IImageProcessor imageProcessor)
|
2014-10-29 23:01:02 +01:00
|
|
|
{
|
2015-02-20 03:12:33 +01:00
|
|
|
ApplicationPaths = applicationPaths;
|
2014-10-29 23:01:02 +01:00
|
|
|
ProviderManager = providerManager;
|
|
|
|
FileSystem = fileSystem;
|
2015-04-08 16:38:02 +02:00
|
|
|
ImageProcessor = imageProcessor;
|
2014-10-29 23:01:02 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 17:19:48 +02:00
|
|
|
protected IFileSystem FileSystem { get; }
|
|
|
|
|
|
|
|
protected IProviderManager ProviderManager { get; }
|
|
|
|
|
|
|
|
protected IApplicationPaths ApplicationPaths { get; }
|
|
|
|
|
|
|
|
protected IImageProcessor ImageProcessor { get; set; }
|
|
|
|
|
|
|
|
protected virtual IReadOnlyCollection<ImageType> SupportedImages { get; }
|
|
|
|
= new ImageType[] { ImageType.Primary };
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public string Name => "Dynamic Image Provider";
|
|
|
|
|
|
|
|
protected virtual int MaxImageAgeDays => 7;
|
|
|
|
|
|
|
|
public int Order => 0;
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
protected virtual bool Supports(BaseItem _) => true;
|
2014-10-29 23:01:02 +01:00
|
|
|
|
2015-03-14 18:02:51 +01:00
|
|
|
public async Task<ItemUpdateType> FetchAsync(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (!Supports(item))
|
|
|
|
{
|
|
|
|
return ItemUpdateType.None;
|
|
|
|
}
|
|
|
|
|
2015-07-27 07:03:34 +02:00
|
|
|
var updateType = ItemUpdateType.None;
|
2015-03-14 18:02:51 +01:00
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
if (SupportedImages.Contains(ImageType.Primary))
|
2015-07-27 07:03:34 +02:00
|
|
|
{
|
|
|
|
var primaryResult = await FetchAsync(item, ImageType.Primary, options, cancellationToken).ConfigureAwait(false);
|
|
|
|
updateType = updateType | primaryResult;
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
if (SupportedImages.Contains(ImageType.Thumb))
|
2015-07-27 07:03:34 +02:00
|
|
|
{
|
|
|
|
var thumbResult = await FetchAsync(item, ImageType.Thumb, options, cancellationToken).ConfigureAwait(false);
|
|
|
|
updateType = updateType | thumbResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateType;
|
2015-03-14 18:02:51 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
protected Task<ItemUpdateType> FetchAsync(BaseItem item, ImageType imageType, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
2015-03-14 18:02:51 +01:00
|
|
|
{
|
2016-05-07 19:47:41 +02:00
|
|
|
var image = item.GetImageInfo(imageType, 0);
|
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
{
|
|
|
|
if (!image.IsLocalFile)
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
return Task.FromResult(ItemUpdateType.None);
|
2016-05-07 19:47:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
|
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
return Task.FromResult(ItemUpdateType.None);
|
2016-05-07 19:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 08:48:54 +02:00
|
|
|
var items = GetItemsWithImages(item);
|
2015-03-14 18:02:51 +01:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
return FetchToFileInternal(item, items, imageType, cancellationToken);
|
2015-03-14 18:02:51 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 17:19:48 +02:00
|
|
|
protected async Task<ItemUpdateType> FetchToFileInternal(
|
|
|
|
BaseItem item,
|
2019-03-07 15:54:30 +01:00
|
|
|
IReadOnlyList<BaseItem> itemsWithImages,
|
2015-03-14 18:02:51 +01:00
|
|
|
ImageType imageType,
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2019-02-28 23:22:57 +01:00
|
|
|
var outputPathWithoutExtension = Path.Combine(ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
|
2019-01-26 22:08:04 +01:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outputPathWithoutExtension));
|
2017-05-19 19:09:37 +02:00
|
|
|
string outputPath = CreateImage(item, itemsWithImages, outputPathWithoutExtension, imageType, 0);
|
2015-03-14 18:02:51 +01:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (string.IsNullOrEmpty(outputPath))
|
2015-03-14 18:02:51 +01:00
|
|
|
{
|
|
|
|
return ItemUpdateType.None;
|
|
|
|
}
|
|
|
|
|
2016-11-12 05:02:35 +01:00
|
|
|
var mimeType = MimeTypes.GetMimeType(outputPath);
|
|
|
|
|
2016-12-02 21:45:11 +01:00
|
|
|
if (string.Equals(mimeType, "application/octet-stream", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
mimeType = "image/png";
|
|
|
|
}
|
|
|
|
|
2016-11-12 05:02:35 +01:00
|
|
|
await ProviderManager.SaveImage(item, outputPath, mimeType, imageType, null, false, cancellationToken).ConfigureAwait(false);
|
2015-03-14 18:02:51 +01:00
|
|
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
protected abstract IReadOnlyList<BaseItem> GetItemsWithImages(BaseItem item);
|
2015-03-13 20:37:19 +01:00
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
protected string CreateThumbCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath)
|
2014-10-29 23:01:02 +01:00
|
|
|
{
|
2015-08-14 19:24:07 +02:00
|
|
|
return CreateCollage(primaryItem, items, outputPath, 640, 360);
|
2014-10-29 23:01:02 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
protected virtual IEnumerable<string> GetStripCollageImagePaths(BaseItem primaryItem, IEnumerable<BaseItem> items)
|
2014-10-29 23:01:02 +01:00
|
|
|
{
|
2020-08-21 19:53:55 +02:00
|
|
|
var useBackdrop = primaryItem is CollectionFolder;
|
2015-04-02 18:58:52 +02:00
|
|
|
return items
|
2015-10-16 19:06:31 +02:00
|
|
|
.Select(i =>
|
|
|
|
{
|
2020-08-21 19:53:55 +02:00
|
|
|
// Use Backdrop instead of Primary image for Library images.
|
|
|
|
if (useBackdrop)
|
|
|
|
{
|
|
|
|
var backdrop = i.GetImageInfo(ImageType.Backdrop, 0);
|
|
|
|
if (backdrop != null && backdrop.IsLocalFile)
|
|
|
|
{
|
|
|
|
return backdrop.Path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 19:06:31 +02:00
|
|
|
var image = i.GetImageInfo(ImageType.Primary, 0);
|
|
|
|
if (image != null && image.IsLocalFile)
|
|
|
|
{
|
|
|
|
return image.Path;
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
image = i.GetImageInfo(ImageType.Thumb, 0);
|
2015-10-16 19:06:31 +02:00
|
|
|
if (image != null && image.IsLocalFile)
|
|
|
|
{
|
|
|
|
return image.Path;
|
|
|
|
}
|
2019-03-07 15:54:30 +01:00
|
|
|
|
2015-10-16 19:06:31 +02:00
|
|
|
return null;
|
|
|
|
})
|
2018-09-12 19:26:21 +02:00
|
|
|
.Where(i => !string.IsNullOrEmpty(i));
|
2015-04-02 18:58:52 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
protected string CreatePosterCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath)
|
2015-04-02 19:44:44 +02:00
|
|
|
{
|
2015-08-14 19:24:07 +02:00
|
|
|
return CreateCollage(primaryItem, items, outputPath, 400, 600);
|
2015-04-08 16:38:02 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
protected string CreateSquareCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath)
|
2015-04-08 16:38:02 +02:00
|
|
|
{
|
2015-08-14 19:24:07 +02:00
|
|
|
return CreateCollage(primaryItem, items, outputPath, 600, 600);
|
2015-04-08 16:38:02 +02:00
|
|
|
}
|
2015-04-02 19:44:44 +02:00
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
protected string CreateThumbCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath, int width, int height)
|
2015-04-08 16:38:02 +02:00
|
|
|
{
|
2015-08-14 19:24:07 +02:00
|
|
|
return CreateCollage(primaryItem, items, outputPath, width, height);
|
2015-04-02 19:44:44 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
private string CreateCollage(BaseItem primaryItem, IEnumerable<BaseItem> items, string outputPath, int width, int height)
|
2015-04-02 18:58:52 +02:00
|
|
|
{
|
2019-01-26 22:08:04 +01:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
|
2015-04-08 16:38:02 +02:00
|
|
|
|
|
|
|
var options = new ImageCollageOptions
|
|
|
|
{
|
|
|
|
Height = height,
|
|
|
|
Width = width,
|
|
|
|
OutputPath = outputPath,
|
|
|
|
InputPaths = GetStripCollageImagePaths(primaryItem, items).ToArray()
|
|
|
|
};
|
|
|
|
|
2021-05-11 13:55:46 +02:00
|
|
|
if (options.InputPaths.Count == 0)
|
2015-05-11 18:32:15 +02:00
|
|
|
{
|
2015-10-26 06:29:32 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ImageProcessor.SupportsImageCollageCreation)
|
|
|
|
{
|
|
|
|
return null;
|
2015-05-11 18:32:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 19:53:55 +02:00
|
|
|
ImageProcessor.CreateImageCollage(options, primaryItem.Name);
|
2015-10-26 06:29:32 +01:00
|
|
|
return outputPath;
|
2014-10-29 23:01:02 +01:00
|
|
|
}
|
|
|
|
|
2019-12-14 07:01:14 +01:00
|
|
|
protected virtual string CreateImage(
|
|
|
|
BaseItem item,
|
2019-03-07 15:54:30 +01:00
|
|
|
IReadOnlyCollection<BaseItem> itemsWithImages,
|
2015-10-26 06:29:32 +01:00
|
|
|
string outputPathWithoutExtension,
|
2015-03-13 20:37:19 +01:00
|
|
|
ImageType imageType,
|
2014-10-29 23:01:02 +01:00
|
|
|
int imageIndex)
|
|
|
|
{
|
|
|
|
if (itemsWithImages.Count == 0)
|
|
|
|
{
|
2015-10-26 06:29:32 +01:00
|
|
|
return null;
|
2014-10-29 23:01:02 +01:00
|
|
|
}
|
|
|
|
|
2015-10-26 06:29:32 +01:00
|
|
|
string outputPath = Path.ChangeExtension(outputPathWithoutExtension, ".png");
|
|
|
|
|
2015-04-02 19:44:44 +02:00
|
|
|
if (imageType == ImageType.Thumb)
|
|
|
|
{
|
2017-05-19 19:09:37 +02:00
|
|
|
return CreateThumbCollage(item, itemsWithImages, outputPath);
|
2015-04-02 19:44:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (imageType == ImageType.Primary)
|
|
|
|
{
|
2019-12-14 07:20:19 +01:00
|
|
|
if (item is UserView
|
|
|
|
|| item is Playlist
|
|
|
|
|| item is MusicGenre
|
|
|
|
|| item is Genre
|
|
|
|
|| item is PhotoAlbum
|
|
|
|
|| item is MusicArtist)
|
2015-04-08 17:45:30 +02:00
|
|
|
{
|
2017-05-19 19:09:37 +02:00
|
|
|
return CreateSquareCollage(item, itemsWithImages, outputPath);
|
2015-04-08 17:45:30 +02:00
|
|
|
}
|
2019-03-07 15:54:30 +01:00
|
|
|
|
2017-05-19 19:09:37 +02:00
|
|
|
return CreatePosterCollage(item, itemsWithImages, outputPath);
|
2015-04-02 19:44:44 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
throw new ArgumentException("Unexpected image type", nameof(imageType));
|
2014-10-29 23:01:02 +01:00
|
|
|
}
|
|
|
|
|
2019-12-14 07:01:14 +01:00
|
|
|
public bool HasChanged(BaseItem item, IDirectoryService directoryService)
|
2014-10-29 23:01:02 +01:00
|
|
|
{
|
|
|
|
if (!Supports(item))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
if (SupportedImages.Contains(ImageType.Primary) && HasChanged(item, ImageType.Primary))
|
2015-07-27 07:03:34 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-14 07:01:14 +01:00
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
if (SupportedImages.Contains(ImageType.Thumb) && HasChanged(item, ImageType.Thumb))
|
2015-07-27 07:03:34 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-10-29 23:01:02 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
protected bool HasChanged(BaseItem item, ImageType type)
|
2015-05-05 17:25:00 +02:00
|
|
|
{
|
|
|
|
var image = item.GetImageInfo(type, 0);
|
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
{
|
2015-10-15 17:51:00 +02:00
|
|
|
if (!image.IsLocalFile)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:25:00 +02:00
|
|
|
if (!FileSystem.ContainsSubPath(item.GetInternalMetadataPath(), image.Path))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-21 18:39:52 +02:00
|
|
|
if (!HasChangedByDate(item, image))
|
2015-05-05 17:25:00 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
protected virtual bool HasChangedByDate(BaseItem item, ItemImageInfo image)
|
2017-10-21 18:39:52 +02:00
|
|
|
{
|
|
|
|
var age = DateTime.UtcNow - image.DateModified;
|
2019-09-25 17:19:48 +02:00
|
|
|
return age.TotalDays > MaxImageAgeDays;
|
2017-10-21 18:39:52 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 15:54:30 +01:00
|
|
|
protected string CreateSingleImage(IEnumerable<BaseItem> itemsWithImages, string outputPathWithoutExtension, ImageType imageType)
|
2015-11-14 17:58:01 +01: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();
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (string.IsNullOrEmpty(image))
|
2015-11-14 17:58:01 +01:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ext = Path.GetExtension(image);
|
|
|
|
|
|
|
|
var outputPath = Path.ChangeExtension(outputPathWithoutExtension, ext);
|
2019-01-26 22:31:59 +01:00
|
|
|
File.Copy(image, outputPath, true);
|
2015-11-14 17:58:01 +01:00
|
|
|
|
|
|
|
return outputPath;
|
|
|
|
}
|
2014-10-29 23:01:02 +01:00
|
|
|
}
|
|
|
|
}
|