Move DeviceService to DevicesController

This commit is contained in:
crobibero 2020-04-19 19:33:55 -06:00
parent 16401ec7ae
commit 21b54b4ad8
2 changed files with 260 additions and 168 deletions

View File

@ -0,0 +1,260 @@
#nullable enable
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Devices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Devices Controller.
/// </summary>
[Authenticated]
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>
/// <param name="supportsSync">/// Gets or sets a value indicating whether [supports synchronize].</param>
/// <param name="userId">/// Gets or sets the user identifier.</param>
/// <returns>Device Infos.</returns>
[HttpGet]
[ProducesResponseType(typeof(DeviceInfo[]), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public IActionResult GetDevices([FromQuery] bool? supportsSync, [FromQuery] Guid? userId)
{
try
{
var deviceQuery = new DeviceQuery { SupportsSync = supportsSync, UserId = userId ?? Guid.Empty };
var devices = _deviceManager.GetDevices(deviceQuery);
return Ok(devices);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
}
/// <summary>
/// Get info for a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <returns>Device Info.</returns>
[HttpGet("Info")]
[ProducesResponseType(typeof(DeviceInfo), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public IActionResult GetDeviceInfo([FromQuery, BindRequired] string id)
{
try
{
var deviceInfo = _deviceManager.GetDevice(id);
if (deviceInfo == null)
{
return NotFound();
}
return Ok(deviceInfo);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
}
/// <summary>
/// Get options for a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <returns>Device Info.</returns>
[HttpGet("Options")]
[ProducesResponseType(typeof(DeviceOptions), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public IActionResult GetDeviceOptions([FromQuery, BindRequired] string id)
{
try
{
var deviceInfo = _deviceManager.GetDeviceOptions(id);
if (deviceInfo == null)
{
return NotFound();
}
return Ok(deviceInfo);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
}
/// <summary>
/// Update device options.
/// </summary>
/// <param name="id">Device Id.</param>
/// <param name="deviceOptions">Device Options.</param>
/// <returns>Status.</returns>
[HttpPost("Options")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public IActionResult UpdateDeviceOptions(
[FromQuery, BindRequired] string id,
[FromBody, BindRequired] DeviceOptions deviceOptions)
{
try
{
var existingDeviceOptions = _deviceManager.GetDeviceOptions(id);
if (existingDeviceOptions == null)
{
return NotFound();
}
_deviceManager.UpdateDeviceOptions(id, deviceOptions);
return Ok();
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
}
/// <summary>
/// Deletes a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <returns>Status.</returns>
[HttpDelete]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public IActionResult DeleteDevice([FromQuery, BindRequired] string id)
{
try
{
var sessions = _authenticationRepository.Get(new AuthenticationInfoQuery { DeviceId = id }).Items;
foreach (var session in sessions)
{
_sessionManager.Logout(session);
}
return Ok();
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
}
/// <summary>
/// Gets camera upload history for a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <returns>Content Upload History.</returns>
[HttpGet("CameraUploads")]
[ProducesResponseType(typeof(ContentUploadHistory), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public IActionResult GetCameraUploads([FromQuery, BindRequired] string id)
{
try
{
var uploadHistory = _deviceManager.GetCameraUploadHistory(id);
return Ok(uploadHistory);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
}
/// <summary>
/// Uploads content.
/// </summary>
/// <param name="deviceId">Device Id.</param>
/// <param name="album">Album.</param>
/// <param name="name">Name.</param>
/// <param name="id">Id.</param>
/// <returns>Status.</returns>
[HttpPost("CameraUploads")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> PostCameraUploadAsync(
[FromQuery, BindRequired] string deviceId,
[FromQuery, BindRequired] string album,
[FromQuery, BindRequired] string name,
[FromQuery, BindRequired] string id)
{
try
{
Stream fileStream;
string contentType;
if (Request.HasFormContentType)
{
if (Request.Form.Files.Any())
{
fileStream = Request.Form.Files[0].OpenReadStream();
contentType = Request.Form.Files[0].ContentType;
}
else
{
return BadRequest();
}
}
else
{
fileStream = Request.Body;
contentType = Request.ContentType;
}
await _deviceManager.AcceptCameraUpload(
deviceId,
fileStream,
new LocalFileInfo
{
MimeType = contentType,
Album = album,
Name = name,
Id = id
}).ConfigureAwait(false);
return Ok();
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
}
}
}

View File

@ -1,168 +0,0 @@
using System.IO;
using System.Threading.Tasks;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Devices;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Services;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api.Devices
{
[Route("/Devices", "GET", Summary = "Gets all devices")]
[Authenticated(Roles = "Admin")]
public class GetDevices : DeviceQuery, IReturn<QueryResult<DeviceInfo>>
{
}
[Route("/Devices/Info", "GET", Summary = "Gets info for a device")]
[Authenticated(Roles = "Admin")]
public class GetDeviceInfo : IReturn<DeviceInfo>
{
[ApiMember(Name = "Id", Description = "Device Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string Id { get; set; }
}
[Route("/Devices/Options", "GET", Summary = "Gets options for a device")]
[Authenticated(Roles = "Admin")]
public class GetDeviceOptions : IReturn<DeviceOptions>
{
[ApiMember(Name = "Id", Description = "Device Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string Id { get; set; }
}
[Route("/Devices", "DELETE", Summary = "Deletes a device")]
public class DeleteDevice
{
[ApiMember(Name = "Id", Description = "Device Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string Id { get; set; }
}
[Route("/Devices/CameraUploads", "GET", Summary = "Gets camera upload history for a device")]
[Authenticated]
public class GetCameraUploads : IReturn<ContentUploadHistory>
{
[ApiMember(Name = "Id", Description = "Device Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string DeviceId { get; set; }
}
[Route("/Devices/CameraUploads", "POST", Summary = "Uploads content")]
[Authenticated]
public class PostCameraUpload : IRequiresRequestStream, IReturnVoid
{
[ApiMember(Name = "DeviceId", Description = "Device Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string DeviceId { get; set; }
[ApiMember(Name = "Album", Description = "Album", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Album { get; set; }
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Name { get; set; }
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Id { get; set; }
public Stream RequestStream { get; set; }
}
[Route("/Devices/Options", "POST", Summary = "Updates device options")]
[Authenticated(Roles = "Admin")]
public class PostDeviceOptions : DeviceOptions, IReturnVoid
{
[ApiMember(Name = "Id", Description = "Device Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string Id { get; set; }
}
public class DeviceService : BaseApiService
{
private readonly IDeviceManager _deviceManager;
private readonly IAuthenticationRepository _authRepo;
private readonly ISessionManager _sessionManager;
public DeviceService(
ILogger<DeviceService> logger,
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
IDeviceManager deviceManager,
IAuthenticationRepository authRepo,
ISessionManager sessionManager)
: base(logger, serverConfigurationManager, httpResultFactory)
{
_deviceManager = deviceManager;
_authRepo = authRepo;
_sessionManager = sessionManager;
}
public void Post(PostDeviceOptions request)
{
_deviceManager.UpdateDeviceOptions(request.Id, request);
}
public object Get(GetDevices request)
{
return ToOptimizedResult(_deviceManager.GetDevices(request));
}
public object Get(GetDeviceInfo request)
{
return _deviceManager.GetDevice(request.Id);
}
public object Get(GetDeviceOptions request)
{
return _deviceManager.GetDeviceOptions(request.Id);
}
public object Get(GetCameraUploads request)
{
return ToOptimizedResult(_deviceManager.GetCameraUploadHistory(request.DeviceId));
}
public void Delete(DeleteDevice request)
{
var sessions = _authRepo.Get(new AuthenticationInfoQuery
{
DeviceId = request.Id
}).Items;
foreach (var session in sessions)
{
_sessionManager.Logout(session);
}
}
public Task Post(PostCameraUpload request)
{
var deviceId = Request.QueryString["DeviceId"];
var album = Request.QueryString["Album"];
var id = Request.QueryString["Id"];
var name = Request.QueryString["Name"];
var req = Request.Response.HttpContext.Request;
if (req.HasFormContentType)
{
var file = req.Form.Files.Count == 0 ? null : req.Form.Files[0];
return _deviceManager.AcceptCameraUpload(deviceId, file.OpenReadStream(), new LocalFileInfo
{
MimeType = file.ContentType,
Album = album,
Name = name,
Id = id
});
}
return _deviceManager.AcceptCameraUpload(deviceId, request.RequestStream, new LocalFileInfo
{
MimeType = Request.ContentType,
Album = album,
Name = name,
Id = id
});
}
}
}