LiveTvImageService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using MediaBrowser.Api.Images;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Controller.Drawing;
  4. using MediaBrowser.Controller.Dto;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.LiveTv;
  7. using MediaBrowser.Controller.Persistence;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.Entities;
  11. using ServiceStack;
  12. using ServiceStack.Text.Controller;
  13. using ServiceStack.Web;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. namespace MediaBrowser.Api.LiveTv
  20. {
  21. [Route("/LiveTv/Channels/{Id}/Images/{Type}", "POST")]
  22. [Route("/LiveTv/Channels/{Id}/Images/{Type}/{Index}", "POST")]
  23. [Api(Description = "Posts an item image")]
  24. public class PostChannelImage : DeleteImageRequest, IRequiresRequestStream, IReturnVoid
  25. {
  26. /// <summary>
  27. /// Gets or sets the id.
  28. /// </summary>
  29. /// <value>The id.</value>
  30. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  31. public string Id { get; set; }
  32. /// <summary>
  33. /// The raw Http Request Input Stream
  34. /// </summary>
  35. /// <value>The request stream.</value>
  36. public Stream RequestStream { get; set; }
  37. }
  38. [Route("/LiveTv/Channels/{Id}/Images/{Type}", "DELETE")]
  39. [Route("/LiveTv/Channels/{Id}/Images/{Type}/{Index}", "DELETE")]
  40. [Api(Description = "Deletes an item image")]
  41. public class DeleteChannelImage : DeleteImageRequest, IReturnVoid
  42. {
  43. /// <summary>
  44. /// Gets or sets the id.
  45. /// </summary>
  46. /// <value>The id.</value>
  47. [ApiMember(Name = "Id", Description = "Channel Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  48. public string Id { get; set; }
  49. }
  50. [Route("/LiveTv/Channels/{Id}/Images/{Type}", "GET")]
  51. [Route("/LiveTv/Channels/{Id}/Images/{Type}/{Index}", "GET")]
  52. [Api(Description = "Gets an item image")]
  53. public class GetChannelImage : ImageRequest
  54. {
  55. /// <summary>
  56. /// Gets or sets the id.
  57. /// </summary>
  58. /// <value>The id.</value>
  59. [ApiMember(Name = "Id", Description = "Channel Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  60. public string Id { get; set; }
  61. }
  62. [Route("/LiveTv/Recordings/{Id}/Images/{Type}", "GET")]
  63. [Route("/LiveTv/Recordings/{Id}/Images/{Type}/{Index}", "GET")]
  64. [Api(Description = "Gets an item image")]
  65. public class GetRecordingImage : ImageRequest
  66. {
  67. /// <summary>
  68. /// Gets or sets the id.
  69. /// </summary>
  70. /// <value>The id.</value>
  71. [ApiMember(Name = "Id", Description = "Recording Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  72. public string Id { get; set; }
  73. }
  74. [Route("/LiveTv/Programs/{Id}/Images/{Type}", "GET")]
  75. [Route("/LiveTv/Programs/{Id}/Images/{Type}/{Index}", "GET")]
  76. [Api(Description = "Gets an item image")]
  77. public class GetProgramImage : ImageRequest
  78. {
  79. /// <summary>
  80. /// Gets or sets the id.
  81. /// </summary>
  82. /// <value>The id.</value>
  83. [ApiMember(Name = "Id", Description = "Program Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  84. public string Id { get; set; }
  85. }
  86. [Route("/LiveTv/Channels/{Id}/Images", "GET")]
  87. [Api(Description = "Gets information about an item's images")]
  88. public class GetChannelImageInfos : IReturn<List<ImageInfo>>
  89. {
  90. /// <summary>
  91. /// Gets or sets the id.
  92. /// </summary>
  93. /// <value>The id.</value>
  94. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  95. public string Id { get; set; }
  96. }
  97. public class LiveTvImageService : BaseApiService
  98. {
  99. private readonly ILiveTvManager _liveTv;
  100. private readonly IUserManager _userManager;
  101. private readonly ILibraryManager _libraryManager;
  102. private readonly IApplicationPaths _appPaths;
  103. private readonly IProviderManager _providerManager;
  104. private readonly IItemRepository _itemRepo;
  105. private readonly IDtoService _dtoService;
  106. private readonly IImageProcessor _imageProcessor;
  107. public LiveTvImageService(ILiveTvManager liveTv, IUserManager userManager, ILibraryManager libraryManager, IApplicationPaths appPaths, IProviderManager providerManager, IItemRepository itemRepo, IDtoService dtoService, IImageProcessor imageProcessor)
  108. {
  109. _liveTv = liveTv;
  110. _userManager = userManager;
  111. _libraryManager = libraryManager;
  112. _appPaths = appPaths;
  113. _providerManager = providerManager;
  114. _itemRepo = itemRepo;
  115. _dtoService = dtoService;
  116. _imageProcessor = imageProcessor;
  117. }
  118. public object Get(GetChannelImageInfos request)
  119. {
  120. var item = _liveTv.GetInternalChannel(request.Id);
  121. var result = GetImageService().GetItemImageInfos(item);
  122. return ToOptimizedResult(result);
  123. }
  124. public object Get(GetChannelImage request)
  125. {
  126. var item = _liveTv.GetInternalChannel(request.Id);
  127. return GetImageService().GetImage(request, item);
  128. }
  129. public object Get(GetRecordingImage request)
  130. {
  131. var item = _liveTv.GetInternalRecording(request.Id, CancellationToken.None).Result;
  132. return GetImageService().GetImage(request, item);
  133. }
  134. public object Get(GetProgramImage request)
  135. {
  136. var item = _liveTv.GetInternalProgram(request.Id);
  137. return GetImageService().GetImage(request, item);
  138. }
  139. public void Post(PostChannelImage request)
  140. {
  141. var pathInfo = PathInfo.Parse(Request.PathInfo);
  142. var id = pathInfo.GetArgumentValue<string>(2);
  143. request.Type = (ImageType)Enum.Parse(typeof(ImageType), pathInfo.GetArgumentValue<string>(4), true);
  144. var item = _liveTv.GetInternalChannel(id);
  145. var task = GetImageService().PostImage(item, request.RequestStream, request.Type, Request.ContentType);
  146. Task.WaitAll(task);
  147. }
  148. public void Delete(DeleteChannelImage request)
  149. {
  150. var item = _liveTv.GetInternalChannel(request.Id);
  151. var task = item.DeleteImage(request.Type, request.Index);
  152. Task.WaitAll(task);
  153. }
  154. private ImageService GetImageService()
  155. {
  156. return new ImageService(_userManager, _libraryManager, _appPaths, _providerManager, _itemRepo, _dtoService,
  157. _imageProcessor)
  158. {
  159. ResultFactory = ResultFactory,
  160. Request = Request
  161. };
  162. }
  163. }
  164. }