|
|
|
@ -1,108 +1,79 @@
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Drawing;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Drawing;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Drawing
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class ImageManager
|
|
|
|
|
/// Class ImageProcessor
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ImageManager
|
|
|
|
|
public class ImageProcessor : IImageProcessor
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list of currently registered image processors
|
|
|
|
|
/// Image processors are specialized metadata providers that run after the normal ones
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The image enhancers.</value>
|
|
|
|
|
public IEnumerable<IImageEnhancer> ImageEnhancers { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image size cache.
|
|
|
|
|
/// The us culture
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The image size cache.</value>
|
|
|
|
|
private FileSystemRepository ImageSizeCache { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the resized image cache.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The resized image cache.</value>
|
|
|
|
|
private FileSystemRepository ResizedImageCache { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the cropped image cache.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The cropped image cache.</value>
|
|
|
|
|
private FileSystemRepository CroppedImageCache { get; set; }
|
|
|
|
|
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the cropped image cache.
|
|
|
|
|
/// The _cached imaged sizes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The cropped image cache.</value>
|
|
|
|
|
private FileSystemRepository EnhancedImageCache { get; set; }
|
|
|
|
|
private readonly ConcurrentDictionary<string, ImageSize> _cachedImagedSizes = new ConcurrentDictionary<string, ImageSize>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The cached imaged sizes
|
|
|
|
|
/// Gets the list of currently registered image processors
|
|
|
|
|
/// Image processors are specialized metadata providers that run after the normal ones
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ConcurrentDictionary<string, ImageSize> _cachedImagedSizes = new ConcurrentDictionary<string, ImageSize>();
|
|
|
|
|
/// <value>The image enhancers.</value>
|
|
|
|
|
public IEnumerable<IImageEnhancer> ImageEnhancers { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly IItemRepository _itemRepo;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="ImageManager" /> class.
|
|
|
|
|
/// The _app paths
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="itemRepo">The item repo.</param>
|
|
|
|
|
public ImageManager(ILogger logger, IServerApplicationPaths appPaths, IItemRepository itemRepo)
|
|
|
|
|
private readonly IServerApplicationPaths _appPaths;
|
|
|
|
|
|
|
|
|
|
private readonly string _imageSizeCachePath;
|
|
|
|
|
private readonly string _croppedWhitespaceImageCachePath;
|
|
|
|
|
private readonly string _enhancedImageCachePath;
|
|
|
|
|
private readonly string _resizedImageCachePath;
|
|
|
|
|
|
|
|
|
|
public ImageProcessor(ILogger logger, IServerApplicationPaths appPaths)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_itemRepo = itemRepo;
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
|
|
|
|
|
ImageSizeCache = new FileSystemRepository(Path.Combine(appPaths.ImageCachePath, "image-sizes"));
|
|
|
|
|
ResizedImageCache = new FileSystemRepository(Path.Combine(appPaths.ImageCachePath, "resized-images"));
|
|
|
|
|
CroppedImageCache = new FileSystemRepository(Path.Combine(appPaths.ImageCachePath, "cropped-images"));
|
|
|
|
|
EnhancedImageCache = new FileSystemRepository(Path.Combine(appPaths.ImageCachePath, "enhanced-images"));
|
|
|
|
|
_imageSizeCachePath = Path.Combine(_appPaths.ImageCachePath, "image-sizes");
|
|
|
|
|
_croppedWhitespaceImageCachePath = Path.Combine(_appPaths.ImageCachePath, "cropped-images");
|
|
|
|
|
_enhancedImageCachePath = Path.Combine(_appPaths.ImageCachePath, "enhanced-images");
|
|
|
|
|
_resizedImageCachePath = Path.Combine(_appPaths.ImageCachePath, "resized-images");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddParts(IEnumerable<IImageEnhancer> enhancers)
|
|
|
|
|
{
|
|
|
|
|
ImageEnhancers = enhancers.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes an image by resizing to target dimensions
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entity">The entity that owns the image</param>
|
|
|
|
|
/// <param name="imageType">The image type</param>
|
|
|
|
|
/// <param name="imageIndex">The image index (currently only used with backdrops)</param>
|
|
|
|
|
/// <param name="originalImagePath">The original image path.</param>
|
|
|
|
|
/// <param name="cropWhitespace">if set to <c>true</c> [crop whitespace].</param>
|
|
|
|
|
/// <param name="dateModified">The last date modified of the original image file</param>
|
|
|
|
|
/// <param name="toStream">The stream to save the new image to</param>
|
|
|
|
|
/// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
|
|
|
|
|
/// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
|
|
|
|
|
/// <param name="enhancers">The enhancers.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">entity</exception>
|
|
|
|
|
public async Task ProcessImage(BaseItem entity, ImageType imageType, int imageIndex, string originalImagePath, bool cropWhitespace, DateTime dateModified, Stream toStream, int? width, int? height, int? maxWidth, int? maxHeight, int? quality, List<IImageEnhancer> enhancers)
|
|
|
|
|
{
|
|
|
|
|
if (entity == null)
|
|
|
|
@ -117,7 +88,7 @@ namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
|
|
|
|
|
if (cropWhitespace)
|
|
|
|
|
{
|
|
|
|
|
originalImagePath = await GetCroppedImage(originalImagePath, dateModified).ConfigureAwait(false);
|
|
|
|
|
originalImagePath = await GetWhitespaceCroppedImage(originalImagePath, dateModified).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No enhancement - don't cache
|
|
|
|
@ -246,6 +217,76 @@ namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Crops whitespace from an image, caches the result, and returns the cached path
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="originalImagePath">The original image path.</param>
|
|
|
|
|
/// <param name="dateModified">The date modified.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private async Task<string> GetWhitespaceCroppedImage(string originalImagePath, DateTime dateModified)
|
|
|
|
|
{
|
|
|
|
|
var name = originalImagePath;
|
|
|
|
|
name += "datemodified=" + dateModified.Ticks;
|
|
|
|
|
|
|
|
|
|
var croppedImagePath = GetCachePath(_croppedWhitespaceImageCachePath, name, Path.GetExtension(originalImagePath));
|
|
|
|
|
|
|
|
|
|
var semaphore = GetLock(croppedImagePath);
|
|
|
|
|
|
|
|
|
|
await semaphore.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
// Check again in case of contention
|
|
|
|
|
if (File.Exists(croppedImagePath))
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
return croppedImagePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
|
|
|
|
|
{
|
|
|
|
|
// Copy to memory stream to avoid Image locking file
|
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
using (var originalImage = (Bitmap)Image.FromStream(memoryStream, true, false))
|
|
|
|
|
{
|
|
|
|
|
var outputFormat = originalImage.RawFormat;
|
|
|
|
|
|
|
|
|
|
using (var croppedImage = originalImage.CropWhitespace())
|
|
|
|
|
{
|
|
|
|
|
var parentPath = Path.GetDirectoryName(croppedImagePath);
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(parentPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(parentPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var outputStream = new FileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
|
|
|
{
|
|
|
|
|
croppedImage.Save(outputFormat, outputStream, 100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// We have to have a catch-all here because some of the .net image methods throw a plain old Exception
|
|
|
|
|
_logger.ErrorException("Error cropping image {0}", ex, originalImagePath);
|
|
|
|
|
|
|
|
|
|
return originalImagePath;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return croppedImagePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Caches the resized image.
|
|
|
|
|
/// </summary>
|
|
|
|
@ -288,31 +329,40 @@ namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
|
|
|
|
|
filename += "datemodified=" + dateModified.Ticks;
|
|
|
|
|
|
|
|
|
|
return ResizedImageCache.GetResourcePath(filename, Path.GetExtension(originalPath));
|
|
|
|
|
return GetCachePath(_resizedImageCachePath, filename, Path.GetExtension(originalPath));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the size of the image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns>ImageSize.</returns>
|
|
|
|
|
public ImageSize GetImageSize(string path)
|
|
|
|
|
{
|
|
|
|
|
return GetImageSize(path, File.GetLastWriteTimeUtc(path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets image dimensions
|
|
|
|
|
/// Gets the size of the image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="imagePath">The image path.</param>
|
|
|
|
|
/// <param name="dateModified">The date modified.</param>
|
|
|
|
|
/// <returns>Task{ImageSize}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">imagePath</exception>
|
|
|
|
|
public ImageSize GetImageSize(string imagePath, DateTime dateModified)
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <param name="imageDateModified">The image date modified.</param>
|
|
|
|
|
/// <returns>ImageSize.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">path</exception>
|
|
|
|
|
public ImageSize GetImageSize(string path, DateTime imageDateModified)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(imagePath))
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("imagePath");
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var name = imagePath + "datemodified=" + dateModified.Ticks;
|
|
|
|
|
var name = path + "datemodified=" + imageDateModified.Ticks;
|
|
|
|
|
|
|
|
|
|
ImageSize size;
|
|
|
|
|
|
|
|
|
|
if (!_cachedImagedSizes.TryGetValue(name, out size))
|
|
|
|
|
{
|
|
|
|
|
size = GetImageSize(name, imagePath);
|
|
|
|
|
size = GetImageSizeInternal(name, path);
|
|
|
|
|
|
|
|
|
|
_cachedImagedSizes.AddOrUpdate(name, size, (keyName, oldValue) => size);
|
|
|
|
|
}
|
|
|
|
@ -320,18 +370,16 @@ namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the size of the image.
|
|
|
|
|
/// Gets the image size internal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="keyName">Name of the key.</param>
|
|
|
|
|
/// <param name="imagePath">The image path.</param>
|
|
|
|
|
/// <param name="cacheKey">The cache key.</param>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns>ImageSize.</returns>
|
|
|
|
|
private ImageSize GetImageSize(string keyName, string imagePath)
|
|
|
|
|
private ImageSize GetImageSizeInternal(string cacheKey, string path)
|
|
|
|
|
{
|
|
|
|
|
// Now check the file system cache
|
|
|
|
|
var fullCachePath = ImageSizeCache.GetResourcePath(keyName, ".txt");
|
|
|
|
|
var fullCachePath = GetCachePath(_imageSizeCachePath, cacheKey, ".txt");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -366,7 +414,7 @@ namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
// Cache file doesn't exist no biggie
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var size = ImageHeader.GetDimensions(imagePath, _logger);
|
|
|
|
|
var size = ImageHeader.GetDimensions(path, _logger);
|
|
|
|
|
|
|
|
|
|
var parentPath = Path.GetDirectoryName(fullCachePath);
|
|
|
|
|
|
|
|
|
@ -383,173 +431,64 @@ namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image path.
|
|
|
|
|
/// Gets the image cache tag.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="imageType">Type of the image.</param>
|
|
|
|
|
/// <param name="imageIndex">Index of the image.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
/// <param name="imagePath">The image path.</param>
|
|
|
|
|
/// <returns>Guid.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">item</exception>
|
|
|
|
|
/// <exception cref="System.InvalidOperationException"></exception>
|
|
|
|
|
public string GetImagePath(BaseItem item, ImageType imageType, int imageIndex)
|
|
|
|
|
public Guid GetImageCacheTag(BaseItem item, ImageType imageType, string imagePath)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imageType == ImageType.Backdrop)
|
|
|
|
|
if (string.IsNullOrEmpty(imagePath))
|
|
|
|
|
{
|
|
|
|
|
if (item.BackdropImagePaths == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(string.Format("Item {0} does not have any Backdrops.", item.Name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item.BackdropImagePaths[imageIndex];
|
|
|
|
|
throw new ArgumentNullException("imagePath");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imageType == ImageType.Screenshot)
|
|
|
|
|
{
|
|
|
|
|
if (item.ScreenshotImagePaths == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(string.Format("Item {0} does not have any Screenshots.", item.Name));
|
|
|
|
|
}
|
|
|
|
|
var dateModified = item.GetImageDateModified(imagePath);
|
|
|
|
|
|
|
|
|
|
return item.ScreenshotImagePaths[imageIndex];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imageType == ImageType.Chapter)
|
|
|
|
|
{
|
|
|
|
|
return _itemRepo.GetChapter(item.Id, imageIndex).ImagePath;
|
|
|
|
|
}
|
|
|
|
|
var supportedEnhancers = GetSupportedEnhancers(item, imageType).ToList();
|
|
|
|
|
|
|
|
|
|
return item.GetImage(imageType);
|
|
|
|
|
return GetImageCacheTag(item, imageType, imagePath, dateModified, supportedEnhancers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image date modified.
|
|
|
|
|
/// Gets the image cache tag.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="imageType">Type of the image.</param>
|
|
|
|
|
/// <param name="imageIndex">Index of the image.</param>
|
|
|
|
|
/// <returns>DateTime.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">item</exception>
|
|
|
|
|
public DateTime GetImageDateModified(BaseItem item, ImageType imageType, int imageIndex)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var imagePath = GetImagePath(item, imageType, imageIndex);
|
|
|
|
|
|
|
|
|
|
return GetImageDateModified(item, imagePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image date modified.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="imagePath">The image path.</param>
|
|
|
|
|
/// <returns>DateTime.</returns>
|
|
|
|
|
/// <param name="originalImagePath">The original image path.</param>
|
|
|
|
|
/// <param name="dateModified">The date modified of the original image file.</param>
|
|
|
|
|
/// <param name="imageEnhancers">The image enhancers.</param>
|
|
|
|
|
/// <returns>Guid.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">item</exception>
|
|
|
|
|
public DateTime GetImageDateModified(BaseItem item, string imagePath)
|
|
|
|
|
public Guid GetImageCacheTag(BaseItem item, ImageType imageType, string originalImagePath, DateTime dateModified, IEnumerable<IImageEnhancer> imageEnhancers)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(imagePath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("imagePath");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var metaFileEntry = item.ResolveArgs.GetMetaFileByPath(imagePath);
|
|
|
|
|
|
|
|
|
|
// If we didn't the metafile entry, check the Season
|
|
|
|
|
if (metaFileEntry == null)
|
|
|
|
|
{
|
|
|
|
|
var episode = item as Episode;
|
|
|
|
|
|
|
|
|
|
if (episode != null && episode.Season != null)
|
|
|
|
|
{
|
|
|
|
|
episode.Season.ResolveArgs.GetMetaFileByPath(imagePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// See if we can avoid a file system lookup by looking for the file in ResolveArgs
|
|
|
|
|
return metaFileEntry == null ? File.GetLastWriteTimeUtc(imagePath) : metaFileEntry.LastWriteTimeUtc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Crops whitespace from an image, caches the result, and returns the cached path
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="originalImagePath">The original image path.</param>
|
|
|
|
|
/// <param name="dateModified">The date modified.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private async Task<string> GetCroppedImage(string originalImagePath, DateTime dateModified)
|
|
|
|
|
{
|
|
|
|
|
var name = originalImagePath;
|
|
|
|
|
name += "datemodified=" + dateModified.Ticks;
|
|
|
|
|
|
|
|
|
|
var croppedImagePath = CroppedImageCache.GetResourcePath(name, Path.GetExtension(originalImagePath));
|
|
|
|
|
|
|
|
|
|
var semaphore = GetLock(croppedImagePath);
|
|
|
|
|
|
|
|
|
|
await semaphore.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
// Check again in case of contention
|
|
|
|
|
if (File.Exists(croppedImagePath))
|
|
|
|
|
if (imageEnhancers == null)
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
return croppedImagePath;
|
|
|
|
|
throw new ArgumentNullException("imageEnhancers");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
if (string.IsNullOrEmpty(originalImagePath))
|
|
|
|
|
{
|
|
|
|
|
using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
|
|
|
|
|
{
|
|
|
|
|
// Copy to memory stream to avoid Image locking file
|
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
using (var originalImage = (Bitmap)Image.FromStream(memoryStream, true, false))
|
|
|
|
|
{
|
|
|
|
|
var outputFormat = originalImage.RawFormat;
|
|
|
|
|
|
|
|
|
|
using (var croppedImage = originalImage.CropWhitespace())
|
|
|
|
|
{
|
|
|
|
|
var parentPath = Path.GetDirectoryName(croppedImagePath);
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(parentPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(parentPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var outputStream = new FileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
|
|
|
{
|
|
|
|
|
croppedImage.Save(outputFormat, outputStream, 100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new ArgumentNullException("originalImagePath");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// We have to have a catch-all here because some of the .net image methods throw a plain old Exception
|
|
|
|
|
_logger.ErrorException("Error cropping image {0}", ex, originalImagePath);
|
|
|
|
|
|
|
|
|
|
return originalImagePath;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
// Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
|
|
|
|
|
var cacheKeys = imageEnhancers.Select(i => i.GetConfigurationCacheKey(item, imageType)).ToList();
|
|
|
|
|
cacheKeys.Add(originalImagePath + dateModified.Ticks);
|
|
|
|
|
|
|
|
|
|
return croppedImagePath;
|
|
|
|
|
return string.Join("|", cacheKeys.ToArray()).GetMD5();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -610,10 +549,10 @@ namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cacheGuid = GetImageCacheTag(originalImagePath, dateModified, supportedEnhancers, item, imageType);
|
|
|
|
|
var cacheGuid = GetImageCacheTag(item, imageType, originalImagePath, dateModified, supportedEnhancers);
|
|
|
|
|
|
|
|
|
|
// All enhanced images are saved as png to allow transparency
|
|
|
|
|
var enhancedImagePath = EnhancedImageCache.GetResourcePath(cacheGuid + ".png");
|
|
|
|
|
var enhancedImagePath = GetCachePath(_enhancedImageCachePath, cacheGuid + ".png");
|
|
|
|
|
|
|
|
|
|
var semaphore = GetLock(enhancedImagePath);
|
|
|
|
|
|
|
|
|
@ -665,80 +604,6 @@ namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
return enhancedImagePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image cache tag.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="imageType">Type of the image.</param>
|
|
|
|
|
/// <param name="imagePath">The image path.</param>
|
|
|
|
|
/// <returns>Guid.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">item</exception>
|
|
|
|
|
public Guid GetImageCacheTag(BaseItem item, ImageType imageType, string imagePath)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(imagePath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("imagePath");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var dateModified = GetImageDateModified(item, imagePath);
|
|
|
|
|
|
|
|
|
|
var supportedEnhancers = ImageEnhancers.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return i.Supports(item, imageType);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
|
|
|
|
return GetImageCacheTag(imagePath, dateModified, supportedEnhancers, item, imageType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image cache tag.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="originalImagePath">The original image path.</param>
|
|
|
|
|
/// <param name="dateModified">The date modified of the original image file.</param>
|
|
|
|
|
/// <param name="imageEnhancers">The image enhancers.</param>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="imageType">Type of the image.</param>
|
|
|
|
|
/// <returns>Guid.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">item</exception>
|
|
|
|
|
public Guid GetImageCacheTag(string originalImagePath, DateTime dateModified, IEnumerable<IImageEnhancer> imageEnhancers, BaseItem item, ImageType imageType)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imageEnhancers == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("imageEnhancers");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(originalImagePath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("originalImagePath");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
|
|
|
|
|
var cacheKeys = imageEnhancers.Select(i => i.GetConfigurationCacheKey(item, imageType)).ToList();
|
|
|
|
|
cacheKeys.Add(originalImagePath + dateModified.Ticks);
|
|
|
|
|
|
|
|
|
|
return string.Join("|", cacheKeys.ToArray()).GetMD5();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes the image enhancers.
|
|
|
|
|
/// </summary>
|
|
|
|
@ -775,31 +640,113 @@ namespace MediaBrowser.Controller.Drawing
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _semaphoreLocks
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
|
|
|
|
private readonly ConcurrentDictionary<string, object> _locks = new ConcurrentDictionary<string, object>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the lock.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
private SemaphoreSlim GetLock(string filename)
|
|
|
|
|
private object GetObjectLock(string filename)
|
|
|
|
|
{
|
|
|
|
|
return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
|
|
|
|
|
return _locks.GetOrAdd(filename, key => new object());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _semaphoreLocks
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ConcurrentDictionary<string, object> _locks = new ConcurrentDictionary<string, object>();
|
|
|
|
|
private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the lock.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
private object GetObjectLock(string filename)
|
|
|
|
|
private SemaphoreSlim GetLock(string filename)
|
|
|
|
|
{
|
|
|
|
|
return _locks.GetOrAdd(filename, key => new object());
|
|
|
|
|
return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the cache path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <param name="uniqueName">Name of the unique.</param>
|
|
|
|
|
/// <param name="fileExtension">The file extension.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
|
/// path
|
|
|
|
|
/// or
|
|
|
|
|
/// uniqueName
|
|
|
|
|
/// or
|
|
|
|
|
/// fileExtension
|
|
|
|
|
/// </exception>
|
|
|
|
|
public string GetCachePath(string path, string uniqueName, string fileExtension)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(uniqueName))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("uniqueName");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(fileExtension))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("fileExtension");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var filename = uniqueName.GetMD5() + fileExtension;
|
|
|
|
|
|
|
|
|
|
return GetCachePath(path, filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the cache path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
|
/// path
|
|
|
|
|
/// or
|
|
|
|
|
/// filename
|
|
|
|
|
/// </exception>
|
|
|
|
|
public string GetCachePath(string path, string filename)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(filename))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("filename");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var prefix = filename.Substring(0, 1);
|
|
|
|
|
|
|
|
|
|
path = Path.Combine(path, prefix);
|
|
|
|
|
|
|
|
|
|
return Path.Combine(path, filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IImageEnhancer> GetSupportedEnhancers(BaseItem item, ImageType imageType)
|
|
|
|
|
{
|
|
|
|
|
return ImageEnhancers.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return i.Supports(item as BaseItem, imageType);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|