2012-09-18 22:32:37 +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-09-18 22:32:37 +00:00
|
|
|
|
|
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;
|
2012-09-18 22:32:37 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
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"]))
|
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
_sourceEntity =
|
|
|
|
|
await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
|
2012-09-18 19:33:57 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["genre"]))
|
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
_sourceEntity =
|
|
|
|
|
await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
|
2012-09-18 19:33:57 +00:00
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["year"]))
|
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
_sourceEntity =
|
|
|
|
|
await
|
|
|
|
|
Kernel.Instance.ItemController.GetYear(int.Parse(QueryString["year"])).ConfigureAwait(false);
|
2012-09-18 19:33:57 +00:00
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
else if (!string.IsNullOrEmpty(QueryString["studio"]))
|
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
_sourceEntity =
|
|
|
|
|
await Kernel.Instance.ItemController.GetStudio(QueryString["studio"]).ConfigureAwait(false);
|
2012-09-18 19:33:57 +00:00
|
|
|
|
}
|
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 22:32:37 +00:00
|
|
|
|
return ImageProcessor.GetImagePath(entity, ImageType, ImageIndex);
|
|
|
|
|
}
|
2012-08-25 20:02:53 +00:00
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
public override async Task<string> GetContentType()
|
|
|
|
|
{
|
|
|
|
|
if (Kernel.Instance.ImageProcessors.Any(i => i.RequiresTransparency))
|
2012-08-25 20:02:53 +00:00
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
return MimeTypes.GetMimeType(".png");
|
2012-08-25 20:02:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
return MimeTypes.GetMimeType(await GetImagePath().ConfigureAwait(false));
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
public override TimeSpan CacheDuration
|
2012-08-19 20:38:31 +00:00
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
get { return TimeSpan.FromDays(365); }
|
2012-08-19 20:38:31 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
protected override async Task<DateTime?> GetLastDateModified()
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
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)
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
if (!File.Exists(path))
|
2012-08-15 13:20:29 +00:00
|
|
|
|
{
|
|
|
|
|
StatusCode = 404;
|
2012-09-18 22:32:37 +00:00
|
|
|
|
return null;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
return await GetMostRecentDateModified(date);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
private async Task<DateTime> GetMostRecentDateModified(DateTime imageFileLastDateModified)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
var date = imageFileLastDateModified;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
var entity = await GetSourceEntity().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
foreach (var processor in Kernel.Instance.ImageProcessors)
|
2012-08-10 13:07:58 +00:00
|
|
|
|
{
|
2012-09-18 22:32:37 +00:00
|
|
|
|
if (processor.IsConfiguredToProcess(entity, ImageType, ImageIndex))
|
|
|
|
|
{
|
|
|
|
|
if (processor.ProcessingConfigurationDateLastModifiedUtc > date)
|
|
|
|
|
{
|
|
|
|
|
date = processor.ProcessingConfigurationDateLastModifiedUtc;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
return date;
|
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
|
|
|
|
var entity = await GetSourceEntity().ConfigureAwait(false);
|
|
|
|
|
|
2012-09-18 22:32:37 +00:00
|
|
|
|
ImageProcessor.ProcessImage(entity, ImageType, ImageIndex, stream, Width, Height, MaxWidth, MaxHeight, Quality);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|