|
|
|
@ -1,6 +1,4 @@
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using MediaBrowser.Common.Logging;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
|
|
|
@ -24,6 +22,7 @@ namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string _imagePath;
|
|
|
|
|
|
|
|
|
|
private async Task<string> GetImagePath()
|
|
|
|
|
{
|
|
|
|
|
_imagePath = _imagePath ?? await DiscoverImagePath();
|
|
|
|
@ -32,28 +31,34 @@ namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private BaseEntity _sourceEntity;
|
|
|
|
|
|
|
|
|
|
private async Task<BaseEntity> GetSourceEntity()
|
|
|
|
|
{
|
|
|
|
|
if (_sourceEntity == null)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(QueryString["personname"]))
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
|
|
|
|
|
_sourceEntity =
|
|
|
|
|
await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["genre"]))
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
|
|
|
|
|
_sourceEntity =
|
|
|
|
|
await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["year"]))
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = await Kernel.Instance.ItemController.GetYear(int.Parse(QueryString["year"])).ConfigureAwait(false);
|
|
|
|
|
_sourceEntity =
|
|
|
|
|
await
|
|
|
|
|
Kernel.Instance.ItemController.GetYear(int.Parse(QueryString["year"])).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["studio"]))
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = await Kernel.Instance.ItemController.GetStudio(QueryString["studio"]).ConfigureAwait(false);
|
|
|
|
|
_sourceEntity =
|
|
|
|
|
await Kernel.Instance.ItemController.GetStudio(QueryString["studio"]).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["userid"]))
|
|
|
|
@ -74,85 +79,62 @@ namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
|
|
|
|
var entity = await GetSourceEntity().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var item = entity as BaseItem;
|
|
|
|
|
return ImageProcessor.GetImagePath(entity, ImageType, ImageIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
public override async Task<string> GetContentType()
|
|
|
|
|
{
|
|
|
|
|
if (Kernel.Instance.ImageProcessors.Any(i => i.RequiresTransparency))
|
|
|
|
|
{
|
|
|
|
|
return GetImagePathFromTypes(item, ImageType, ImageIndex);
|
|
|
|
|
return MimeTypes.GetMimeType(".png");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entity.PrimaryImagePath;
|
|
|
|
|
return MimeTypes.GetMimeType(await GetImagePath().ConfigureAwait(false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Stream _sourceStream;
|
|
|
|
|
private async Task<Stream> GetSourceStream()
|
|
|
|
|
public override TimeSpan CacheDuration
|
|
|
|
|
{
|
|
|
|
|
await EnsureSourceStream().ConfigureAwait(false);
|
|
|
|
|
return _sourceStream;
|
|
|
|
|
get { return TimeSpan.FromDays(365); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool _sourceStreamEnsured;
|
|
|
|
|
private async Task EnsureSourceStream()
|
|
|
|
|
protected override async Task<DateTime?> GetLastDateModified()
|
|
|
|
|
{
|
|
|
|
|
if (!_sourceStreamEnsured)
|
|
|
|
|
string path = await GetImagePath().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
DateTime date = File.GetLastWriteTimeUtc(path);
|
|
|
|
|
|
|
|
|
|
// If the file does not exist it will return jan 1, 1601
|
|
|
|
|
// http://msdn.microsoft.com/en-us/library/system.io.file.getlastwritetimeutc.aspx
|
|
|
|
|
if (date.Year == 1601)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_sourceStream = File.OpenRead(await GetImagePath().ConfigureAwait(false));
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
StatusCode = 404;
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException ex)
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
StatusCode = 404;
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
|
{
|
|
|
|
|
StatusCode = 403;
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_sourceStreamEnsured = true;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async override Task<string> GetContentType()
|
|
|
|
|
{
|
|
|
|
|
if (await GetSourceStream().ConfigureAwait(false) == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Kernel.Instance.ImageProcessors.Any(i => i.RequiresTransparency))
|
|
|
|
|
{
|
|
|
|
|
return MimeTypes.GetMimeType(".png");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MimeTypes.GetMimeType(await GetImagePath().ConfigureAwait(false));
|
|
|
|
|
return await GetMostRecentDateModified(date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TimeSpan CacheDuration
|
|
|
|
|
private async Task<DateTime> GetMostRecentDateModified(DateTime imageFileLastDateModified)
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return TimeSpan.FromDays(365);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var date = imageFileLastDateModified;
|
|
|
|
|
|
|
|
|
|
protected async override Task<DateTime?> GetLastDateModified()
|
|
|
|
|
{
|
|
|
|
|
if (await GetSourceStream().ConfigureAwait(false) == null)
|
|
|
|
|
var entity = await GetSourceEntity().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
foreach (var processor in Kernel.Instance.ImageProcessors)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
if (processor.IsConfiguredToProcess(entity, ImageType, ImageIndex))
|
|
|
|
|
{
|
|
|
|
|
if (processor.ProcessingConfigurationDateLastModifiedUtc > date)
|
|
|
|
|
{
|
|
|
|
|
date = processor.ProcessingConfigurationDateLastModifiedUtc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return File.GetLastWriteTimeUtc(await GetImagePath().ConfigureAwait(false));
|
|
|
|
|
return date;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int ImageIndex
|
|
|
|
@ -262,37 +244,9 @@ namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
|
|
|
|
|
protected override async Task WriteResponseToOutputStream(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
Stream sourceStream = await GetSourceStream().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var entity = await GetSourceEntity().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
ImageProcessor.ProcessImage(sourceStream, stream, Width, Height, MaxWidth, MaxHeight, Quality, entity, ImageType, ImageIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetImagePathFromTypes(BaseItem item, ImageType imageType, int imageIndex)
|
|
|
|
|
{
|
|
|
|
|
if (imageType == ImageType.Logo)
|
|
|
|
|
{
|
|
|
|
|
return item.LogoImagePath;
|
|
|
|
|
}
|
|
|
|
|
if (imageType == ImageType.Backdrop)
|
|
|
|
|
{
|
|
|
|
|
return item.BackdropImagePaths.ElementAt(imageIndex);
|
|
|
|
|
}
|
|
|
|
|
if (imageType == ImageType.Banner)
|
|
|
|
|
{
|
|
|
|
|
return item.BannerImagePath;
|
|
|
|
|
}
|
|
|
|
|
if (imageType == ImageType.Art)
|
|
|
|
|
{
|
|
|
|
|
return item.ArtImagePath;
|
|
|
|
|
}
|
|
|
|
|
if (imageType == ImageType.Thumbnail)
|
|
|
|
|
{
|
|
|
|
|
return item.ThumbnailImagePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item.PrimaryImagePath;
|
|
|
|
|
ImageProcessor.ProcessImage(entity, ImageType, ImageIndex, stream, Width, Height, MaxWidth, MaxHeight, Quality);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|