DevicesController.cs 5.2 KB

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