DevicesController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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="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 async Task<ActionResult<QueryResult<DeviceInfo>>> GetDevices([FromQuery] bool? supportsSync, [FromQuery] Guid? userId)
  48. {
  49. userId = RequestHelpers.GetUserId(User, userId);
  50. return await _deviceManager.GetDevicesForUser(userId, supportsSync).ConfigureAwait(false);
  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 async Task<ActionResult<DeviceInfo>> GetDeviceInfo([FromQuery, Required] string id)
  63. {
  64. var deviceInfo = await _deviceManager.GetDevice(id).ConfigureAwait(false);
  65. if (deviceInfo is 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 async Task<ActionResult<DeviceOptions>> GetDeviceOptions([FromQuery, Required] string id)
  82. {
  83. var deviceInfo = await _deviceManager.GetDeviceOptions(id).ConfigureAwait(false);
  84. if (deviceInfo is 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. /// <returns>A <see cref="NoContentResult"/>.</returns>
  97. [HttpPost("Options")]
  98. [ProducesResponseType(StatusCodes.Status204NoContent)]
  99. public async Task<ActionResult> UpdateDeviceOptions(
  100. [FromQuery, Required] string id,
  101. [FromBody, Required] DeviceOptionsDto deviceOptions)
  102. {
  103. await _deviceManager.UpdateDeviceOptions(id, deviceOptions.CustomName).ConfigureAwait(false);
  104. return NoContent();
  105. }
  106. /// <summary>
  107. /// Deletes a device.
  108. /// </summary>
  109. /// <param name="id">Device Id.</param>
  110. /// <response code="204">Device deleted.</response>
  111. /// <response code="404">Device not found.</response>
  112. /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  113. [HttpDelete]
  114. [ProducesResponseType(StatusCodes.Status204NoContent)]
  115. [ProducesResponseType(StatusCodes.Status404NotFound)]
  116. public async Task<ActionResult> DeleteDevice([FromQuery, Required] string id)
  117. {
  118. var existingDevice = await _deviceManager.GetDevice(id).ConfigureAwait(false);
  119. if (existingDevice is null)
  120. {
  121. return NotFound();
  122. }
  123. var sessions = await _deviceManager.GetDevices(new DeviceQuery { DeviceId = id }).ConfigureAwait(false);
  124. foreach (var session in sessions.Items)
  125. {
  126. await _sessionManager.Logout(session).ConfigureAwait(false);
  127. }
  128. return NoContent();
  129. }
  130. }