DevicesController.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Controller.Devices;
  8. using MediaBrowser.Controller.Net;
  9. using MediaBrowser.Controller.Security;
  10. using MediaBrowser.Controller.Session;
  11. using MediaBrowser.Model.Devices;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.AspNetCore.Mvc.ModelBinding;
  15. namespace Jellyfin.Api.Controllers
  16. {
  17. /// <summary>
  18. /// Devices Controller.
  19. /// </summary>
  20. [Authenticated]
  21. public class DevicesController : BaseJellyfinApiController
  22. {
  23. private readonly IDeviceManager _deviceManager;
  24. private readonly IAuthenticationRepository _authenticationRepository;
  25. private readonly ISessionManager _sessionManager;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="DevicesController"/> class.
  28. /// </summary>
  29. /// <param name="deviceManager">Instance of <see cref="IDeviceManager"/> interface.</param>
  30. /// <param name="authenticationRepository">Instance of <see cref="IAuthenticationRepository"/> interface.</param>
  31. /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
  32. public DevicesController(
  33. IDeviceManager deviceManager,
  34. IAuthenticationRepository authenticationRepository,
  35. ISessionManager sessionManager)
  36. {
  37. _deviceManager = deviceManager;
  38. _authenticationRepository = authenticationRepository;
  39. _sessionManager = sessionManager;
  40. }
  41. /// <summary>
  42. /// Get Devices.
  43. /// </summary>
  44. /// <param name="supportsSync">/// Gets or sets a value indicating whether [supports synchronize].</param>
  45. /// <param name="userId">/// Gets or sets the user identifier.</param>
  46. /// <response code="200">Devices retrieved.</response>
  47. /// <returns>An <see cref="OkResult"/> containing the list of devices.</returns>
  48. [HttpGet]
  49. [Authenticated(Roles = "Admin")]
  50. [ProducesResponseType(StatusCodes.Status200OK)]
  51. public ActionResult<IEnumerable<DeviceInfo>> GetDevices([FromQuery] bool? supportsSync, [FromQuery] Guid? userId)
  52. {
  53. var deviceQuery = new DeviceQuery { SupportsSync = supportsSync, UserId = userId ?? Guid.Empty };
  54. var devices = _deviceManager.GetDevices(deviceQuery);
  55. return Ok(devices);
  56. }
  57. /// <summary>
  58. /// Get info for a device.
  59. /// </summary>
  60. /// <param name="id">Device Id.</param>
  61. /// <response code="200">Device info retrieved.</response>
  62. /// <response code="404">Device not found.</response>
  63. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  64. [HttpGet("Info")]
  65. [Authenticated(Roles = "Admin")]
  66. [ProducesResponseType(StatusCodes.Status200OK)]
  67. [ProducesResponseType(StatusCodes.Status404NotFound)]
  68. public ActionResult<DeviceInfo> GetDeviceInfo([FromQuery, BindRequired] string id)
  69. {
  70. var deviceInfo = _deviceManager.GetDevice(id);
  71. if (deviceInfo == null)
  72. {
  73. return NotFound();
  74. }
  75. return deviceInfo;
  76. }
  77. /// <summary>
  78. /// Get options for a device.
  79. /// </summary>
  80. /// <param name="id">Device Id.</param>
  81. /// <response code="200">Device options retrieved.</response>
  82. /// <response code="404">Device not found.</response>
  83. /// <returns>An <see cref="OkResult"/> containing the device info on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  84. [HttpGet("Options")]
  85. [Authenticated(Roles = "Admin")]
  86. [ProducesResponseType(StatusCodes.Status200OK)]
  87. [ProducesResponseType(StatusCodes.Status404NotFound)]
  88. public ActionResult<DeviceOptions> GetDeviceOptions([FromQuery, BindRequired] string id)
  89. {
  90. var deviceInfo = _deviceManager.GetDeviceOptions(id);
  91. if (deviceInfo == null)
  92. {
  93. return NotFound();
  94. }
  95. return deviceInfo;
  96. }
  97. /// <summary>
  98. /// Update device options.
  99. /// </summary>
  100. /// <param name="id">Device Id.</param>
  101. /// <param name="deviceOptions">Device Options.</param>
  102. /// <response code="200">Device options updated.</response>
  103. /// <response code="404">Device not found.</response>
  104. /// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  105. [HttpPost("Options")]
  106. [Authenticated(Roles = "Admin")]
  107. [ProducesResponseType(StatusCodes.Status200OK)]
  108. [ProducesResponseType(StatusCodes.Status404NotFound)]
  109. public ActionResult UpdateDeviceOptions(
  110. [FromQuery, BindRequired] string id,
  111. [FromBody, BindRequired] DeviceOptions deviceOptions)
  112. {
  113. var existingDeviceOptions = _deviceManager.GetDeviceOptions(id);
  114. if (existingDeviceOptions == null)
  115. {
  116. return NotFound();
  117. }
  118. _deviceManager.UpdateDeviceOptions(id, deviceOptions);
  119. return Ok();
  120. }
  121. /// <summary>
  122. /// Deletes a device.
  123. /// </summary>
  124. /// <param name="id">Device Id.</param>
  125. /// <response code="200">Device deleted.</response>
  126. /// <response code="404">Device not found.</response>
  127. /// <returns>An <see cref="OkResult"/> on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  128. [HttpDelete]
  129. [ProducesResponseType(StatusCodes.Status200OK)]
  130. public ActionResult DeleteDevice([FromQuery, BindRequired] string id)
  131. {
  132. var existingDevice = _deviceManager.GetDevice(id);
  133. if (existingDevice == null)
  134. {
  135. return NotFound();
  136. }
  137. var sessions = _authenticationRepository.Get(new AuthenticationInfoQuery { DeviceId = id }).Items;
  138. foreach (var session in sessions)
  139. {
  140. _sessionManager.Logout(session);
  141. }
  142. return Ok();
  143. }
  144. /// <summary>
  145. /// Gets camera upload history for a device.
  146. /// </summary>
  147. /// <param name="id">Device Id.</param>
  148. /// <response code="200">Device upload history retrieved.</response>
  149. /// <response code="404">Device not found.</response>
  150. /// <returns>An <see cref="OkResult"/> containing the device upload history on success, or a <see cref="NotFoundResult"/> if the device could not be found.</returns>
  151. [HttpGet("CameraUploads")]
  152. [ProducesResponseType(StatusCodes.Status200OK)]
  153. public ActionResult<ContentUploadHistory> GetCameraUploads([FromQuery, BindRequired] string id)
  154. {
  155. var existingDevice = _deviceManager.GetDevice(id);
  156. if (existingDevice == null)
  157. {
  158. return NotFound();
  159. }
  160. var uploadHistory = _deviceManager.GetCameraUploadHistory(id);
  161. return uploadHistory;
  162. }
  163. /// <summary>
  164. /// Uploads content.
  165. /// </summary>
  166. /// <param name="deviceId">Device Id.</param>
  167. /// <param name="album">Album.</param>
  168. /// <param name="name">Name.</param>
  169. /// <param name="id">Id.</param>
  170. /// <response code="200">Contents uploaded.</response>
  171. /// <response code="400">No uploaded contents.</response>
  172. /// <response code="404">Device not found.</response>
  173. /// <returns>
  174. /// An <see cref="OkResult"/> on success,
  175. /// or a <see cref="NotFoundResult"/> if the device could not be found
  176. /// or a <see cref="BadRequestResult"/> if the upload contains no files.
  177. /// </returns>
  178. [HttpPost("CameraUploads")]
  179. [ProducesResponseType(StatusCodes.Status200OK)]
  180. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  181. public async Task<ActionResult> PostCameraUploadAsync(
  182. [FromQuery, BindRequired] string deviceId,
  183. [FromQuery, BindRequired] string album,
  184. [FromQuery, BindRequired] string name,
  185. [FromQuery, BindRequired] string id)
  186. {
  187. var existingDevice = _deviceManager.GetDevice(id);
  188. if (existingDevice == null)
  189. {
  190. return NotFound();
  191. }
  192. Stream fileStream;
  193. string contentType;
  194. if (Request.HasFormContentType)
  195. {
  196. if (Request.Form.Files.Any())
  197. {
  198. fileStream = Request.Form.Files[0].OpenReadStream();
  199. contentType = Request.Form.Files[0].ContentType;
  200. }
  201. else
  202. {
  203. return BadRequest();
  204. }
  205. }
  206. else
  207. {
  208. fileStream = Request.Body;
  209. contentType = Request.ContentType;
  210. }
  211. await _deviceManager.AcceptCameraUpload(
  212. deviceId,
  213. fileStream,
  214. new LocalFileInfo { MimeType = contentType, Album = album, Name = name, Id = id }).ConfigureAwait(false);
  215. return Ok();
  216. }
  217. }
  218. }