diff options
| author | Luke <luke.pulverenti@gmail.com> | 2013-02-20 17:10:49 -0800 |
|---|---|---|
| committer | Luke <luke.pulverenti@gmail.com> | 2013-02-20 17:10:49 -0800 |
| commit | 845554722efaed872948a9e0f7202e3ef52f1b6e (patch) | |
| tree | 534ab2d11fe4824ed303bb01e885b330631b657e /MediaBrowser.Api/HttpHandlers/ImageHandler.cs | |
| parent | 2f142263f080f7b012a0ec2095d350ccf75b49b7 (diff) | |
| parent | 14bbb6804718ef78a8118fe750896ba679e3b6cb (diff) | |
Merge pull request #1 from MediaBrowser/Repo
Repo
Diffstat (limited to 'MediaBrowser.Api/HttpHandlers/ImageHandler.cs')
| -rw-r--r-- | MediaBrowser.Api/HttpHandlers/ImageHandler.cs | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/MediaBrowser.Api/HttpHandlers/ImageHandler.cs b/MediaBrowser.Api/HttpHandlers/ImageHandler.cs new file mode 100644 index 000000000..4aa367fb7 --- /dev/null +++ b/MediaBrowser.Api/HttpHandlers/ImageHandler.cs @@ -0,0 +1,224 @@ +using MediaBrowser.Common.Net;
+using MediaBrowser.Common.Net.Handlers;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Drawing;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Model.Entities;
+using System;
+using System.ComponentModel.Composition;
+using System.IO;
+using System.Net;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+ [Export(typeof(BaseHandler))]
+ public class ImageHandler : BaseHandler
+ {
+ public override bool HandlesRequest(HttpListenerRequest request)
+ {
+ return ApiService.IsApiUrlMatch("image", request);
+ }
+
+ private string _imagePath;
+
+ private async Task<string> GetImagePath()
+ {
+ _imagePath = _imagePath ?? await DiscoverImagePath();
+
+ return _imagePath;
+ }
+
+ private BaseEntity _sourceEntity;
+
+ private async Task<BaseEntity> GetSourceEntity()
+ {
+ if (_sourceEntity == null)
+ {
+ if (!string.IsNullOrEmpty(QueryString["personname"]))
+ {
+ _sourceEntity =
+ await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
+ }
+
+ else if (!string.IsNullOrEmpty(QueryString["genre"]))
+ {
+ _sourceEntity =
+ await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
+ }
+
+ else if (!string.IsNullOrEmpty(QueryString["year"]))
+ {
+ _sourceEntity =
+ await
+ Kernel.Instance.ItemController.GetYear(int.Parse(QueryString["year"])).ConfigureAwait(false);
+ }
+
+ else if (!string.IsNullOrEmpty(QueryString["studio"]))
+ {
+ _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);
+
+ return ImageProcessor.GetImagePath(entity, ImageType, ImageIndex);
+ }
+
+ protected async override Task<ResponseInfo> GetResponseInfo()
+ {
+ string path = await GetImagePath().ConfigureAwait(false);
+
+ ResponseInfo info = new ResponseInfo
+ {
+ CacheDuration = TimeSpan.FromDays(365),
+ ContentType = MimeTypes.GetMimeType(path)
+ };
+
+ 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.Value.Year == 1601)
+ {
+ if (!File.Exists(path))
+ {
+ info.StatusCode = 404;
+ date = null;
+ }
+ }
+
+ info.DateLastModified = date;
+
+ return info;
+ }
+
+ 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);
+
+ ImageProcessor.ProcessImage(entity, ImageType, ImageIndex, stream, Width, Height, MaxWidth, MaxHeight, Quality);
+ }
+ }
+}
|
