ImageHandler.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Common.Net.Handlers;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Drawing;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Model.Entities;
  7. using System;
  8. using System.ComponentModel.Composition;
  9. using System.IO;
  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;
  22. private async Task<string> GetImagePath()
  23. {
  24. _imagePath = _imagePath ?? await DiscoverImagePath();
  25. return _imagePath;
  26. }
  27. private BaseEntity _sourceEntity;
  28. private async Task<BaseEntity> GetSourceEntity()
  29. {
  30. if (_sourceEntity == null)
  31. {
  32. if (!string.IsNullOrEmpty(QueryString["personname"]))
  33. {
  34. _sourceEntity =
  35. await Kernel.Instance.ItemController.GetPerson(QueryString["personname"]).ConfigureAwait(false);
  36. }
  37. else if (!string.IsNullOrEmpty(QueryString["genre"]))
  38. {
  39. _sourceEntity =
  40. await Kernel.Instance.ItemController.GetGenre(QueryString["genre"]).ConfigureAwait(false);
  41. }
  42. else if (!string.IsNullOrEmpty(QueryString["year"]))
  43. {
  44. _sourceEntity =
  45. await
  46. Kernel.Instance.ItemController.GetYear(int.Parse(QueryString["year"])).ConfigureAwait(false);
  47. }
  48. else if (!string.IsNullOrEmpty(QueryString["studio"]))
  49. {
  50. _sourceEntity =
  51. await Kernel.Instance.ItemController.GetStudio(QueryString["studio"]).ConfigureAwait(false);
  52. }
  53. else if (!string.IsNullOrEmpty(QueryString["userid"]))
  54. {
  55. _sourceEntity = ApiService.GetUserById(QueryString["userid"], false);
  56. }
  57. else
  58. {
  59. _sourceEntity = ApiService.GetItemById(QueryString["id"]);
  60. }
  61. }
  62. return _sourceEntity;
  63. }
  64. private async Task<string> DiscoverImagePath()
  65. {
  66. var entity = await GetSourceEntity().ConfigureAwait(false);
  67. return ImageProcessor.GetImagePath(entity, ImageType, ImageIndex);
  68. }
  69. protected async override Task<ResponseInfo> GetResponseInfo()
  70. {
  71. string path = await GetImagePath().ConfigureAwait(false);
  72. ResponseInfo info = new ResponseInfo
  73. {
  74. CacheDuration = TimeSpan.FromDays(365),
  75. ContentType = MimeTypes.GetMimeType(path)
  76. };
  77. DateTime? date = File.GetLastWriteTimeUtc(path);
  78. // If the file does not exist it will return jan 1, 1601
  79. // http://msdn.microsoft.com/en-us/library/system.io.file.getlastwritetimeutc.aspx
  80. if (date.Value.Year == 1601)
  81. {
  82. if (!File.Exists(path))
  83. {
  84. info.StatusCode = 404;
  85. date = null;
  86. }
  87. }
  88. info.DateLastModified = date;
  89. return info;
  90. }
  91. private int ImageIndex
  92. {
  93. get
  94. {
  95. string val = QueryString["index"];
  96. if (string.IsNullOrEmpty(val))
  97. {
  98. return 0;
  99. }
  100. return int.Parse(val);
  101. }
  102. }
  103. private int? Height
  104. {
  105. get
  106. {
  107. string val = QueryString["height"];
  108. if (string.IsNullOrEmpty(val))
  109. {
  110. return null;
  111. }
  112. return int.Parse(val);
  113. }
  114. }
  115. private int? Width
  116. {
  117. get
  118. {
  119. string val = QueryString["width"];
  120. if (string.IsNullOrEmpty(val))
  121. {
  122. return null;
  123. }
  124. return int.Parse(val);
  125. }
  126. }
  127. private int? MaxHeight
  128. {
  129. get
  130. {
  131. string val = QueryString["maxheight"];
  132. if (string.IsNullOrEmpty(val))
  133. {
  134. return null;
  135. }
  136. return int.Parse(val);
  137. }
  138. }
  139. private int? MaxWidth
  140. {
  141. get
  142. {
  143. string val = QueryString["maxwidth"];
  144. if (string.IsNullOrEmpty(val))
  145. {
  146. return null;
  147. }
  148. return int.Parse(val);
  149. }
  150. }
  151. private int? Quality
  152. {
  153. get
  154. {
  155. string val = QueryString["quality"];
  156. if (string.IsNullOrEmpty(val))
  157. {
  158. return null;
  159. }
  160. return int.Parse(val);
  161. }
  162. }
  163. private ImageType ImageType
  164. {
  165. get
  166. {
  167. string imageType = QueryString["type"];
  168. if (string.IsNullOrEmpty(imageType))
  169. {
  170. return ImageType.Primary;
  171. }
  172. return (ImageType)Enum.Parse(typeof(ImageType), imageType, true);
  173. }
  174. }
  175. protected override async Task WriteResponseToOutputStream(Stream stream)
  176. {
  177. var entity = await GetSourceEntity().ConfigureAwait(false);
  178. ImageProcessor.ProcessImage(entity, ImageType, ImageIndex, stream, Width, Height, MaxWidth, MaxHeight, Quality);
  179. }
  180. }
  181. }