DevicesController.cs 7.2 KB

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