DevicesController.cs 6.3 KB

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