ImageHandler.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. protected override DateTime? GetLastDateModified()
  43. {
  44. try
  45. {
  46. return File.GetLastWriteTime(ImagePath);
  47. }
  48. catch
  49. {
  50. return base.GetLastDateModified();
  51. }
  52. }
  53. private int? Height
  54. {
  55. get
  56. {
  57. string val = QueryString["height"];
  58. if (string.IsNullOrEmpty(val))
  59. {
  60. return null;
  61. }
  62. return int.Parse(val);
  63. }
  64. }
  65. private int? Width
  66. {
  67. get
  68. {
  69. string val = QueryString["width"];
  70. if (string.IsNullOrEmpty(val))
  71. {
  72. return null;
  73. }
  74. return int.Parse(val);
  75. }
  76. }
  77. private int? MaxHeight
  78. {
  79. get
  80. {
  81. string val = QueryString["maxheight"];
  82. if (string.IsNullOrEmpty(val))
  83. {
  84. return null;
  85. }
  86. return int.Parse(val);
  87. }
  88. }
  89. private int? MaxWidth
  90. {
  91. get
  92. {
  93. string val = QueryString["maxwidth"];
  94. if (string.IsNullOrEmpty(val))
  95. {
  96. return null;
  97. }
  98. return int.Parse(val);
  99. }
  100. }
  101. private int? Quality
  102. {
  103. get
  104. {
  105. string val = QueryString["quality"];
  106. if (string.IsNullOrEmpty(val))
  107. {
  108. return null;
  109. }
  110. return int.Parse(val);
  111. }
  112. }
  113. private ImageType ImageType
  114. {
  115. get
  116. {
  117. string imageType = QueryString["type"];
  118. if (string.IsNullOrEmpty(imageType))
  119. {
  120. return Model.Entities.ImageType.Primary;
  121. }
  122. return (ImageType)Enum.Parse(typeof(ImageType), imageType, true);
  123. }
  124. }
  125. protected override void WriteResponseToOutputStream(Stream stream)
  126. {
  127. ImageProcessor.ProcessImage(ImagePath, stream, Width, Height, MaxWidth, MaxHeight, Quality);
  128. }
  129. private string GetImagePath()
  130. {
  131. string path = QueryString["path"] ?? string.Empty;
  132. if (!string.IsNullOrEmpty(path))
  133. {
  134. return path;
  135. }
  136. string id = QueryString["id"];
  137. string personName = QueryString["personname"];
  138. string imageIndex = QueryString["index"];
  139. BaseItem item;
  140. if (!string.IsNullOrEmpty(personName))
  141. {
  142. item = Kernel.Instance.ItemController.GetPerson(personName);
  143. }
  144. else
  145. {
  146. item = ApiService.GetItemById(QueryString["id"]);
  147. }
  148. int index = string.IsNullOrEmpty(imageIndex) ? 0 : int.Parse(imageIndex);
  149. return GetImagePathFromTypes(item, ImageType, index);
  150. }
  151. private string GetImagePathFromTypes(BaseItem item, ImageType imageType, int imageIndex)
  152. {
  153. if (imageType == ImageType.Logo)
  154. {
  155. return item.LogoImagePath;
  156. }
  157. else if (imageType == ImageType.Backdrop)
  158. {
  159. return item.BackdropImagePaths.ElementAt(imageIndex);
  160. }
  161. else if (imageType == ImageType.Banner)
  162. {
  163. return item.BannerImagePath;
  164. }
  165. else if (imageType == ImageType.Art)
  166. {
  167. return item.ArtImagePath;
  168. }
  169. else if (imageType == ImageType.Thumbnail)
  170. {
  171. return item.ThumbnailImagePath;
  172. }
  173. else
  174. {
  175. return item.PrimaryImagePath;
  176. }
  177. }
  178. }
  179. }