DevicesController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  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. namespace Jellyfin.Api.Controllers
  13. {
  14. /// <summary>
  15. /// Devices Controller.
  16. /// </summary>
  17. [Authorize(Policy = Policies.DefaultAuthorization)]
  18. public class DevicesController : BaseJellyfinApiController
  19. {
  20. private readonly IDeviceManager _deviceManager;
  21. private readonly IAuthenticationRepository _authenticationRepository;
  22. private readonly ISessionManager _sessionManager;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="DevicesController"/> class.
  25. /// </summary>
  26. /// <param name="deviceManager">Instance of <see cref="IDeviceManager"/> interface.</param>
  27. /// <param name="authenticationRepository">Instance of <see cref="IAuthenticationRepository"/> interface.</param>
  28. /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
  29. public DevicesController(
  30. IDeviceManager deviceManager,
  31. IAuthenticationRepository authenticationRepository,
  32. ISessionManager sessionManager)
  33. {
  34. _deviceManager = deviceManager;
  35. _authenticationRepository = authenticationRepository;
  36. _sessionManager = sessionManager;
  37. }
  38. /// <summary>
  39. /// Get Devices.
  40. /// </summary>
  41. /// <param name="supportsSync">Gets or sets a value indicating whether [supports synchronize].</param>
  42. /// <param name="userId">Gets or sets the user identifier.</param>
  43. /// <response code="200">Devices retrieved.</response>
  44. /// <returns>An <see cref="OkResult"/> containing the list of devices.</returns>
  45. [HttpGet]
  46. [Authorize(Policy = Policies.RequiresElevation)]
  47. [ProducesResponseType(StatusCodes.Status200OK)]
  48. public ActionResult<QueryResult<DeviceInfo>> GetDevices([FromQuery] bool? supportsSync, [FromQuery, Required] Guid? userId)
  49. {
  50. var deviceQuery = new DeviceQuery { SupportsSync = supportsSync, UserId = userId ?? Guid.Empty };
  51. return _deviceManager.GetDevices(deviceQuery);
  52. }
  53. /// <summary>
  54. /// Get info for a device.
  55. /// </summary>
  56. /// <param name="id">Device Id.</param>
  57. /// <response code="200">Device info retrieved.</response>
  58. /// <response code="404">Device not found.</response>
  59. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  60. [HttpGet("Info")]
  61. [Authorize(Policy = Policies.RequiresElevation)]
  62. [ProducesResponseType(StatusCodes.Status200OK)]
  63. [ProducesResponseType(StatusCodes.Status404NotFound)]
  64. public ActionResult<DeviceInfo> GetDeviceInfo([FromQuery, Required] string? id)
  65. {
  66. var deviceInfo = _deviceManager.GetDevice(id);
  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. [Authorize(Policy = Policies.RequiresElevation)]
  82. [ProducesResponseType(StatusCodes.Status200OK)]
  83. [ProducesResponseType(StatusCodes.Status404NotFound)]
  84. public ActionResult<DeviceOptions> GetDeviceOptions([FromQuery, Required] string? id)
  85. {
  86. var deviceInfo = _deviceManager.GetDeviceOptions(id);
  87. if (deviceInfo == null)
  88. {
  89. return NotFound();
  90. }
  91. return deviceInfo;
  92. }
  93. /// <summary>
  94. /// Update device options.
  95. /// </summary>
  96. /// <param name="id">Device Id.</param>
  97. /// <param name="deviceOptions">Device Options.</param>
  98. /// <response code="204">Device options updated.</response>
  99. /// <response code="404">Device not found.</response>
  100. /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  101. [HttpPost("Options")]
  102. [Authorize(Policy = Policies.RequiresElevation)]
  103. [ProducesResponseType(StatusCodes.Status204NoContent)]
  104. [ProducesResponseType(StatusCodes.Status404NotFound)]
  105. public ActionResult UpdateDeviceOptions(
  106. [FromQuery, Required] string? id,
  107. [FromBody, Required] DeviceOptions deviceOptions)
  108. {
  109. var existingDeviceOptions = _deviceManager.GetDeviceOptions(id);
  110. if (existingDeviceOptions == null)
  111. {
  112. return NotFound();
  113. }
  114. _deviceManager.UpdateDeviceOptions(id, deviceOptions);
  115. return NoContent();
  116. }
  117. /// <summary>
  118. /// Deletes a device.
  119. /// </summary>
  120. /// <param name="id">Device Id.</param>
  121. /// <response code="204">Device deleted.</response>
  122. /// <response code="404">Device not found.</response>
  123. /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  124. [HttpDelete]
  125. [ProducesResponseType(StatusCodes.Status204NoContent)]
  126. [ProducesResponseType(StatusCodes.Status404NotFound)]
  127. public ActionResult DeleteDevice([FromQuery, Required] 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 NoContent();
  140. }
  141. }
  142. }