jellyfin-server/MediaBrowser.Api/Devices/DeviceService.cs

171 lines
6.1 KiB
C#
Raw Normal View History

using System.IO;
using System.Threading.Tasks;
using MediaBrowser.Controller.Configuration;
2015-09-10 18:28:22 +00:00
using MediaBrowser.Controller.Devices;
2014-10-11 20:38:13 +00:00
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
2014-10-11 20:38:13 +00:00
using MediaBrowser.Model.Devices;
2014-12-13 03:56:30 +00:00
using MediaBrowser.Model.Querying;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
using Microsoft.Extensions.Logging;
2014-10-11 20:38:13 +00:00
namespace MediaBrowser.Api.Devices
{
[Route("/Devices", "GET", Summary = "Gets all devices")]
2014-11-15 02:31:03 +00:00
[Authenticated(Roles = "Admin")]
2014-12-13 03:56:30 +00:00
public class GetDevices : DeviceQuery, IReturn<QueryResult<DeviceInfo>>
2014-10-11 20:38:13 +00:00
{
}
2018-09-12 17:26:21 +00:00
[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; }
}
2014-10-11 20:38:13 +00:00
[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")]
2014-11-15 02:31:03 +00:00
[Authenticated]
2014-10-11 20:38:13 +00:00
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")]
2014-11-15 02:31:03 +00:00
[Authenticated]
2014-10-11 20:38:13 +00:00
public class PostCameraUpload : IRequiresRequestStream, IReturnVoid
{
2018-09-12 17:26:21 +00:00
[ApiMember(Name = "DeviceId", Description = "Device Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2014-10-11 20:38:13 +00:00
public string DeviceId { get; set; }
2018-09-12 17:26:21 +00:00
[ApiMember(Name = "Album", Description = "Album", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2014-10-11 20:38:13 +00:00
public string Album { get; set; }
2018-09-12 17:26:21 +00:00
[ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2014-10-11 20:38:13 +00:00
public string Name { get; set; }
2018-09-12 17:26:21 +00:00
[ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2014-10-24 04:54:35 +00:00
public string Id { get; set; }
2014-10-11 20:38:13 +00:00
public Stream RequestStream { get; set; }
}
2014-10-13 20:14:53 +00:00
[Route("/Devices/Options", "POST", Summary = "Updates device options")]
2014-11-15 02:31:03 +00:00
[Authenticated(Roles = "Admin")]
2014-10-13 20:14:53 +00:00
public class PostDeviceOptions : DeviceOptions, IReturnVoid
{
[ApiMember(Name = "Id", Description = "Device Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string Id { get; set; }
}
2014-11-15 02:31:03 +00:00
2014-10-11 20:38:13 +00:00
public class DeviceService : BaseApiService
{
private readonly IDeviceManager _deviceManager;
2018-09-12 17:26:21 +00:00
private readonly IAuthenticationRepository _authRepo;
private readonly ISessionManager _sessionManager;
2014-10-11 20:38:13 +00:00
public DeviceService(
ILogger<DeviceService> logger,
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
IDeviceManager deviceManager,
IAuthenticationRepository authRepo,
ISessionManager sessionManager)
: base(logger, serverConfigurationManager, httpResultFactory)
2014-10-11 20:38:13 +00:00
{
_deviceManager = deviceManager;
2018-09-12 17:26:21 +00:00
_authRepo = authRepo;
_sessionManager = sessionManager;
2014-10-11 20:38:13 +00:00
}
2014-10-13 20:14:53 +00:00
public void Post(PostDeviceOptions request)
{
2018-09-12 17:26:21 +00:00
_deviceManager.UpdateDeviceOptions(request.Id, request);
2014-10-13 20:14:53 +00:00
}
2018-09-12 17:26:21 +00:00
public object Get(GetDevices request)
2014-10-13 20:14:53 +00:00
{
2018-09-12 17:26:21 +00:00
return ToOptimizedResult(_deviceManager.GetDevices(request));
2014-10-13 20:14:53 +00:00
}
2018-09-12 17:26:21 +00:00
public object Get(GetDeviceInfo request)
2014-10-13 20:14:53 +00:00
{
2018-09-12 17:26:21 +00:00
return _deviceManager.GetDevice(request.Id);
2014-10-13 20:14:53 +00:00
}
2018-09-12 17:26:21 +00:00
public object Get(GetDeviceOptions request)
2014-10-11 20:38:13 +00:00
{
2018-09-12 17:26:21 +00:00
return _deviceManager.GetDeviceOptions(request.Id);
2014-10-11 20:38:13 +00:00
}
public object Get(GetCameraUploads request)
{
return ToOptimizedResult(_deviceManager.GetCameraUploadHistory(request.DeviceId));
}
public void Delete(DeleteDevice request)
{
2018-09-12 17:26:21 +00:00
var sessions = _authRepo.Get(new AuthenticationInfoQuery
{
DeviceId = request.Id
}).Items;
foreach (var session in sessions)
{
_sessionManager.Logout(session);
}
2014-10-11 20:38:13 +00:00
}
2018-09-12 17:26:21 +00:00
public Task Post(PostCameraUpload request)
2014-10-11 20:38:13 +00:00
{
2014-10-12 01:46:02 +00:00
var deviceId = Request.QueryString["DeviceId"];
2014-10-11 20:38:13 +00:00
var album = Request.QueryString["Album"];
2014-10-24 04:54:35 +00:00
var id = Request.QueryString["Id"];
2014-10-11 20:38:13 +00:00
var name = Request.QueryString["Name"];
2019-07-28 22:33:28 +00:00
var req = Request.Response.HttpContext.Request;
2014-10-11 20:38:13 +00:00
2019-07-28 22:33:28 +00:00
if (req.HasFormContentType)
2014-10-11 20:38:13 +00:00
{
2019-07-28 22:33:28 +00:00
var file = req.Form.Files.Count == 0 ? null : req.Form.Files[0];
return _deviceManager.AcceptCameraUpload(deviceId, file.OpenReadStream(), new LocalFileInfo
2015-09-10 18:28:22 +00:00
{
2019-07-28 22:33:28 +00:00
MimeType = file.ContentType,
2015-09-10 18:28:22 +00:00
Album = album,
Name = name,
Id = id
});
}
else
{
2019-07-28 22:33:28 +00:00
return _deviceManager.AcceptCameraUpload(deviceId, request.RequestStream, new LocalFileInfo
2015-09-10 18:28:22 +00:00
{
2019-07-28 22:33:28 +00:00
MimeType = Request.ContentType,
2015-09-10 18:28:22 +00:00
Album = album,
Name = name,
Id = id
});
}
2014-10-11 20:38:13 +00:00
}
}
2015-09-20 17:34:05 +00:00
}