ImageHandler.cs 7.6 KB

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