ImageHandler.cs 6.4 KB

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