1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
#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]
[Authenticated(Roles = "Admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<DeviceInfo[]> GetDevices([FromQuery] bool? supportsSync, [FromQuery] Guid? userId)
{
var deviceQuery = new DeviceQuery { SupportsSync = supportsSync, UserId = userId ?? Guid.Empty };
var devices = _deviceManager.GetDevices(deviceQuery);
return Ok(devices);
}
/// <summary>
/// Get info for a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <returns>Device Info.</returns>
[HttpGet("Info")]
[Authenticated(Roles = "Admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult<DeviceInfo> GetDeviceInfo([FromQuery, BindRequired] string id)
{
var deviceInfo = _deviceManager.GetDevice(id);
if (deviceInfo == null)
{
return NotFound();
}
return Ok(deviceInfo);
}
/// <summary>
/// Get options for a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <returns>Device Info.</returns>
[HttpGet("Options")]
[Authenticated(Roles = "Admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult<DeviceInfo> GetDeviceOptions([FromQuery, BindRequired] string id)
{
var deviceInfo = _deviceManager.GetDeviceOptions(id);
if (deviceInfo == null)
{
return NotFound();
}
return Ok(deviceInfo);
}
/// <summary>
/// Update device options.
/// </summary>
/// <param name="id">Device Id.</param>
/// <param name="deviceOptions">Device Options.</param>
/// <returns>Status.</returns>
[HttpPost("Options")]
[Authenticated(Roles = "Admin")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult UpdateDeviceOptions(
[FromQuery, BindRequired] string id,
[FromBody, BindRequired] DeviceOptions deviceOptions)
{
var existingDeviceOptions = _deviceManager.GetDeviceOptions(id);
if (existingDeviceOptions == null)
{
return NotFound();
}
_deviceManager.UpdateDeviceOptions(id, deviceOptions);
return Ok();
}
/// <summary>
/// Deletes a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <returns>Status.</returns>
[HttpDelete]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult DeleteDevice([FromQuery, BindRequired] string id)
{
var sessions = _authenticationRepository.Get(new AuthenticationInfoQuery { DeviceId = id }).Items;
foreach (var session in sessions)
{
_sessionManager.Logout(session);
}
return Ok();
}
/// <summary>
/// Gets camera upload history for a device.
/// </summary>
/// <param name="id">Device Id.</param>
/// <returns>Content Upload History.</returns>
[HttpGet("CameraUploads")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<ContentUploadHistory> GetCameraUploads([FromQuery, BindRequired] string id)
{
var uploadHistory = _deviceManager.GetCameraUploadHistory(id);
return Ok(uploadHistory);
}
/// <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)]
public async Task<ActionResult> PostCameraUploadAsync(
[FromQuery, BindRequired] string deviceId,
[FromQuery, BindRequired] string album,
[FromQuery, BindRequired] string name,
[FromQuery, BindRequired] string id)
{
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();
}
}
}
|