ImageHandler.cs 7.1 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 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. BaseItem item = ApiService.GetItemById(QueryString["id"]);
  46. string imageIndex = QueryString["index"];
  47. int index = string.IsNullOrEmpty(imageIndex) ? 0 : int.Parse(imageIndex);
  48. return GetImagePathFromTypes(item, ImageType, index);
  49. }
  50. private Stream _SourceStream = null;
  51. private async Task<Stream> GetSourceStream()
  52. {
  53. await EnsureSourceStream().ConfigureAwait(false);
  54. return _SourceStream;
  55. }
  56. private bool _SourceStreamEnsured = false;
  57. private async Task EnsureSourceStream()
  58. {
  59. if (!_SourceStreamEnsured)
  60. {
  61. try
  62. {
  63. _SourceStream = File.OpenRead(await GetImagePath().ConfigureAwait(false));
  64. }
  65. catch (FileNotFoundException ex)
  66. {
  67. StatusCode = 404;
  68. Logger.LogException(ex);
  69. }
  70. catch (DirectoryNotFoundException ex)
  71. {
  72. StatusCode = 404;
  73. Logger.LogException(ex);
  74. }
  75. catch (UnauthorizedAccessException ex)
  76. {
  77. StatusCode = 403;
  78. Logger.LogException(ex);
  79. }
  80. finally
  81. {
  82. _SourceStreamEnsured = true;
  83. }
  84. }
  85. }
  86. public async override Task<string> GetContentType()
  87. {
  88. await EnsureSourceStream().ConfigureAwait(false);
  89. if (await GetSourceStream().ConfigureAwait(false) == null)
  90. {
  91. return null;
  92. }
  93. return MimeTypes.GetMimeType(await GetImagePath().ConfigureAwait(false));
  94. }
  95. public override TimeSpan CacheDuration
  96. {
  97. get
  98. {
  99. return TimeSpan.FromDays(365);
  100. }
  101. }
  102. protected async override Task<DateTime?> GetLastDateModified()
  103. {
  104. await EnsureSourceStream().ConfigureAwait(false);
  105. if (await GetSourceStream().ConfigureAwait(false) == null)
  106. {
  107. return null;
  108. }
  109. return File.GetLastWriteTime(await GetImagePath().ConfigureAwait(false));
  110. }
  111. private int? Height
  112. {
  113. get
  114. {
  115. string val = QueryString["height"];
  116. if (string.IsNullOrEmpty(val))
  117. {
  118. return null;
  119. }
  120. return int.Parse(val);
  121. }
  122. }
  123. private int? Width
  124. {
  125. get
  126. {
  127. string val = QueryString["width"];
  128. if (string.IsNullOrEmpty(val))
  129. {
  130. return null;
  131. }
  132. return int.Parse(val);
  133. }
  134. }
  135. private int? MaxHeight
  136. {
  137. get
  138. {
  139. string val = QueryString["maxheight"];
  140. if (string.IsNullOrEmpty(val))
  141. {
  142. return null;
  143. }
  144. return int.Parse(val);
  145. }
  146. }
  147. private int? MaxWidth
  148. {
  149. get
  150. {
  151. string val = QueryString["maxwidth"];
  152. if (string.IsNullOrEmpty(val))
  153. {
  154. return null;
  155. }
  156. return int.Parse(val);
  157. }
  158. }
  159. private int? Quality
  160. {
  161. get
  162. {
  163. string val = QueryString["quality"];
  164. if (string.IsNullOrEmpty(val))
  165. {
  166. return null;
  167. }
  168. return int.Parse(val);
  169. }
  170. }
  171. private ImageType ImageType
  172. {
  173. get
  174. {
  175. string imageType = QueryString["type"];
  176. if (string.IsNullOrEmpty(imageType))
  177. {
  178. return ImageType.Primary;
  179. }
  180. return (ImageType)Enum.Parse(typeof(ImageType), imageType, true);
  181. }
  182. }
  183. protected override async Task WriteResponseToOutputStream(Stream stream)
  184. {
  185. ImageProcessor.ProcessImage(await GetSourceStream().ConfigureAwait(false), stream, Width, Height, MaxWidth, MaxHeight, Quality);
  186. }
  187. private string GetImagePathFromTypes(BaseItem item, ImageType imageType, int imageIndex)
  188. {
  189. if (imageType == ImageType.Logo)
  190. {
  191. return item.LogoImagePath;
  192. }
  193. else if (imageType == ImageType.Backdrop)
  194. {
  195. return item.BackdropImagePaths.ElementAt(imageIndex);
  196. }
  197. else if (imageType == ImageType.Banner)
  198. {
  199. return item.BannerImagePath;
  200. }
  201. else if (imageType == ImageType.Art)
  202. {
  203. return item.ArtImagePath;
  204. }
  205. else if (imageType == ImageType.Thumbnail)
  206. {
  207. return item.ThumbnailImagePath;
  208. }
  209. else
  210. {
  211. return item.PrimaryImagePath;
  212. }
  213. }
  214. }
  215. }