DevicesController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using Jellyfin.Api.Constants;
  5. using MediaBrowser.Controller.Devices;
  6. using MediaBrowser.Controller.Net;
  7. using MediaBrowser.Controller.Security;
  8. using MediaBrowser.Controller.Session;
  9. using MediaBrowser.Model.Devices;
  10. using MediaBrowser.Model.Querying;
  11. using Microsoft.AspNetCore.Authorization;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.AspNetCore.Mvc.ModelBinding;
  15. namespace Jellyfin.Api.Controllers
  16. {
  17. /// <summary>
  18. /// Devices Controller.
  19. /// </summary>
  20. [Authorize]
  21. public class DevicesController : BaseJellyfinApiController
  22. {
  23. private readonly IDeviceManager _deviceManager;
  24. private readonly IAuthenticationRepository _authenticationRepository;
  25. private readonly ISessionManager _sessionManager;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="DevicesController"/> class.
  28. /// </summary>
  29. /// <param name="deviceManager">Instance of <see cref="IDeviceManager"/> interface.</param>
  30. /// <param name="authenticationRepository">Instance of <see cref="IAuthenticationRepository"/> interface.</param>
  31. /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
  32. public DevicesController(
  33. IDeviceManager deviceManager,
  34. IAuthenticationRepository authenticationRepository,
  35. ISessionManager sessionManager)
  36. {
  37. _deviceManager = deviceManager;
  38. _authenticationRepository = authenticationRepository;
  39. _sessionManager = sessionManager;
  40. }
  41. /// <summary>
  42. /// Get Devices.
  43. /// </summary>
  44. /// <param name="supportsSync">/// Gets or sets a value indicating whether [supports synchronize].</param>
  45. /// <param name="userId">/// Gets or sets the user identifier.</param>
  46. /// <response code="200">Devices retrieved.</response>
  47. /// <returns>An <see cref="OkResult"/> containing the list of devices.</returns>
  48. [HttpGet]
  49. [Authorize(Policy = Policies.RequiresElevation)]
  50. [ProducesResponseType(StatusCodes.Status200OK)]
  51. public ActionResult<QueryResult<DeviceInfo>> GetDevices([FromQuery] bool? supportsSync, [FromQuery] Guid? userId)
  52. {
  53. var deviceQuery = new DeviceQuery { SupportsSync = supportsSync, UserId = userId ?? Guid.Empty };
  54. return _deviceManager.GetDevices(deviceQuery);
  55. }
  56. /// <summary>
  57. /// Get info for a device.
  58. /// </summary>
  59. /// <param name="id">Device Id.</param>
  60. /// <response code="200">Device info retrieved.</response>
  61. /// <response code="404">Device not found.</response>
  62. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  63. [HttpGet("Info")]
  64. [Authorize(Policy = Policies.RequiresElevation)]
  65. [ProducesResponseType(StatusCodes.Status200OK)]
  66. [ProducesResponseType(StatusCodes.Status404NotFound)]
  67. public ActionResult<DeviceInfo> GetDeviceInfo([FromQuery, BindRequired] string id)
  68. {
  69. var deviceInfo = _deviceManager.GetDevice(id);
  70. if (deviceInfo == null)
  71. {
  72. return NotFound();
  73. }
  74. return deviceInfo;
  75. }
  76. /// <summary>
  77. /// Get options for a device.
  78. /// </summary>
  79. /// <param name="id">Device Id.</param>
  80. /// <response code="200">Device options retrieved.</response>
  81. /// <response code="404">Device not found.</response>
  82. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  83. [HttpGet("Options")]
  84. [Authorize(Policy = Policies.RequiresElevation)]
  85. [ProducesResponseType(StatusCodes.Status200OK)]
  86. [ProducesResponseType(StatusCodes.Status404NotFound)]
  87. public ActionResult<DeviceOptions> GetDeviceOptions([FromQuery, BindRequired] string id)
  88. {
  89. var deviceInfo = _deviceManager.GetDeviceOptions(id);
  90. if (deviceInfo == null)
  91. {
  92. return NotFound();
  93. }
  94. return deviceInfo;
  95. }
  96. /// <summary>
  97. /// Update device options.
  98. /// </summary>
  99. /// <param name="id">Device Id.</param>
  100. /// <param name="deviceOptions">Device Options.</param>
  101. /// <response code="200">Device options updated.</response>
  102. /// <response code="404">Device not found.</response>
  103. /// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  104. [HttpPost("Options")]
  105. [Authorize(Policy = Policies.RequiresElevation)]
  106. [ProducesResponseType(StatusCodes.Status200OK)]
  107. [ProducesResponseType(StatusCodes.Status404NotFound)]
  108. public ActionResult UpdateDeviceOptions(
  109. [FromQuery, BindRequired] string id,
  110. [FromBody, BindRequired] DeviceOptions deviceOptions)
  111. {
  112. var existingDeviceOptions = _deviceManager.GetDeviceOptions(id);
  113. if (existingDeviceOptions == null)
  114. {
  115. return NotFound();
  116. }
  117. _deviceManager.UpdateDeviceOptions(id, deviceOptions);
  118. return Ok();
  119. }
  120. /// <summary>
  121. /// Deletes a device.
  122. /// </summary>
  123. /// <param name="id">Device Id.</param>
  124. /// <response code="200">Device deleted.</response>
  125. /// <response code="404">Device not found.</response>
  126. /// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  127. [HttpDelete]
  128. [ProducesResponseType(StatusCodes.Status200OK)]
  129. public ActionResult DeleteDevice([FromQuery, BindRequired] string id)
  130. {
  131. var existingDevice = _deviceManager.GetDevice(id);
  132. if (existingDevice == null)
  133. {
  134. return NotFound();
  135. }
  136. var sessions = _authenticationRepository.Get(new AuthenticationInfoQuery { DeviceId = id }).Items;
  137. foreach (var session in sessions)
  138. {
  139. _sessionManager.Logout(session);
  140. }
  141. return Ok();
  142. }
  143. }
  144. }