DlnaServerController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.IO;
  5. using System.Net.Mime;
  6. using System.Threading.Tasks;
  7. using Emby.Dlna;
  8. using Emby.Dlna.Main;
  9. using Jellyfin.Api.Attributes;
  10. using MediaBrowser.Controller.Dlna;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Mvc;
  13. namespace Jellyfin.Api.Controllers
  14. {
  15. /// <summary>
  16. /// Dlna Server Controller.
  17. /// </summary>
  18. [Route("Dlna")]
  19. public class DlnaServerController : BaseJellyfinApiController
  20. {
  21. private readonly IDlnaManager _dlnaManager;
  22. private readonly IContentDirectory _contentDirectory;
  23. private readonly IConnectionManager _connectionManager;
  24. private readonly IMediaReceiverRegistrar _mediaReceiverRegistrar;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="DlnaServerController"/> class.
  27. /// </summary>
  28. /// <param name="dlnaManager">Instance of the <see cref="IDlnaManager"/> interface.</param>
  29. public DlnaServerController(IDlnaManager dlnaManager)
  30. {
  31. _dlnaManager = dlnaManager;
  32. _contentDirectory = DlnaEntryPoint.Current.ContentDirectory;
  33. _connectionManager = DlnaEntryPoint.Current.ConnectionManager;
  34. _mediaReceiverRegistrar = DlnaEntryPoint.Current.MediaReceiverRegistrar;
  35. }
  36. /// <summary>
  37. /// Get Description Xml.
  38. /// </summary>
  39. /// <param name="serverId">Server UUID.</param>
  40. /// <response code="200">Description xml returned.</response>
  41. /// <response code="503">DLNA is disabled.</response>
  42. /// <returns>An <see cref="OkResult"/> containing the description xml.</returns>
  43. [HttpGet("{serverId}/description")]
  44. [HttpGet("{serverId}/description.xml", Name = "GetDescriptionXml_2")]
  45. [ProducesResponseType(StatusCodes.Status200OK)]
  46. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  47. [Produces(MediaTypeNames.Text.Xml)]
  48. [ProducesFile(MediaTypeNames.Text.Xml)]
  49. public ActionResult GetDescriptionXml([FromRoute, Required] string serverId)
  50. {
  51. if (DlnaEntryPoint.Enabled)
  52. {
  53. var url = GetAbsoluteUri();
  54. var serverAddress = url.Substring(0, url.IndexOf("/dlna/", StringComparison.OrdinalIgnoreCase));
  55. var xml = _dlnaManager.GetServerDescriptionXml(Request.Headers, serverId, serverAddress);
  56. return Ok(xml);
  57. }
  58. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  59. }
  60. /// <summary>
  61. /// Gets Dlna content directory xml.
  62. /// </summary>
  63. /// <param name="serverId">Server UUID.</param>
  64. /// <response code="200">Dlna content directory returned.</response>
  65. /// <response code="503">DLNA is disabled.</response>
  66. /// <returns>An <see cref="OkResult"/> containing the dlna content directory xml.</returns>
  67. [HttpGet("{serverId}/ContentDirectory")]
  68. [HttpGet("{serverId}/ContentDirectory/ContentDirectory", Name = "GetContentDirectory_2")]
  69. [HttpGet("{serverId}/ContentDirectory/ContentDirectory.xml", Name = "GetContentDirectory_3")]
  70. [ProducesResponseType(StatusCodes.Status200OK)]
  71. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  72. [Produces(MediaTypeNames.Text.Xml)]
  73. [ProducesFile(MediaTypeNames.Text.Xml)]
  74. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  75. public ActionResult GetContentDirectory([FromRoute, Required] string serverId)
  76. {
  77. if (DlnaEntryPoint.Enabled)
  78. {
  79. return Ok(_contentDirectory.GetServiceXml());
  80. }
  81. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  82. }
  83. /// <summary>
  84. /// Gets Dlna media receiver registrar xml.
  85. /// </summary>
  86. /// <param name="serverId">Server UUID.</param>
  87. /// <response code="200">Dlna media receiver registrar xml returned.</response>
  88. /// <response code="503">DLNA is disabled.</response>
  89. /// <returns>Dlna media receiver registrar xml.</returns>
  90. [HttpGet("{serverId}/MediaReceiverRegistrar")]
  91. [HttpGet("{serverId}/MediaReceiverRegistrar/MediaReceiverRegistrar", Name = "GetMediaReceiverRegistrar_2")]
  92. [HttpGet("{serverId}/MediaReceiverRegistrar/MediaReceiverRegistrar.xml", Name = "GetMediaReceiverRegistrar_3")]
  93. [ProducesResponseType(StatusCodes.Status200OK)]
  94. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  95. [Produces(MediaTypeNames.Text.Xml)]
  96. [ProducesFile(MediaTypeNames.Text.Xml)]
  97. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  98. public ActionResult GetMediaReceiverRegistrar([FromRoute, Required] string serverId)
  99. {
  100. if (DlnaEntryPoint.Enabled)
  101. {
  102. return Ok(_mediaReceiverRegistrar.GetServiceXml());
  103. }
  104. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  105. }
  106. /// <summary>
  107. /// Gets Dlna media receiver registrar xml.
  108. /// </summary>
  109. /// <param name="serverId">Server UUID.</param>
  110. /// <response code="200">Dlna media receiver registrar xml returned.</response>
  111. /// <response code="503">DLNA is disabled.</response>
  112. /// <returns>Dlna media receiver registrar xml.</returns>
  113. [HttpGet("{serverId}/ConnectionManager")]
  114. [HttpGet("{serverId}/ConnectionManager/ConnectionManager", Name = "GetConnectionManager_2")]
  115. [HttpGet("{serverId}/ConnectionManager/ConnectionManager.xml", Name = "GetConnectionManager_3")]
  116. [ProducesResponseType(StatusCodes.Status200OK)]
  117. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  118. [Produces(MediaTypeNames.Text.Xml)]
  119. [ProducesFile(MediaTypeNames.Text.Xml)]
  120. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  121. public ActionResult GetConnectionManager([FromRoute, Required] string serverId)
  122. {
  123. if (DlnaEntryPoint.Enabled)
  124. {
  125. return Ok(_connectionManager.GetServiceXml());
  126. }
  127. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  128. }
  129. /// <summary>
  130. /// Process a content directory control request.
  131. /// </summary>
  132. /// <param name="serverId">Server UUID.</param>
  133. /// <response code="200">Request processed.</response>
  134. /// <response code="503">DLNA is disabled.</response>
  135. /// <returns>Control response.</returns>
  136. [HttpPost("{serverId}/ContentDirectory/Control")]
  137. [ProducesResponseType(StatusCodes.Status200OK)]
  138. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  139. [Produces(MediaTypeNames.Text.Xml)]
  140. [ProducesFile(MediaTypeNames.Text.Xml)]
  141. public async Task<ActionResult<ControlResponse>> ProcessContentDirectoryControlRequest([FromRoute, Required] string serverId)
  142. {
  143. if (DlnaEntryPoint.Enabled)
  144. {
  145. return await ProcessControlRequestInternalAsync(serverId, Request.Body, _contentDirectory).ConfigureAwait(false);
  146. }
  147. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  148. }
  149. /// <summary>
  150. /// Process a connection manager control request.
  151. /// </summary>
  152. /// <param name="serverId">Server UUID.</param>
  153. /// <response code="200">Request processed.</response>
  154. /// <response code="503">DLNA is disabled.</response>
  155. /// <returns>Control response.</returns>
  156. [HttpPost("{serverId}/ConnectionManager/Control")]
  157. [ProducesResponseType(StatusCodes.Status200OK)]
  158. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  159. [Produces(MediaTypeNames.Text.Xml)]
  160. [ProducesFile(MediaTypeNames.Text.Xml)]
  161. public async Task<ActionResult<ControlResponse>> ProcessConnectionManagerControlRequest([FromRoute, Required] string serverId)
  162. {
  163. if (DlnaEntryPoint.Enabled)
  164. {
  165. return await ProcessControlRequestInternalAsync(serverId, Request.Body, _connectionManager).ConfigureAwait(false);
  166. }
  167. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  168. }
  169. /// <summary>
  170. /// Process a media receiver registrar control request.
  171. /// </summary>
  172. /// <param name="serverId">Server UUID.</param>
  173. /// <response code="200">Request processed.</response>
  174. /// <response code="503">DLNA is disabled.</response>
  175. /// <returns>Control response.</returns>
  176. [HttpPost("{serverId}/MediaReceiverRegistrar/Control")]
  177. [ProducesResponseType(StatusCodes.Status200OK)]
  178. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  179. [Produces(MediaTypeNames.Text.Xml)]
  180. [ProducesFile(MediaTypeNames.Text.Xml)]
  181. public async Task<ActionResult<ControlResponse>> ProcessMediaReceiverRegistrarControlRequest([FromRoute, Required] string serverId)
  182. {
  183. if (DlnaEntryPoint.Enabled)
  184. {
  185. return await ProcessControlRequestInternalAsync(serverId, Request.Body, _mediaReceiverRegistrar).ConfigureAwait(false);
  186. }
  187. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  188. }
  189. /// <summary>
  190. /// Processes an event subscription request.
  191. /// </summary>
  192. /// <param name="serverId">Server UUID.</param>
  193. /// <response code="200">Request processed.</response>
  194. /// <response code="503">DLNA is disabled.</response>
  195. /// <returns>Event subscription response.</returns>
  196. [HttpSubscribe("{serverId}/MediaReceiverRegistrar/Events")]
  197. [HttpUnsubscribe("{serverId}/MediaReceiverRegistrar/Events")]
  198. [ApiExplorerSettings(IgnoreApi = true)] // Ignore in openapi docs
  199. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  200. [ProducesResponseType(StatusCodes.Status200OK)]
  201. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  202. [Produces(MediaTypeNames.Text.Xml)]
  203. [ProducesFile(MediaTypeNames.Text.Xml)]
  204. public ActionResult<EventSubscriptionResponse> ProcessMediaReceiverRegistrarEventRequest(string serverId)
  205. {
  206. if (DlnaEntryPoint.Enabled)
  207. {
  208. return ProcessEventRequest(_mediaReceiverRegistrar);
  209. }
  210. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  211. }
  212. /// <summary>
  213. /// Processes an event subscription request.
  214. /// </summary>
  215. /// <param name="serverId">Server UUID.</param>
  216. /// <response code="200">Request processed.</response>
  217. /// <response code="503">DLNA is disabled.</response>
  218. /// <returns>Event subscription response.</returns>
  219. [HttpSubscribe("{serverId}/ContentDirectory/Events")]
  220. [HttpUnsubscribe("{serverId}/ContentDirectory/Events")]
  221. [ApiExplorerSettings(IgnoreApi = true)] // Ignore in openapi docs
  222. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  223. [ProducesResponseType(StatusCodes.Status200OK)]
  224. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  225. [Produces(MediaTypeNames.Text.Xml)]
  226. [ProducesFile(MediaTypeNames.Text.Xml)]
  227. public ActionResult<EventSubscriptionResponse> ProcessContentDirectoryEventRequest(string serverId)
  228. {
  229. if (DlnaEntryPoint.Enabled)
  230. {
  231. return ProcessEventRequest(_contentDirectory);
  232. }
  233. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  234. }
  235. /// <summary>
  236. /// Processes an event subscription request.
  237. /// </summary>
  238. /// <param name="serverId">Server UUID.</param>
  239. /// <response code="200">Request processed.</response>
  240. /// <response code="503">DLNA is disabled.</response>
  241. /// <returns>Event subscription response.</returns>
  242. [HttpSubscribe("{serverId}/ConnectionManager/Events")]
  243. [HttpUnsubscribe("{serverId}/ConnectionManager/Events")]
  244. [ApiExplorerSettings(IgnoreApi = true)] // Ignore in openapi docs
  245. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  246. [ProducesResponseType(StatusCodes.Status200OK)]
  247. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  248. [Produces(MediaTypeNames.Text.Xml)]
  249. [ProducesFile(MediaTypeNames.Text.Xml)]
  250. public ActionResult<EventSubscriptionResponse> ProcessConnectionManagerEventRequest(string serverId)
  251. {
  252. if (DlnaEntryPoint.Enabled)
  253. {
  254. return ProcessEventRequest(_connectionManager);
  255. }
  256. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  257. }
  258. /// <summary>
  259. /// Gets a server icon.
  260. /// </summary>
  261. /// <param name="serverId">Server UUID.</param>
  262. /// <param name="fileName">The icon filename.</param>
  263. /// <response code="200">Request processed.</response>
  264. /// <response code="404">Not Found.</response>
  265. /// <response code="503">DLNA is disabled.</response>
  266. /// <returns>Icon stream.</returns>
  267. [HttpGet("{serverId}/icons/{fileName}")]
  268. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "serverId", Justification = "Required for DLNA")]
  269. [ProducesResponseType(StatusCodes.Status200OK)]
  270. [ProducesResponseType(StatusCodes.Status404NotFound)]
  271. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  272. [ProducesImageFile]
  273. public ActionResult GetIconId([FromRoute, Required] string serverId, [FromRoute, Required] string fileName)
  274. {
  275. if (DlnaEntryPoint.Enabled)
  276. {
  277. return GetIconInternal(fileName);
  278. }
  279. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  280. }
  281. /// <summary>
  282. /// Gets a server icon.
  283. /// </summary>
  284. /// <param name="fileName">The icon filename.</param>
  285. /// <returns>Icon stream.</returns>
  286. /// <response code="200">Request processed.</response>
  287. /// <response code="404">Not Found.</response>
  288. /// <response code="503">DLNA is disabled.</response>
  289. [HttpGet("icons/{fileName}")]
  290. [ProducesResponseType(StatusCodes.Status200OK)]
  291. [ProducesResponseType(StatusCodes.Status404NotFound)]
  292. [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
  293. [ProducesImageFile]
  294. public ActionResult GetIcon([FromRoute, Required] string fileName)
  295. {
  296. if (DlnaEntryPoint.Enabled)
  297. {
  298. return GetIconInternal(fileName);
  299. }
  300. return StatusCode(StatusCodes.Status503ServiceUnavailable);
  301. }
  302. private ActionResult GetIconInternal(string fileName)
  303. {
  304. var icon = _dlnaManager.GetIcon(fileName);
  305. if (icon == null)
  306. {
  307. return NotFound();
  308. }
  309. var contentType = "image/" + Path.GetExtension(fileName)
  310. .TrimStart('.')
  311. .ToLowerInvariant();
  312. return File(icon.Stream, contentType);
  313. }
  314. private string GetAbsoluteUri()
  315. {
  316. return $"{Request.Scheme}://{Request.Host}{Request.PathBase}{Request.Path}";
  317. }
  318. private Task<ControlResponse> ProcessControlRequestInternalAsync(string id, Stream requestStream, IUpnpService service)
  319. {
  320. return service.ProcessControlRequestAsync(new ControlRequest(Request.Headers)
  321. {
  322. InputXml = requestStream,
  323. TargetServerUuId = id,
  324. RequestedUrl = GetAbsoluteUri()
  325. });
  326. }
  327. private EventSubscriptionResponse ProcessEventRequest(IDlnaEventManager dlnaEventManager)
  328. {
  329. var subscriptionId = Request.Headers["SID"];
  330. if (string.Equals(Request.Method, "subscribe", StringComparison.OrdinalIgnoreCase))
  331. {
  332. var notificationType = Request.Headers["NT"];
  333. var callback = Request.Headers["CALLBACK"];
  334. var timeoutString = Request.Headers["TIMEOUT"];
  335. if (string.IsNullOrEmpty(notificationType))
  336. {
  337. return dlnaEventManager.RenewEventSubscription(
  338. subscriptionId,
  339. notificationType,
  340. timeoutString,
  341. callback);
  342. }
  343. return dlnaEventManager.CreateEventSubscription(notificationType, timeoutString, callback);
  344. }
  345. return dlnaEventManager.CancelEventSubscription(subscriptionId);
  346. }
  347. }
  348. }