ImageHandler.cs 5.4 KB

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