ImageHandler.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Common.Logging;
  6. using MediaBrowser.Common.Net;
  7. using MediaBrowser.Common.Net.Handlers;
  8. using MediaBrowser.Controller;
  9. using MediaBrowser.Model.Entities;
  10. namespace MediaBrowser.Api.HttpHandlers
  11. {
  12. public class ImageHandler : BaseHandler
  13. {
  14. private string _ImagePath = null;
  15. private string ImagePath
  16. {
  17. get
  18. {
  19. if (_ImagePath == null)
  20. {
  21. _ImagePath = GetImagePath();
  22. }
  23. return _ImagePath;
  24. }
  25. }
  26. private Stream _SourceStream = null;
  27. private Stream SourceStream
  28. {
  29. get
  30. {
  31. EnsureSourceStream();
  32. return _SourceStream;
  33. }
  34. }
  35. private bool _SourceStreamEnsured = false;
  36. private void EnsureSourceStream()
  37. {
  38. if (!_SourceStreamEnsured)
  39. {
  40. try
  41. {
  42. _SourceStream = File.OpenRead(ImagePath);
  43. }
  44. catch (FileNotFoundException ex)
  45. {
  46. StatusCode = 404;
  47. Logger.LogException(ex);
  48. }
  49. catch (DirectoryNotFoundException ex)
  50. {
  51. StatusCode = 404;
  52. Logger.LogException(ex);
  53. }
  54. catch (UnauthorizedAccessException ex)
  55. {
  56. StatusCode = 403;
  57. Logger.LogException(ex);
  58. }
  59. finally
  60. {
  61. _SourceStreamEnsured = true;
  62. }
  63. }
  64. }
  65. public override string ContentType
  66. {
  67. get
  68. {
  69. EnsureSourceStream();
  70. if (SourceStream == null)
  71. {
  72. return null;
  73. }
  74. return MimeTypes.GetMimeType(ImagePath);
  75. }
  76. }
  77. public override TimeSpan CacheDuration
  78. {
  79. get
  80. {
  81. return TimeSpan.FromDays(365);
  82. }
  83. }
  84. protected override DateTime? GetLastDateModified()
  85. {
  86. EnsureSourceStream();
  87. if (SourceStream == null)
  88. {
  89. return null;
  90. }
  91. return File.GetLastWriteTime(ImagePath);
  92. }
  93. private int? Height
  94. {
  95. get
  96. {
  97. string val = QueryString["height"];
  98. if (string.IsNullOrEmpty(val))
  99. {
  100. return null;
  101. }
  102. return int.Parse(val);
  103. }
  104. }
  105. private int? Width
  106. {
  107. get
  108. {
  109. string val = QueryString["width"];
  110. if (string.IsNullOrEmpty(val))
  111. {
  112. return null;
  113. }
  114. return int.Parse(val);
  115. }
  116. }
  117. private int? MaxHeight
  118. {
  119. get
  120. {
  121. string val = QueryString["maxheight"];
  122. if (string.IsNullOrEmpty(val))
  123. {
  124. return null;
  125. }
  126. return int.Parse(val);
  127. }
  128. }
  129. private int? MaxWidth
  130. {
  131. get
  132. {
  133. string val = QueryString["maxwidth"];
  134. if (string.IsNullOrEmpty(val))
  135. {
  136. return null;
  137. }
  138. return int.Parse(val);
  139. }
  140. }
  141. private int? Quality
  142. {
  143. get
  144. {
  145. string val = QueryString["quality"];
  146. if (string.IsNullOrEmpty(val))
  147. {
  148. return null;
  149. }
  150. return int.Parse(val);
  151. }
  152. }
  153. private ImageType ImageType
  154. {
  155. get
  156. {
  157. string imageType = QueryString["type"];
  158. if (string.IsNullOrEmpty(imageType))
  159. {
  160. return ImageType.Primary;
  161. }
  162. return (ImageType)Enum.Parse(typeof(ImageType), imageType, true);
  163. }
  164. }
  165. protected override Task WriteResponseToOutputStream(Stream stream)
  166. {
  167. return Task.Run(() =>
  168. {
  169. ImageProcessor.ProcessImage(SourceStream, stream, Width, Height, MaxWidth, MaxHeight, Quality);
  170. });
  171. }
  172. private string GetImagePath()
  173. {
  174. string path = QueryString["path"] ?? string.Empty;
  175. if (!string.IsNullOrEmpty(path))
  176. {
  177. return path;
  178. }
  179. string personName = QueryString["personname"];
  180. if (!string.IsNullOrEmpty(personName))
  181. {
  182. return Kernel.Instance.ItemController.GetPerson(personName).PrimaryImagePath;
  183. }
  184. BaseItem item = ApiService.GetItemById(QueryString["id"]);
  185. string imageIndex = QueryString["index"];
  186. int index = string.IsNullOrEmpty(imageIndex) ? 0 : int.Parse(imageIndex);
  187. return GetImagePathFromTypes(item, ImageType, index);
  188. }
  189. private string GetImagePathFromTypes(BaseItem item, ImageType imageType, int imageIndex)
  190. {
  191. if (imageType == ImageType.Logo)
  192. {
  193. return item.LogoImagePath;
  194. }
  195. else if (imageType == ImageType.Backdrop)
  196. {
  197. return item.BackdropImagePaths.ElementAt(imageIndex);
  198. }
  199. else if (imageType == ImageType.Banner)
  200. {
  201. return item.BannerImagePath;
  202. }
  203. else if (imageType == ImageType.Art)
  204. {
  205. return item.ArtImagePath;
  206. }
  207. else if (imageType == ImageType.Thumbnail)
  208. {
  209. return item.ThumbnailImagePath;
  210. }
  211. else
  212. {
  213. return item.PrimaryImagePath;
  214. }
  215. }
  216. }
  217. }