DevicesController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. {
  17. /// <summary>
  18. /// Devices Controller.
  19. /// </summary>
  20. [Authorize(Policy = Policies.RequiresElevation)]
  21. public class DevicesController : BaseJellyfinApiController
  22. {
  23. private readonly IDeviceManager _deviceManager;
  24. private readonly ISessionManager _sessionManager;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="DevicesController"/> class.
  27. /// </summary>
  28. /// <param name="deviceManager">Instance of <see cref="IDeviceManager"/> interface.</param>
  29. /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
  30. public DevicesController(
  31. IDeviceManager deviceManager,
  32. ISessionManager sessionManager)
  33. {
  34. _deviceManager = deviceManager;
  35. _sessionManager = sessionManager;
  36. }
  37. /// <summary>
  38. /// Get Devices.
  39. /// </summary>
  40. /// <param name="supportsSync">Gets or sets a value indicating whether [supports synchronize].</param>
  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] bool? supportsSync, [FromQuery] Guid? userId)
  47. {
  48. return await _deviceManager.GetDevicesForUser(userId, supportsSync).ConfigureAwait(false);
  49. }
  50. /// <summary>
  51. /// Get info for a device.
  52. /// </summary>
  53. /// <param name="id">Device Id.</param>
  54. /// <response code="200">Device info retrieved.</response>
  55. /// <response code="404">Device not found.</response>
  56. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  57. [HttpGet("Info")]
  58. [ProducesResponseType(StatusCodes.Status200OK)]
  59. [ProducesResponseType(StatusCodes.Status404NotFound)]
  60. public async Task<ActionResult<DeviceInfo>> GetDeviceInfo([FromQuery, Required] string id)
  61. {
  62. var deviceInfo = await _deviceManager.GetDevice(id).ConfigureAwait(false);
  63. if (deviceInfo is null)
  64. {
  65. return NotFound();
  66. }
  67. return deviceInfo;
  68. }
  69. /// <summary>
  70. /// Get options for a device.
  71. /// </summary>
  72. /// <param name="id">Device Id.</param>
  73. /// <response code="200">Device options retrieved.</response>
  74. /// <response code="404">Device not found.</response>
  75. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  76. [HttpGet("Options")]
  77. [ProducesResponseType(StatusCodes.Status200OK)]
  78. [ProducesResponseType(StatusCodes.Status404NotFound)]
  79. public async Task<ActionResult<DeviceOptions>> GetDeviceOptions([FromQuery, Required] string id)
  80. {
  81. var deviceInfo = await _deviceManager.GetDeviceOptions(id).ConfigureAwait(false);
  82. if (deviceInfo is null)
  83. {
  84. return NotFound();
  85. }
  86. return deviceInfo;
  87. }
  88. /// <summary>
  89. /// Update device options.
  90. /// </summary>
  91. /// <param name="id">Device Id.</param>
  92. /// <param name="deviceOptions">Device Options.</param>
  93. /// <response code="204">Device options updated.</response>
  94. /// <returns>A <see cref="NoContentResult"/>.</returns>
  95. [HttpPost("Options")]
  96. [ProducesResponseType(StatusCodes.Status204NoContent)]
  97. public async Task<ActionResult> UpdateDeviceOptions(
  98. [FromQuery, Required] string id,
  99. [FromBody, Required] DeviceOptionsDto deviceOptions)
  100. {
  101. await _deviceManager.UpdateDeviceOptions(id, deviceOptions.CustomName).ConfigureAwait(false);
  102. return NoContent();
  103. }
  104. /// <summary>
  105. /// Deletes a device.
  106. /// </summary>
  107. /// <param name="id">Device Id.</param>
  108. /// <response code="204">Device deleted.</response>
  109. /// <response code="404">Device not found.</response>
  110. /// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  111. [HttpDelete]
  112. [ProducesResponseType(StatusCodes.Status204NoContent)]
  113. [ProducesResponseType(StatusCodes.Status404NotFound)]
  114. public async Task<ActionResult> DeleteDevice([FromQuery, Required] string id)
  115. {
  116. var existingDevice = await _deviceManager.GetDevice(id).ConfigureAwait(false);
  117. if (existingDevice is null)
  118. {
  119. return NotFound();
  120. }
  121. var sessions = await _deviceManager.GetDevices(new DeviceQuery { DeviceId = id }).ConfigureAwait(false);
  122. foreach (var session in sessions.Items)
  123. {
  124. await _sessionManager.Logout(session).ConfigureAwait(false);
  125. }
  126. return NoContent();
  127. }
  128. }
  129. }