123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- using System;
- using System.IO;
- using System.Linq;
- using MediaBrowser.Common.Net;
- using MediaBrowser.Model.Entities;
- using System.IO.Compression;
- namespace MediaBrowser.Api.HttpHandlers
- {
- public class ImageHandler : Response
- {
- public ImageHandler(RequestContext ctx)
- : base(ctx)
- {
- Headers["Content-Encoding"] = "gzip";
- WriteStream = s =>
- {
- WriteReponse(s);
- s.Close();
- };
- }
- 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);
- }
- }
- public override DateTime? LastDateModified
- {
- get
- {
- try
- {
- return File.GetLastWriteTime(ImagePath);
- }
- catch
- {
- return null;
- }
- }
- }
- 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 void WriteReponse(Stream stream)
- {
- using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress, false))
- {
- ImageProcessor.ProcessImage(ImagePath, gzipStream, Width, Height, MaxWidth, MaxHeight, Quality);
- }
- }
- private string GetImagePath()
- {
- string path = QueryString["path"] ?? string.Empty;
- if (!string.IsNullOrEmpty(path))
- {
- return path;
- }
- string id = QueryString["id"];
- string personName = QueryString["personname"];
- string imageType = QueryString["type"] ?? string.Empty;
- string imageIndex = QueryString["index"];
- BaseItem item;
- if (!string.IsNullOrEmpty(personName))
- {
- item = ApiService.GetPersonByName(personName);
- }
- else
- {
- item = ApiService.GetItemById(QueryString["id"]);
- }
- int index = string.IsNullOrEmpty(imageIndex) ? 0 : int.Parse(imageIndex);
- return GetImagePathFromTypes(item, imageType, index);
- }
- private string GetImagePathFromTypes(BaseItem item, string imageType, int imageIndex)
- {
- if (imageType.Equals("logo", StringComparison.OrdinalIgnoreCase))
- {
- return item.LogoImagePath;
- }
- else if (imageType.Equals("backdrop", StringComparison.OrdinalIgnoreCase))
- {
- return item.BackdropImagePaths.ElementAt(imageIndex);
- }
- else if (imageType.Equals("banner", StringComparison.OrdinalIgnoreCase))
- {
- return item.BannerImagePath;
- }
- else if (imageType.Equals("art", StringComparison.OrdinalIgnoreCase))
- {
- return item.ArtImagePath;
- }
- else if (imageType.Equals("thumbnail", StringComparison.OrdinalIgnoreCase))
- {
- return item.ThumbnailImagePath;
- }
- else
- {
- return item.PrimaryImagePath;
- }
- }
- }
- }
|