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-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-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-08-19 20:38:31 +00:00
|
|
|
|
private async Task<string> DiscoverImagePath()
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-08-20 01:24:44 +00:00
|
|
|
|
string personName = QueryString["personname"];
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-20 01:24:44 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(personName))
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return (await Kernel.Instance.ItemController.GetPerson(personName).ConfigureAwait(false)).PrimaryImagePath;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
|
2012-08-20 01:24:44 +00:00
|
|
|
|
string genreName = QueryString["genre"];
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-20 01:24:44 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(genreName))
|
2012-08-19 20:38:31 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return (await Kernel.Instance.ItemController.GetGenre(genreName).ConfigureAwait(false)).PrimaryImagePath;
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-20 01:24:44 +00:00
|
|
|
|
string year = QueryString["year"];
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(year))
|
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return (await Kernel.Instance.ItemController.GetYear(int.Parse(year)).ConfigureAwait(false)).PrimaryImagePath;
|
2012-08-20 01:24:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string studio = QueryString["studio"];
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(studio))
|
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return (await Kernel.Instance.ItemController.GetStudio(studio).ConfigureAwait(false)).PrimaryImagePath;
|
2012-08-20 01:24:44 +00:00
|
|
|
|
}
|
2012-08-25 20:02:53 +00:00
|
|
|
|
|
|
|
|
|
string userId = QueryString["userid"];
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(userId))
|
|
|
|
|
{
|
2012-09-07 16:17:39 +00:00
|
|
|
|
return ApiService.GetUserById(userId, false).PrimaryImagePath;
|
2012-08-25 20:02:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00: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 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
|
|
|
|
await EnsureSourceStream().ConfigureAwait(false);
|
2012-07-13 03:50:50 +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
|
|
|
|
await EnsureSourceStream().ConfigureAwait(false);
|
2012-08-15 13:20:29 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-08-22 02:50:59 +00:00
|
|
|
|
ImageProcessor.ProcessImage(await GetSourceStream().ConfigureAwait(false), stream, Width, Height, MaxWidth, MaxHeight, Quality);
|
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
|
|
|
|
}
|
|
|
|
|
}
|