ImageHandler.cs 7.3 KB

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