2012-07-13 03:50:50 +00:00
|
|
|
|
using System;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
using System.IO;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
using System.Linq;
|
2012-08-11 18:07:07 +00:00
|
|
|
|
using System.Threading.Tasks;
|
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-07-12 06:55:27 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-07-20 02:22:44 +00:00
|
|
|
|
public class ImageHandler : BaseHandler
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-07-13 03:50:50 +00:00
|
|
|
|
private string _ImagePath = string.Empty;
|
|
|
|
|
private string ImagePath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_ImagePath))
|
|
|
|
|
{
|
|
|
|
|
_ImagePath = GetImagePath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _ImagePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ContentType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string extension = Path.GetExtension(ImagePath);
|
|
|
|
|
|
|
|
|
|
if (extension.EndsWith("png", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "image/png";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "image/jpeg";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override TimeSpan CacheDuration
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return TimeSpan.FromDays(365);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-10 13:07:58 +00:00
|
|
|
|
protected override DateTime? GetLastDateModified()
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
try
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-08-10 13:07:58 +00:00
|
|
|
|
return File.GetLastWriteTime(ImagePath);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return base.GetLastDateModified();
|
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))
|
|
|
|
|
{
|
|
|
|
|
return Model.Entities.ImageType.Primary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (ImageType)Enum.Parse(typeof(ImageType), imageType, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 18:07:07 +00:00
|
|
|
|
protected override Task WriteResponseToOutputStream(Stream stream)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-08-11 18:07:07 +00:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
ImageProcessor.ProcessImage(ImagePath, stream, Width, Height, MaxWidth, MaxHeight, Quality);
|
|
|
|
|
});
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetImagePath()
|
|
|
|
|
{
|
|
|
|
|
string path = QueryString["path"] ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string personName = QueryString["personname"];
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(personName))
|
|
|
|
|
{
|
2012-08-12 23:57:54 +00:00
|
|
|
|
return Kernel.Instance.ItemController.GetPerson(personName).PrimaryImagePath;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 23:57:54 +00:00
|
|
|
|
BaseItem item = ApiService.GetItemById(QueryString["id"]);
|
|
|
|
|
|
|
|
|
|
string imageIndex = QueryString["index"];
|
2012-07-13 03:50:50 +00:00
|
|
|
|
int index = string.IsNullOrEmpty(imageIndex) ? 0 : int.Parse(imageIndex);
|
|
|
|
|
|
2012-07-30 19:03:07 +00:00
|
|
|
|
return GetImagePathFromTypes(item, ImageType, index);
|
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-07-30 19:03:07 +00:00
|
|
|
|
else if (imageType == ImageType.Backdrop)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
return item.BackdropImagePaths.ElementAt(imageIndex);
|
|
|
|
|
}
|
2012-07-30 19:03:07 +00:00
|
|
|
|
else if (imageType == ImageType.Banner)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
return item.BannerImagePath;
|
|
|
|
|
}
|
2012-07-30 19:03:07 +00:00
|
|
|
|
else if (imageType == ImageType.Art)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
return item.ArtImagePath;
|
|
|
|
|
}
|
2012-07-30 19:03:07 +00:00
|
|
|
|
else if (imageType == ImageType.Thumbnail)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
|
|
|
|
return item.ThumbnailImagePath;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return item.PrimaryImagePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|