jellyfin-server/MediaBrowser.Api/HttpHandlers/ImageHandler.cs

270 lines
7.6 KiB
C#
Raw Normal View History

2012-09-18 22:32:37 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Drawing;
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;
using System.ComponentModel.Composition;
2012-09-07 16:17:39 +00:00
using System.IO;
using System.Linq;
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
{
[Export(typeof(BaseHandler))]
public class ImageHandler : BaseHandler
2012-07-12 06:55:27 +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-09-18 22:32:37 +00:00
private async Task<string> GetImagePath()
{
2012-09-11 19:37:14 +00:00
_imagePath = _imagePath ?? await DiscoverImagePath();
2012-09-11 18:20:12 +00:00
return _imagePath;
}
private BaseEntity _sourceEntity;
2012-09-18 22:32:37 +00:00
private async Task<BaseEntity> GetSourceEntity()
{
if (_sourceEntity == null)
{
if (!string.IsNullOrEmpty(QueryString["personname"]))
{
2012-09-18 22:32:37 +00:00
_sourceEntity =
await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
}
else if (!string.IsNullOrEmpty(QueryString["genre"]))
{
2012-09-18 22:32:37 +00:00
_sourceEntity =
await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
}
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);
}
else if (!string.IsNullOrEmpty(QueryString["studio"]))
{
2012-09-18 22:32:37 +00:00
_sourceEntity =
await Kernel.Instance.ItemController.GetStudio(QueryString["studio"]).ConfigureAwait(false);
}
else if (!string.IsNullOrEmpty(QueryString["userid"]))
{
_sourceEntity = ApiService.GetUserById(QueryString["userid"], false);
}
else
{
_sourceEntity = ApiService.GetItemById(QueryString["id"]);
}
}
return _sourceEntity;
}
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-09-18 22:32:37 +00:00
public override TimeSpan CacheDuration
{
2012-09-18 22:32:37 +00:00
get { return TimeSpan.FromDays(365); }
}
2012-09-18 22:32:37 +00:00
protected override async Task<DateTime?> GetLastDateModified()
{
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-09-18 22:32:37 +00:00
if (!File.Exists(path))
{
StatusCode = 404;
2012-09-18 22:32:37 +00:00
return null;
}
}
2012-09-18 22:32:37 +00:00
return await GetMostRecentDateModified(date);
}
2012-09-18 22:32:37 +00:00
private async Task<DateTime> GetMostRecentDateModified(DateTime imageFileLastDateModified)
{
2012-09-18 22:32:37 +00:00
var date = imageFileLastDateModified;
2012-09-18 22:32:37 +00:00
var entity = await GetSourceEntity().ConfigureAwait(false);
foreach (var processor in Kernel.Instance.ImageProcessors)
{
2012-09-18 22:32:37 +00:00
if (processor.IsConfiguredToProcess(entity, ImageType, ImageIndex))
{
if (processor.ProcessingConfigurationDateLastModifiedUtc > date)
{
date = processor.ProcessingConfigurationDateLastModifiedUtc;
}
}
}
2012-09-18 22:32:37 +00:00
return date;
}
2012-09-18 22:50:26 +00:00
protected override async Task<string> GetETag()
{
string tag = string.Empty;
var entity = await GetSourceEntity().ConfigureAwait(false);
foreach (var processor in Kernel.Instance.ImageProcessors)
{
if (processor.IsConfiguredToProcess(entity, ImageType, ImageIndex))
{
tag += processor.ProcessingConfigurationDateLastModifiedUtc.Ticks.ToString();
}
}
return tag;
}
private int ImageIndex
{
get
{
string val = QueryString["index"];
if (string.IsNullOrEmpty(val))
{
return 0;
}
return int.Parse(val);
}
}
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);
}
}
private ImageType ImageType
{
get
{
string imageType = QueryString["type"];
if (string.IsNullOrEmpty(imageType))
{
return ImageType.Primary;
}
return (ImageType)Enum.Parse(typeof(ImageType), imageType, true);
}
}
protected override async Task WriteResponseToOutputStream(Stream stream)
{
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-12 06:55:27 +00:00
}
}