2012-07-13 05:50:50 +02:00
|
|
|
|
using System;
|
2012-07-12 08:55:27 +02:00
|
|
|
|
using System.IO;
|
2012-07-13 05:50:50 +02:00
|
|
|
|
using System.Linq;
|
2012-08-11 20:07:07 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2012-08-15 15:20:29 +02:00
|
|
|
|
using MediaBrowser.Common.Logging;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2012-07-21 20:39:47 +02:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
2012-07-16 18:50:44 +02:00
|
|
|
|
using MediaBrowser.Controller;
|
2012-07-12 08:55:27 +02:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-07-20 04:22:44 +02:00
|
|
|
|
public class ImageHandler : BaseHandler
|
2012-07-12 08:55:27 +02:00
|
|
|
|
{
|
2012-08-15 15:20:29 +02:00
|
|
|
|
private string _ImagePath = null;
|
2012-08-19 22:38:31 +02:00
|
|
|
|
private async Task<string> GetImagePath()
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-08-19 22:38:31 +02:00
|
|
|
|
if (_ImagePath == null)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-08-19 22:38:31 +02:00
|
|
|
|
_ImagePath = await DiscoverImagePath();
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
2012-08-19 22:38:31 +02:00
|
|
|
|
|
|
|
|
|
return _ImagePath;
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
private async Task<string> DiscoverImagePath()
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-08-20 03:24:44 +02:00
|
|
|
|
string personName = QueryString["personname"];
|
2012-08-19 22:38:31 +02:00
|
|
|
|
|
2012-08-20 03:24:44 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(personName))
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return (await Kernel.Instance.ItemController.GetPerson(personName).ConfigureAwait(false)).PrimaryImagePath;
|
2012-08-19 22:38:31 +02:00
|
|
|
|
}
|
2012-08-15 15:20:29 +02:00
|
|
|
|
|
2012-08-20 03:24:44 +02:00
|
|
|
|
string genreName = QueryString["genre"];
|
2012-08-19 22:38:31 +02:00
|
|
|
|
|
2012-08-20 03:24:44 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(genreName))
|
2012-08-19 22:38:31 +02:00
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return (await Kernel.Instance.ItemController.GetGenre(genreName).ConfigureAwait(false)).PrimaryImagePath;
|
2012-08-15 15:20:29 +02:00
|
|
|
|
}
|
2012-08-19 22:38:31 +02:00
|
|
|
|
|
2012-08-20 03:24:44 +02:00
|
|
|
|
string year = QueryString["year"];
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(year))
|
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return (await Kernel.Instance.ItemController.GetYear(int.Parse(year)).ConfigureAwait(false)).PrimaryImagePath;
|
2012-08-20 03:24:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string studio = QueryString["studio"];
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(studio))
|
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return (await Kernel.Instance.ItemController.GetStudio(studio).ConfigureAwait(false)).PrimaryImagePath;
|
2012-08-20 03:24:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
BaseItem item = ApiService.GetItemById(QueryString["id"]);
|
|
|
|
|
|
|
|
|
|
string imageIndex = QueryString["index"];
|
|
|
|
|
int index = string.IsNullOrEmpty(imageIndex) ? 0 : int.Parse(imageIndex);
|
|
|
|
|
|
|
|
|
|
return GetImagePathFromTypes(item, ImageType, index);
|
2012-08-15 15:20:29 +02:00
|
|
|
|
}
|
2012-07-13 05:50:50 +02:00
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
private Stream _SourceStream = null;
|
|
|
|
|
private async Task<Stream> GetSourceStream()
|
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
await EnsureSourceStream().ConfigureAwait(false);
|
2012-08-19 22:38:31 +02:00
|
|
|
|
return _SourceStream;
|
|
|
|
|
}
|
2012-08-15 15:20:29 +02:00
|
|
|
|
|
|
|
|
|
private bool _SourceStreamEnsured = false;
|
2012-08-19 22:38:31 +02:00
|
|
|
|
private async Task EnsureSourceStream()
|
2012-08-15 15:20:29 +02:00
|
|
|
|
{
|
|
|
|
|
if (!_SourceStreamEnsured)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
_SourceStream = File.OpenRead(await GetImagePath().ConfigureAwait(false));
|
2012-08-15 15:20:29 +02:00
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
StatusCode = 404;
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException ex)
|
|
|
|
|
{
|
|
|
|
|
StatusCode = 404;
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
}
|
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
|
{
|
|
|
|
|
StatusCode = 403;
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-08-15 15:20:29 +02:00
|
|
|
|
_SourceStreamEnsured = true;
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
2012-08-15 15:20:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-19 22:38:31 +02:00
|
|
|
|
|
|
|
|
|
public async override Task<string> GetContentType()
|
2012-08-15 15:20:29 +02:00
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
await EnsureSourceStream().ConfigureAwait(false);
|
2012-07-13 05:50:50 +02:00
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
if (await GetSourceStream().ConfigureAwait(false) == null)
|
2012-08-19 22:38:31 +02:00
|
|
|
|
{
|
|
|
|
|
return null;
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
2012-08-19 22:38:31 +02:00
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return MimeTypes.GetMimeType(await GetImagePath().ConfigureAwait(false));
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TimeSpan CacheDuration
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return TimeSpan.FromDays(365);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
protected async override Task<DateTime?> GetLastDateModified()
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
await EnsureSourceStream().ConfigureAwait(false);
|
2012-08-15 15:20:29 +02:00
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
if (await GetSourceStream().ConfigureAwait(false) == null)
|
2012-08-10 15:07:58 +02:00
|
|
|
|
{
|
2012-08-15 15:20:29 +02:00
|
|
|
|
return null;
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
2012-08-15 15:20:29 +02:00
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return File.GetLastWriteTime(await GetImagePath().ConfigureAwait(false));
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? Height
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["height"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? Width
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["width"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? MaxHeight
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["maxheight"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? MaxWidth
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["maxwidth"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? Quality
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["quality"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 21:03:07 +02:00
|
|
|
|
private ImageType ImageType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string imageType = QueryString["type"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(imageType))
|
|
|
|
|
{
|
2012-08-15 15:20:29 +02:00
|
|
|
|
return ImageType.Primary;
|
2012-07-30 21:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (ImageType)Enum.Parse(typeof(ImageType), imageType, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
protected override async Task WriteResponseToOutputStream(Stream stream)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
ImageProcessor.ProcessImage(await GetSourceStream().ConfigureAwait(false), stream, Width, Height, MaxWidth, MaxHeight, Quality);
|
2012-07-13 05:50:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 21:03:07 +02:00
|
|
|
|
private string GetImagePathFromTypes(BaseItem item, ImageType imageType, int imageIndex)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
2012-07-30 21:03:07 +02:00
|
|
|
|
if (imageType == ImageType.Logo)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
return item.LogoImagePath;
|
|
|
|
|
}
|
2012-07-30 21:03:07 +02:00
|
|
|
|
else if (imageType == ImageType.Backdrop)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
return item.BackdropImagePaths.ElementAt(imageIndex);
|
|
|
|
|
}
|
2012-07-30 21:03:07 +02:00
|
|
|
|
else if (imageType == ImageType.Banner)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
return item.BannerImagePath;
|
|
|
|
|
}
|
2012-07-30 21:03:07 +02:00
|
|
|
|
else if (imageType == ImageType.Art)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
return item.ArtImagePath;
|
|
|
|
|
}
|
2012-07-30 21:03:07 +02:00
|
|
|
|
else if (imageType == ImageType.Thumbnail)
|
2012-07-13 05:50:50 +02:00
|
|
|
|
{
|
|
|
|
|
return item.ThumbnailImagePath;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return item.PrimaryImagePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-12 08:55:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|