ImageHandler.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using MediaBrowser.Common.Net;
  5. using MediaBrowser.Model.Entities;
  6. using System.IO.Compression;
  7. namespace MediaBrowser.Api.HttpHandlers
  8. {
  9. public class ImageHandler : Response
  10. {
  11. public ImageHandler(RequestContext ctx)
  12. : base(ctx)
  13. {
  14. Headers["Content-Encoding"] = "gzip";
  15. WriteStream = s =>
  16. {
  17. WriteReponse(s);
  18. s.Close();
  19. };
  20. }
  21. private string _ImagePath = string.Empty;
  22. private string ImagePath
  23. {
  24. get
  25. {
  26. if (string.IsNullOrEmpty(_ImagePath))
  27. {
  28. _ImagePath = GetImagePath();
  29. }
  30. return _ImagePath;
  31. }
  32. }
  33. public override string ContentType
  34. {
  35. get
  36. {
  37. string extension = Path.GetExtension(ImagePath);
  38. if (extension.EndsWith("png", StringComparison.OrdinalIgnoreCase))
  39. {
  40. return "image/png";
  41. }
  42. return "image/jpeg";
  43. }
  44. }
  45. public override TimeSpan CacheDuration
  46. {
  47. get
  48. {
  49. return TimeSpan.FromDays(365);
  50. }
  51. }
  52. public override DateTime? LastDateModified
  53. {
  54. get
  55. {
  56. try
  57. {
  58. return File.GetLastWriteTime(ImagePath);
  59. }
  60. catch
  61. {
  62. return null;
  63. }
  64. }
  65. }
  66. private int? Height
  67. {
  68. get
  69. {
  70. string val = QueryString["height"];
  71. if (string.IsNullOrEmpty(val))
  72. {
  73. return null;
  74. }
  75. return int.Parse(val);
  76. }
  77. }
  78. private int? Width
  79. {
  80. get
  81. {
  82. string val = QueryString["width"];
  83. if (string.IsNullOrEmpty(val))
  84. {
  85. return null;
  86. }
  87. return int.Parse(val);
  88. }
  89. }
  90. private int? MaxHeight
  91. {
  92. get
  93. {
  94. string val = QueryString["maxheight"];
  95. if (string.IsNullOrEmpty(val))
  96. {
  97. return null;
  98. }
  99. return int.Parse(val);
  100. }
  101. }
  102. private int? MaxWidth
  103. {
  104. get
  105. {
  106. string val = QueryString["maxwidth"];
  107. if (string.IsNullOrEmpty(val))
  108. {
  109. return null;
  110. }
  111. return int.Parse(val);
  112. }
  113. }
  114. private int? Quality
  115. {
  116. get
  117. {
  118. string val = QueryString["quality"];
  119. if (string.IsNullOrEmpty(val))
  120. {
  121. return null;
  122. }
  123. return int.Parse(val);
  124. }
  125. }
  126. private void WriteReponse(Stream stream)
  127. {
  128. using (GZipStream gzipStream = new GZipStream(stream, CompressionMode.Compress, false))
  129. {
  130. ImageProcessor.ProcessImage(ImagePath, gzipStream, Width, Height, MaxWidth, MaxHeight, Quality);
  131. }
  132. }
  133. private string GetImagePath()
  134. {
  135. string path = QueryString["path"] ?? string.Empty;
  136. if (!string.IsNullOrEmpty(path))
  137. {
  138. return path;
  139. }
  140. string id = QueryString["id"];
  141. string personName = QueryString["personname"];
  142. string imageType = QueryString["type"] ?? string.Empty;
  143. string imageIndex = QueryString["index"];
  144. BaseItem item;
  145. if (!string.IsNullOrEmpty(personName))
  146. {
  147. item = ApiService.GetPersonByName(personName);
  148. }
  149. else
  150. {
  151. item = ApiService.GetItemById(QueryString["id"]);
  152. }
  153. int index = string.IsNullOrEmpty(imageIndex) ? 0 : int.Parse(imageIndex);
  154. return GetImagePathFromTypes(item, imageType, index);
  155. }
  156. private string GetImagePathFromTypes(BaseItem item, string imageType, int imageIndex)
  157. {
  158. if (imageType.Equals("logo", StringComparison.OrdinalIgnoreCase))
  159. {
  160. return item.LogoImagePath;
  161. }
  162. else if (imageType.Equals("backdrop", StringComparison.OrdinalIgnoreCase))
  163. {
  164. return item.BackdropImagePaths.ElementAt(imageIndex);
  165. }
  166. else if (imageType.Equals("banner", StringComparison.OrdinalIgnoreCase))
  167. {
  168. return item.BannerImagePath;
  169. }
  170. else if (imageType.Equals("art", StringComparison.OrdinalIgnoreCase))
  171. {
  172. return item.ArtImagePath;
  173. }
  174. else if (imageType.Equals("thumbnail", StringComparison.OrdinalIgnoreCase))
  175. {
  176. return item.ThumbnailImagePath;
  177. }
  178. else
  179. {
  180. return item.PrimaryImagePath;
  181. }
  182. }
  183. }
  184. }