DevicesController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.RequiresElevation)]
  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. [ProducesResponseType(StatusCodes.Status200OK)]
  47. public ActionResult<QueryResult<DeviceInfo>> GetDevices([FromQuery] bool? supportsSync, [FromQuery] Guid? userId)
  48. {
  49. var deviceQuery = new DeviceQuery { SupportsSync = supportsSync, UserId = userId ?? Guid.Empty };
  50. return _deviceManager.GetDevices(deviceQuery);
  51. }
  52. /// <summary>
  53. /// Get info for a device.
  54. /// </summary>
  55. /// <param name="id">Device Id.</param>
  56. /// <response code="200">Device info retrieved.</response>
  57. /// <response code="404">Device not found.</response>
  58. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  59. [HttpGet("Info")]
  60. [ProducesResponseType(StatusCodes.Status200OK)]
  61. [ProducesResponseType(StatusCodes.Status404NotFound)]
  62. public ActionResult<DeviceInfo> GetDeviceInfo([FromQuery, Required] string id)
  63. {
  64. var deviceInfo = _deviceManager.GetDevice(id);
  65. if (deviceInfo == null)
  66. {
  67. return NotFound();
  68. }
  69. return deviceInfo;
  70. }
  71. /// <summary>
  72. /// Get options for a device.
  73. /// </summary>
  74. /// <param name="id">Device Id.</param>
  75. /// <response code="200">Device options retrieved.</response>
  76. /// <response code="404">Device not found.</response>
  77. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  78. [HttpGet("Options")]
  79. [ProducesResponseType(StatusCodes.Status200OK)]
  80. [ProducesResponseType(StatusCodes.Status404NotFound)]
  81. public ActionResult<DeviceOptions> GetDeviceOptions([FromQuery, Required] string id)
  82. {
  83. var deviceInfo = _deviceManager.GetDeviceOptions(id);
  84. if (deviceInfo == null)
  85. {
  86. return NotFound();
  87. }
  88. return deviceInfo;
  89. }
  90. /// <summary>
  91. /// Update device options.
  92. /// </summary>
  93. /// <param name="id">Device Id.</param>
  94. /// <param name="deviceOptions">Device Options.</param>
  95. /// <response code="204">Device options updated.</response>
  96. /// <response code="404">Device not found.</response>
  97. /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  98. [HttpPost("Options")]
  99. [ProducesResponseType(StatusCodes.Status204NoContent)]
  100. [ProducesResponseType(StatusCodes.Status404NotFound)]
  101. public ActionResult UpdateDeviceOptions(
  102. [FromQuery, Required] string id,
  103. [FromBody, Required] DeviceOptions deviceOptions)
  104. {
  105. var existingDeviceOptions = _deviceManager.GetDeviceOptions(id);
  106. if (existingDeviceOptions == null)
  107. {
  108. return NotFound();
  109. }
  110. _deviceManager.UpdateDeviceOptions(id, deviceOptions);
  111. return NoContent();
  112. }
  113. /// <summary>
  114. /// Deletes a device.
  115. /// </summary>
  116. /// <param name="id">Device Id.</param>
  117. /// <response code="204">Device deleted.</response>
  118. /// <response code="404">Device not found.</response>
  119. /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  120. [HttpDelete]
  121. [ProducesResponseType(StatusCodes.Status204NoContent)]
  122. [ProducesResponseType(StatusCodes.Status404NotFound)]
  123. public ActionResult DeleteDevice([FromQuery, Required] string id)
  124. {
  125. var existingDevice = _deviceManager.GetDevice(id);
  126. if (existingDevice == null)
  127. {
  128. return NotFound();
  129. }
  130. var sessions = _authenticationRepository.Get(new AuthenticationInfoQuery { DeviceId = id }).Items;
  131. foreach (var session in sessions)
  132. {
  133. _sessionManager.Logout(session);
  134. }
  135. return NoContent();
  136. }
  137. }
  138. }