DevicesController.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Threading.Tasks;
  4. using Jellyfin.Api.Constants;
  5. using Jellyfin.Data.Dtos;
  6. using Jellyfin.Data.Entities.Security;
  7. using Jellyfin.Data.Queries;
  8. using MediaBrowser.Controller.Devices;
  9. using MediaBrowser.Controller.Session;
  10. using MediaBrowser.Model.Devices;
  11. using MediaBrowser.Model.Querying;
  12. using Microsoft.AspNetCore.Authorization;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.AspNetCore.Mvc;
  15. namespace Jellyfin.Api.Controllers;
  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 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="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
  29. public DevicesController(
  30. IDeviceManager deviceManager,
  31. ISessionManager sessionManager)
  32. {
  33. _deviceManager = deviceManager;
  34. _sessionManager = sessionManager;
  35. }
  36. /// <summary>
  37. /// Get Devices.
  38. /// </summary>
  39. /// <param name="supportsSync">Gets or sets a value indicating whether [supports synchronize].</param>
  40. /// <param name="userId">Gets or sets the user identifier.</param>
  41. /// <response code="200">Devices retrieved.</response>
  42. /// <returns>An <see cref="OkResult"/> containing the list of devices.</returns>
  43. [HttpGet]
  44. [ProducesResponseType(StatusCodes.Status200OK)]
  45. public async Task<ActionResult<QueryResult<DeviceInfo>>> GetDevices([FromQuery] bool? supportsSync, [FromQuery] Guid? userId)
  46. {
  47. return await _deviceManager.GetDevicesForUser(userId, supportsSync).ConfigureAwait(false);
  48. }
  49. /// <summary>
  50. /// Get info for a device.
  51. /// </summary>
  52. /// <param name="id">Device Id.</param>
  53. /// <response code="200">Device info retrieved.</response>
  54. /// <response code="404">Device not found.</response>
  55. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  56. [HttpGet("Info")]
  57. [ProducesResponseType(StatusCodes.Status200OK)]
  58. [ProducesResponseType(StatusCodes.Status404NotFound)]
  59. public async Task<ActionResult<DeviceInfo>> GetDeviceInfo([FromQuery, Required] string id)
  60. {
  61. var deviceInfo = await _deviceManager.GetDevice(id).ConfigureAwait(false);
  62. if (deviceInfo is null)
  63. {
  64. return NotFound();
  65. }
  66. return deviceInfo;
  67. }
  68. /// <summary>
  69. /// Get options for a device.
  70. /// </summary>
  71. /// <param name="id">Device Id.</param>
  72. /// <response code="200">Device options retrieved.</response>
  73. /// <response code="404">Device not found.</response>
  74. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  75. [HttpGet("Options")]
  76. [ProducesResponseType(StatusCodes.Status200OK)]
  77. [ProducesResponseType(StatusCodes.Status404NotFound)]
  78. public async Task<ActionResult<DeviceOptions>> GetDeviceOptions([FromQuery, Required] string id)
  79. {
  80. var deviceInfo = await _deviceManager.GetDeviceOptions(id).ConfigureAwait(false);
  81. if (deviceInfo is null)
  82. {
  83. return NotFound();
  84. }
  85. return deviceInfo;
  86. }
  87. /// <summary>
  88. /// Update device options.
  89. /// </summary>
  90. /// <param name="id">Device Id.</param>
  91. /// <param name="deviceOptions">Device Options.</param>
  92. /// <response code="204">Device options updated.</response>
  93. /// <returns>A <see cref="NoContentResult"/>.</returns>
  94. [HttpPost("Options")]
  95. [ProducesResponseType(StatusCodes.Status204NoContent)]
  96. public async Task<ActionResult> UpdateDeviceOptions(
  97. [FromQuery, Required] string id,
  98. [FromBody, Required] DeviceOptionsDto deviceOptions)
  99. {
  100. await _deviceManager.UpdateDeviceOptions(id, deviceOptions.CustomName).ConfigureAwait(false);
  101. return NoContent();
  102. }
  103. /// <summary>
  104. /// Deletes a device.
  105. /// </summary>
  106. /// <param name="id">Device Id.</param>
  107. /// <response code="204">Device deleted.</response>
  108. /// <response code="404">Device not found.</response>
  109. /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  110. [HttpDelete]
  111. [ProducesResponseType(StatusCodes.Status204NoContent)]
  112. [ProducesResponseType(StatusCodes.Status404NotFound)]
  113. public async Task<ActionResult> DeleteDevice([FromQuery, Required] string id)
  114. {
  115. var existingDevice = await _deviceManager.GetDevice(id).ConfigureAwait(false);
  116. if (existingDevice is null)
  117. {
  118. return NotFound();
  119. }
  120. var sessions = await _deviceManager.GetDevices(new DeviceQuery { DeviceId = id }).ConfigureAwait(false);
  121. foreach (var session in sessions.Items)
  122. {
  123. await _sessionManager.Logout(session).ConfigureAwait(false);
  124. }
  125. return NoContent();
  126. }
  127. }