2020-04-19 21:33:55 -04:00
using System ;
2020-08-06 10:17:45 -04:00
using System.ComponentModel.DataAnnotations ;
2021-04-10 16:17:36 -04:00
using System.Threading.Tasks ;
2020-05-19 12:05:23 -04:00
using Jellyfin.Api.Constants ;
2021-07-13 19:30:11 -04:00
using Jellyfin.Data.Dtos ;
2021-04-10 16:17:36 -04:00
using Jellyfin.Data.Entities.Security ;
2021-05-20 23:56:59 -04:00
using Jellyfin.Data.Queries ;
2020-04-19 21:33:55 -04:00
using MediaBrowser.Controller.Devices ;
using MediaBrowser.Controller.Session ;
using MediaBrowser.Model.Devices ;
2020-05-19 12:06:25 -04:00
using MediaBrowser.Model.Querying ;
2020-05-19 12:05:23 -04:00
using Microsoft.AspNetCore.Authorization ;
2020-04-19 21:33:55 -04:00
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Devices Controller.
/// </summary>
2020-11-01 22:37:03 -05:00
[Authorize(Policy = Policies.RequiresElevation)]
2020-04-19 21:33:55 -04:00
public class DevicesController : BaseJellyfinApiController
{
private readonly IDeviceManager _deviceManager ;
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="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
public DevicesController (
IDeviceManager deviceManager ,
ISessionManager sessionManager )
{
_deviceManager = deviceManager ;
_sessionManager = sessionManager ;
}
/// <summary>
/// Get Devices.
/// </summary>
2020-06-04 09:29:00 -04: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-02 19:23:02 -04:00
/// <response code="200">Devices retrieved.</response>
/// <returns>An <see cref="OkResult"/> containing the list of devices.</returns>
2020-04-19 21:33:55 -04:00
[HttpGet]
2020-04-21 15:59:43 -04:00
[ProducesResponseType(StatusCodes.Status200OK)]
2021-04-10 16:17:36 -04:00
public async Task < ActionResult < QueryResult < DeviceInfo > > > GetDevices ( [ FromQuery ] bool? supportsSync , [ FromQuery ] Guid ? userId )
2020-04-19 21:33:55 -04:00
{
2021-06-19 15:09:16 -04:00
return await _deviceManager . GetDevicesForUser ( userId , supportsSync ) . ConfigureAwait ( false ) ;
2020-04-19 21:33:55 -04:00
}
/// <summary>
/// Get info for a device.
/// </summary>
/// <param name="id">Device Id.</param>
2020-05-02 19:23:02 -04: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-19 21:33:55 -04:00
[HttpGet("Info")]
2020-04-21 15:59:43 -04:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-19 21:33:55 -04:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2021-04-10 16:17:36 -04:00
public async Task < ActionResult < DeviceInfo > > GetDeviceInfo ( [ FromQuery , Required ] string id )
2020-04-19 21:33:55 -04:00
{
2021-04-10 16:17:36 -04:00
var deviceInfo = await _deviceManager . GetDevice ( id ) . ConfigureAwait ( false ) ;
2020-04-21 09:55:01 -04:00
if ( deviceInfo = = null )
2020-04-19 21:33:55 -04:00
{
2020-04-21 09:55:01 -04:00
return NotFound ( ) ;
2020-04-19 21:33:55 -04:00
}
2020-04-21 09:55:01 -04:00
2020-04-23 09:55:47 -04:00
return deviceInfo ;
2020-04-19 21:33:55 -04:00
}
/// <summary>
/// Get options for a device.
/// </summary>
/// <param name="id">Device Id.</param>
2020-05-02 19:23:02 -04: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-19 21:33:55 -04:00
[HttpGet("Options")]
2020-04-21 15:59:43 -04:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-19 21:33:55 -04:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2021-04-10 16:57:25 -04:00
public async Task < ActionResult < DeviceOptions > > GetDeviceOptions ( [ FromQuery , Required ] string id )
2020-04-19 21:33:55 -04:00
{
2021-04-10 16:57:25 -04:00
var deviceInfo = await _deviceManager . GetDeviceOptions ( id ) . ConfigureAwait ( false ) ;
2020-04-21 09:55:01 -04:00
if ( deviceInfo = = null )
2020-04-19 21:33:55 -04:00
{
2020-04-21 09:55:01 -04:00
return NotFound ( ) ;
2020-04-19 21:33:55 -04:00
}
2020-04-21 09:55:01 -04:00
2020-04-23 09:55:47 -04:00
return deviceInfo ;
2020-04-19 21:33:55 -04:00
}
/// <summary>
/// Update device options.
/// </summary>
/// <param name="id">Device Id.</param>
/// <param name="deviceOptions">Device Options.</param>
2020-06-12 06:38:13 -04:00
/// <response code="204">Device options updated.</response>
2021-08-13 21:08:49 -04:00
/// <returns>A <see cref="NoContentResult"/>.</returns>
2020-04-19 21:33:55 -04:00
[HttpPost("Options")]
2020-06-12 06:38:13 -04:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2021-04-10 16:17:36 -04:00
public async Task < ActionResult > UpdateDeviceOptions (
2020-09-09 16:28:30 -04:00
[FromQuery, Required] string id ,
2021-07-13 19:30:11 -04:00
[FromBody, Required] DeviceOptionsDto deviceOptions )
2020-04-19 21:33:55 -04:00
{
2021-07-13 19:30:11 -04:00
await _deviceManager . UpdateDeviceOptions ( id , deviceOptions . CustomName ) . ConfigureAwait ( false ) ;
2020-06-12 06:38:13 -04:00
return NoContent ( ) ;
2020-04-19 21:33:55 -04:00
}
/// <summary>
/// Deletes a device.
/// </summary>
/// <param name="id">Device Id.</param>
2020-06-12 06:38:13 -04:00
/// <response code="204">Device deleted.</response>
2020-05-02 19:23:02 -04:00
/// <response code="404">Device not found.</response>
2020-06-12 06:38:13 -04:00
/// <returns>A <see cref="NoContentResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
2020-04-19 21:33:55 -04:00
[HttpDelete]
2020-06-12 06:38:13 -04:00
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-06-20 20:02:07 -04:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2021-05-20 23:56:59 -04:00
public async Task < ActionResult > DeleteDevice ( [ FromQuery , Required ] string id )
2020-04-19 21:33:55 -04:00
{
2021-05-20 23:56:59 -04:00
var existingDevice = await _deviceManager . GetDevice ( id ) . ConfigureAwait ( false ) ;
2020-05-02 19:23:02 -04:00
if ( existingDevice = = null )
{
return NotFound ( ) ;
}
2021-05-20 23:56:59 -04:00
var sessions = await _deviceManager . GetDevices ( new DeviceQuery { DeviceId = id } ) . ConfigureAwait ( false ) ;
2020-04-19 21:33:55 -04:00
2021-05-20 23:56:59 -04:00
foreach ( var session in sessions . Items )
2020-04-19 21:33:55 -04:00
{
2021-05-20 23:56:59 -04:00
await _sessionManager . Logout ( session ) . ConfigureAwait ( false ) ;
2020-04-19 21:33:55 -04:00
}
2020-04-21 09:55:01 -04:00
2020-06-12 06:38:13 -04:00
return NoContent ( ) ;
2020-04-19 21:33:55 -04:00
}
}
}