2012-09-07 16:17:39 +00:00
|
|
|
|
using MediaBrowser.Common.Logging;
|
2012-08-15 13:20:29 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
2012-07-16 16:50:44 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2012-09-18 19:33:57 +00:00
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
2012-09-11 01:34:02 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
using System;
|
2012-09-08 14:52:13 +00:00
|
|
|
|
using System.ComponentModel.Composition;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2012-09-08 14:52:13 +00:00
|
|
|
|
using System.Net;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-09-08 14:52:13 +00:00
|
|
|
|
[Export(typeof(BaseHandler))]
|
2012-07-20 02:22:44 +00:00
|
|
|
|
public class ImageHandler : BaseHandler
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-09-08 14:52:13 +00:00
|
|
|
|
public override bool HandlesRequest(HttpListenerRequest request)
|
|
|
|
|
{
|
|
|
|
|
return ApiService.IsApiUrlMatch("image", request);
|
|
|
|
|
}
|
2012-09-18 19:33:57 +00:00
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
private string _imagePath;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
private async Task<string> GetImagePath()
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-09-11 19:37:14 +00:00
|
|
|
|
_imagePath = _imagePath ?? await DiscoverImagePath();
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
return _imagePath;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
private BaseEntity _sourceEntity;
|
|
|
|
|
private async Task<BaseEntity> GetSourceEntity()
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
if (_sourceEntity == null)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(QueryString["personname"]))
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
|
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["genre"]))
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
|
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["year"]))
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = await Kernel.Instance.ItemController.GetYear(int.Parse(QueryString["year"])).ConfigureAwait(false);
|
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["studio"]))
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = await Kernel.Instance.ItemController.GetStudio(QueryString["studio"]).ConfigureAwait(false);
|
|
|
|
|
}
|
2012-08-20 01:24:44 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["userid"]))
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = ApiService.GetUserById(QueryString["userid"], false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_sourceEntity = ApiService.GetItemById(QueryString["id"]);
|
|
|
|
|
}
|
2012-08-20 01:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
return _sourceEntity;
|
|
|
|
|
}
|
2012-08-20 01:24:44 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
private async Task<string> DiscoverImagePath()
|
|
|
|
|
{
|
|
|
|
|
var entity = await GetSourceEntity().ConfigureAwait(false);
|
2012-08-25 20:02:53 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
var item = entity as BaseItem;
|
2012-08-25 20:02:53 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
if (item != null)
|
2012-08-25 20:02:53 +00:00
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
return GetImagePathFromTypes(item, ImageType, ImageIndex);
|
2012-08-25 20:02:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
return entity.PrimaryImagePath;
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
private Stream _sourceStream;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
private async Task<Stream> GetSourceStream()
|
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
await EnsureSourceStream().ConfigureAwait(false);
|
2012-09-11 18:20:12 +00:00
|
|
|
|
return _sourceStream;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
private bool _sourceStreamEnsured;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
private async Task EnsureSourceStream()
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
if (!_sourceStreamEnsured)
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
_sourceStream = File.OpenRead(await GetImagePath().ConfigureAwait(false));
|
2012-08-15 13:20:29 +00: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 03:50:50 +00:00
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
_sourceStreamEnsured = true;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
|
|
|
|
public async override Task<string> GetContentType()
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
if (await GetSourceStream().ConfigureAwait(false) == null)
|
2012-08-19 20:38:31 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return MimeTypes.GetMimeType(await GetImagePath().ConfigureAwait(false));
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TimeSpan CacheDuration
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return TimeSpan.FromDays(365);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
protected async override Task<DateTime?> GetLastDateModified()
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
if (await GetSourceStream().ConfigureAwait(false) == null)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
return null;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
|
2012-09-04 19:23:15 +00:00
|
|
|
|
return File.GetLastWriteTimeUtc(await GetImagePath().ConfigureAwait(false));
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
private int ImageIndex
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["index"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-13 03:50:50 +00: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 19:03:07 +00:00
|
|
|
|
private ImageType ImageType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string imageType = QueryString["type"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(imageType))
|
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
return ImageType.Primary;
|
2012-07-30 19:03:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (ImageType)Enum.Parse(typeof(ImageType), imageType, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
protected override async Task WriteResponseToOutputStream(Stream stream)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
Stream sourceStream = await GetSourceStream().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var entity = await GetSourceEntity().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
ImageProcessor.ProcessImage(sourceStream, stream, Width, Height, MaxWidth, MaxHeight, Quality, entity, ImageType, ImageIndex);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 19:03:07 +00:00
|
|
|
|
private string GetImagePathFromTypes(BaseItem item, ImageType imageType, int imageIndex)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-07-30 19:03:07 +00:00
|
|
|
|
if (imageType == ImageType.Logo)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
return item.LogoImagePath;
|
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
if (imageType == ImageType.Backdrop)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
return item.BackdropImagePaths.ElementAt(imageIndex);
|
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
if (imageType == ImageType.Banner)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
return item.BannerImagePath;
|
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
if (imageType == ImageType.Art)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
return item.ArtImagePath;
|
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
if (imageType == ImageType.Thumbnail)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
return item.ThumbnailImagePath;
|
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
|
|
|
|
|
return item.PrimaryImagePath;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|