2020-04-20 03:33:55 +02:00
using System ;
2020-05-19 18:05:23 +02:00
using Jellyfin.Api.Constants ;
2020-04-20 03:33:55 +02:00
using MediaBrowser.Controller.Devices ;
using MediaBrowser.Controller.Security ;
using MediaBrowser.Controller.Session ;
using MediaBrowser.Model.Devices ;
2020-05-19 18:06:25 +02:00
using MediaBrowser.Model.Querying ;
2020-05-19 18:05:23 +02:00
using Microsoft.AspNetCore.Authorization ;
2020-04-20 03:33:55 +02:00
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.Mvc.ModelBinding ;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Devices Controller.
/// </summary>
2020-06-16 22:15:58 +02:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-04-20 03:33:55 +02:00
public class DevicesController : BaseJellyfinApiController
{
private readonly IDeviceManager _deviceManager ;
private readonly IAuthenticationRepository _authenticationRepository ;
private readonly ISessionManager _sessionManager ;
/// <summary>
/// Initializes a new instance of the <see cref="DevicesController"/> class.
/// </summary>
/// <param name="deviceManager">Instance of <see cref="IDeviceManager"/> interface.</param>
/// <param name="authenticationRepository">Instance of <see cref="IAuthenticationRepository"/> interface.</param>
/// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
public DevicesController (
IDeviceManager deviceManager ,
IAuthenticationRepository authenticationRepository ,
ISessionManager sessionManager )
{
_deviceManager = deviceManager ;
_authenticationRepository = authenticationRepository ;
_sessionManager = sessionManager ;
}
/// <summary>
/// Get Devices.
/// </summary>
2020-06-04 15:29:00 +02:00
/// <param name="supportsSync">Gets or sets a value indicating whether [supports synchronize].</param>
/// <param name="userId">Gets or sets the user identifier.</param>
2020-05-03 01:23:02 +02:00
/// <response code="200">Devices retrieved.</response>
/// <returns>An <see cref="OkResult"/> containing the list of devices.</returns>
2020-04-20 03:33:55 +02:00
[HttpGet]
2020-05-19 18:05:23 +02:00
[Authorize(Policy = Policies.RequiresElevation)]
2020-04-21 21:59:43 +02:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-05-19 18:06:25 +02:00
public ActionResult < QueryResult < DeviceInfo > > GetDevices ( [ FromQuery ] bool? supportsSync , [ FromQuery ] Guid ? userId )
2020-04-20 03:33:55 +02:00
{
2020-04-21 15:55:01 +02:00
var deviceQuery = new DeviceQuery { SupportsSync = supportsSync , UserId = userId ? ? Guid . Empty } ;
2020-05-19 18:06:25 +02:00
return _deviceManager . GetDevices ( deviceQuery ) ;
2020-04-20 03:33:55 +02:00
}
/// <summary>
/// Get info for a device.
/// </summary>
/// <param name="id">Device Id.</param>
2020-05-03 01:23:02 +02:00
/// <response code="200">Device info retrieved.</response>
/// <response code="404">Device not found.</response>
/// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
2020-04-20 03:33:55 +02:00
[HttpGet("Info")]
2020-05-19 18:05:23 +02:00
[Authorize(Policy = Policies.RequiresElevation)]
2020-04-21 21:59:43 +02:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-20 03:33:55 +02:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-06-27 18:50:44 +02:00
public ActionResult < DeviceInfo > GetDeviceInfo ( [ FromQuery , BindRequired ] string? id )
2020-04-20 03:33:55 +02:00
{
2020-04-21 15:55:01 +02:00
var deviceInfo = _deviceManager . GetDevice ( id ) ;
if ( deviceInfo = = null )
2020-04-20 03:33:55 +02:00
{
2020-04-21 15:55:01 +02:00
return NotFound ( ) ;
2020-04-20 03:33:55 +02:00
}
2020-04-21 15:55:01 +02:00
2020-04-23 15:55:47 +02:00
return deviceInfo ;
2020-04-20 03:33:55 +02:00
}
/// <summary>
/// Get options for a device.
/// </summary>
/// <param name="id">Device Id.</param>
2020-05-03 01:23:02 +02:00
/// <response code="200">Device options retrieved.</response>
/// <response code="404">Device not found.</response>
/// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
2020-04-20 03:33:55 +02:00
[HttpGet("Options")]
2020-05-19 18:05:23 +02:00
[Authorize(Policy = Policies.RequiresElevation)]
2020-04-21 21:59:43 +02:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-20 03:33:55 +02:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-06-27 18:50:44 +02:00
public ActionResult < DeviceOptions > GetDeviceOptions ( [ FromQuery , BindRequired ] string? id )
2020-04-20 03:33:55 +02:00
{
2020-04-21 15:55:01 +02:00
var deviceInfo = _deviceManager . GetDeviceOptions ( id ) ;
if ( deviceInfo = = null )
2020-04-20 03:33:55 +02:00
{
2020-04-21 15:55:01 +02:00
return NotFound ( ) ;
2020-04-20 03:33:55 +02:00
}
2020-04-21 15:55:01 +02:00
2020-04-23 15:55:47 +02:00
return deviceInfo ;
2020-04-20 03:33:55 +02:00
}
/// <summary>
/// Update device options.
/// </summary>
/// <param name="id">Device Id.</param>
/// <param name="deviceOptions">Device Options.</param>
2020-06-12 12:38:13 +02:00
/// <response code="204">Device options updated.</response>
2020-05-03 01:23:02 +02:00
/// <response code="404">Device not found.</response>
2020-06-12 12:38:13 +02:00
/// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
2020-04-20 03:33:55 +02:00
[HttpPost("Options")]
2020-05-19 18:05:23 +02:00
[Authorize(Policy = Policies.RequiresElevation)]
2020-06-12 12:38:13 +02:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-04-20 03:33:55 +02:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 21:59:43 +02:00
public ActionResult UpdateDeviceOptions (
2020-06-27 18:50:44 +02:00
[FromQuery, BindRequired] string? id ,
2020-04-20 03:33:55 +02:00
[FromBody, BindRequired] DeviceOptions deviceOptions )
{
2020-04-21 15:55:01 +02:00
var existingDeviceOptions = _deviceManager . GetDeviceOptions ( id ) ;
if ( existingDeviceOptions = = null )
2020-04-20 03:33:55 +02:00
{
2020-04-21 15:55:01 +02:00
return NotFound ( ) ;
2020-04-20 03:33:55 +02:00
}
2020-04-21 15:55:01 +02:00
_deviceManager . UpdateDeviceOptions ( id , deviceOptions ) ;
2020-06-12 12:38:13 +02:00
return NoContent ( ) ;
2020-04-20 03:33:55 +02:00
}
/// <summary>
/// Deletes a device.
/// </summary>
/// <param name="id">Device Id.</param>
2020-06-12 12:38:13 +02:00
/// <response code="204">Device deleted.</response>
2020-05-03 01:23:02 +02:00
/// <response code="404">Device not found.</response>
2020-06-12 12:38:13 +02:00
/// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
2020-04-20 03:33:55 +02:00
[HttpDelete]
2020-06-12 12:38:13 +02:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-06-21 02:02:07 +02:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-06-27 18:50:44 +02:00
public ActionResult DeleteDevice ( [ FromQuery , BindRequired ] string? id )
2020-04-20 03:33:55 +02:00
{
2020-05-03 01:23:02 +02:00
var existingDevice = _deviceManager . GetDevice ( id ) ;
if ( existingDevice = = null )
{
return NotFound ( ) ;
}
2020-04-21 15:55:01 +02:00
var sessions = _authenticationRepository . Get ( new AuthenticationInfoQuery { DeviceId = id } ) . Items ;
2020-04-20 03:33:55 +02:00
2020-04-21 15:55:01 +02:00
foreach ( var session in sessions )
2020-04-20 03:33:55 +02:00
{
2020-04-21 15:55:01 +02:00
_sessionManager . Logout ( session ) ;
2020-04-20 03:33:55 +02:00
}
2020-04-21 15:55:01 +02:00
2020-06-12 12:38:13 +02:00
return NoContent ( ) ;
2020-04-20 03:33:55 +02:00
}
}
}