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