DevicesController.cs 6.3 KB

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