ImageHandler.cs 5.3 KB

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