ImageHandler.cs 7.4 KB

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