ItemLookupController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Text.Json;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Jellyfin.Api.Constants;
  8. using MediaBrowser.Controller;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Entities.Audio;
  12. using MediaBrowser.Controller.Entities.Movies;
  13. using MediaBrowser.Controller.Entities.TV;
  14. using MediaBrowser.Controller.Library;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Model.Providers;
  18. using Microsoft.AspNetCore.Authorization;
  19. using Microsoft.AspNetCore.Http;
  20. using Microsoft.AspNetCore.Mvc;
  21. using Microsoft.Extensions.Logging;
  22. namespace Jellyfin.Api.Controllers
  23. {
  24. /// <summary>
  25. /// Item lookup controller.
  26. /// </summary>
  27. [Route("")]
  28. [Authorize(Policy = Policies.DefaultAuthorization)]
  29. public class ItemLookupController : BaseJellyfinApiController
  30. {
  31. private readonly IProviderManager _providerManager;
  32. private readonly IServerApplicationPaths _appPaths;
  33. private readonly IFileSystem _fileSystem;
  34. private readonly ILibraryManager _libraryManager;
  35. private readonly ILogger<ItemLookupController> _logger;
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="ItemLookupController"/> class.
  38. /// </summary>
  39. /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
  40. /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  41. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  42. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  43. /// <param name="logger">Instance of the <see cref="ILogger{ItemLookupController}"/> interface.</param>
  44. public ItemLookupController(
  45. IProviderManager providerManager,
  46. IServerConfigurationManager serverConfigurationManager,
  47. IFileSystem fileSystem,
  48. ILibraryManager libraryManager,
  49. ILogger<ItemLookupController> logger)
  50. {
  51. _providerManager = providerManager;
  52. _appPaths = serverConfigurationManager.ApplicationPaths;
  53. _fileSystem = fileSystem;
  54. _libraryManager = libraryManager;
  55. _logger = logger;
  56. }
  57. /// <summary>
  58. /// Get the item's external id info.
  59. /// </summary>
  60. /// <param name="itemId">Item id.</param>
  61. /// <response code="200">External id info retrieved.</response>
  62. /// <response code="404">Item not found.</response>
  63. /// <returns>List of external id info.</returns>
  64. [HttpGet("Items/{itemId}/ExternalIdInfos")]
  65. [Authorize(Policy = Policies.RequiresElevation)]
  66. [ProducesResponseType(StatusCodes.Status200OK)]
  67. [ProducesResponseType(StatusCodes.Status404NotFound)]
  68. public ActionResult<IEnumerable<ExternalIdInfo>> GetExternalIdInfos([FromRoute, Required] Guid itemId)
  69. {
  70. var item = _libraryManager.GetItemById(itemId);
  71. if (item == null)
  72. {
  73. return NotFound();
  74. }
  75. return Ok(_providerManager.GetExternalIdInfos(item));
  76. }
  77. /// <summary>
  78. /// Get movie remote search.
  79. /// </summary>
  80. /// <param name="query">Remote search query.</param>
  81. /// <response code="200">Movie remote search executed.</response>
  82. /// <returns>
  83. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  84. /// The task result contains an <see cref="OkResult"/> containing the list of remote search results.
  85. /// </returns>
  86. [HttpPost("Items/RemoteSearch/Movie")]
  87. public async Task<ActionResult<IEnumerable<RemoteSearchResult>>> GetMovieRemoteSearchResults([FromBody, Required] RemoteSearchQuery<MovieInfo> query)
  88. {
  89. var results = await _providerManager.GetRemoteSearchResults<Movie, MovieInfo>(query, CancellationToken.None)
  90. .ConfigureAwait(false);
  91. return Ok(results);
  92. }
  93. /// <summary>
  94. /// Get trailer remote search.
  95. /// </summary>
  96. /// <param name="query">Remote search query.</param>
  97. /// <response code="200">Trailer remote search executed.</response>
  98. /// <returns>
  99. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  100. /// The task result contains an <see cref="OkResult"/> containing the list of remote search results.
  101. /// </returns>
  102. [HttpPost("Items/RemoteSearch/Trailer")]
  103. public async Task<ActionResult<IEnumerable<RemoteSearchResult>>> GetTrailerRemoteSearchResults([FromBody, Required] RemoteSearchQuery<TrailerInfo> query)
  104. {
  105. var results = await _providerManager.GetRemoteSearchResults<Trailer, TrailerInfo>(query, CancellationToken.None)
  106. .ConfigureAwait(false);
  107. return Ok(results);
  108. }
  109. /// <summary>
  110. /// Get music video remote search.
  111. /// </summary>
  112. /// <param name="query">Remote search query.</param>
  113. /// <response code="200">Music video remote search executed.</response>
  114. /// <returns>
  115. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  116. /// The task result contains an <see cref="OkResult"/> containing the list of remote search results.
  117. /// </returns>
  118. [HttpPost("Items/RemoteSearch/MusicVideo")]
  119. public async Task<ActionResult<IEnumerable<RemoteSearchResult>>> GetMusicVideoRemoteSearchResults([FromBody, Required] RemoteSearchQuery<MusicVideoInfo> query)
  120. {
  121. var results = await _providerManager.GetRemoteSearchResults<MusicVideo, MusicVideoInfo>(query, CancellationToken.None)
  122. .ConfigureAwait(false);
  123. return Ok(results);
  124. }
  125. /// <summary>
  126. /// Get series remote search.
  127. /// </summary>
  128. /// <param name="query">Remote search query.</param>
  129. /// <response code="200">Series remote search executed.</response>
  130. /// <returns>
  131. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  132. /// The task result contains an <see cref="OkResult"/> containing the list of remote search results.
  133. /// </returns>
  134. [HttpPost("Items/RemoteSearch/Series")]
  135. public async Task<ActionResult<IEnumerable<RemoteSearchResult>>> GetSeriesRemoteSearchResults([FromBody, Required] RemoteSearchQuery<SeriesInfo> query)
  136. {
  137. var results = await _providerManager.GetRemoteSearchResults<Series, SeriesInfo>(query, CancellationToken.None)
  138. .ConfigureAwait(false);
  139. return Ok(results);
  140. }
  141. /// <summary>
  142. /// Get box set remote search.
  143. /// </summary>
  144. /// <param name="query">Remote search query.</param>
  145. /// <response code="200">Box set remote search executed.</response>
  146. /// <returns>
  147. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  148. /// The task result contains an <see cref="OkResult"/> containing the list of remote search results.
  149. /// </returns>
  150. [HttpPost("Items/RemoteSearch/BoxSet")]
  151. public async Task<ActionResult<IEnumerable<RemoteSearchResult>>> GetBoxSetRemoteSearchResults([FromBody, Required] RemoteSearchQuery<BoxSetInfo> query)
  152. {
  153. var results = await _providerManager.GetRemoteSearchResults<BoxSet, BoxSetInfo>(query, CancellationToken.None)
  154. .ConfigureAwait(false);
  155. return Ok(results);
  156. }
  157. /// <summary>
  158. /// Get music artist remote search.
  159. /// </summary>
  160. /// <param name="query">Remote search query.</param>
  161. /// <response code="200">Music artist remote search executed.</response>
  162. /// <returns>
  163. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  164. /// The task result contains an <see cref="OkResult"/> containing the list of remote search results.
  165. /// </returns>
  166. [HttpPost("Items/RemoteSearch/MusicArtist")]
  167. public async Task<ActionResult<IEnumerable<RemoteSearchResult>>> GetMusicArtistRemoteSearchResults([FromBody, Required] RemoteSearchQuery<ArtistInfo> query)
  168. {
  169. var results = await _providerManager.GetRemoteSearchResults<MusicArtist, ArtistInfo>(query, CancellationToken.None)
  170. .ConfigureAwait(false);
  171. return Ok(results);
  172. }
  173. /// <summary>
  174. /// Get music album remote search.
  175. /// </summary>
  176. /// <param name="query">Remote search query.</param>
  177. /// <response code="200">Music album remote search executed.</response>
  178. /// <returns>
  179. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  180. /// The task result contains an <see cref="OkResult"/> containing the list of remote search results.
  181. /// </returns>
  182. [HttpPost("Items/RemoteSearch/MusicAlbum")]
  183. public async Task<ActionResult<IEnumerable<RemoteSearchResult>>> GetMusicAlbumRemoteSearchResults([FromBody, Required] RemoteSearchQuery<AlbumInfo> query)
  184. {
  185. var results = await _providerManager.GetRemoteSearchResults<MusicAlbum, AlbumInfo>(query, CancellationToken.None)
  186. .ConfigureAwait(false);
  187. return Ok(results);
  188. }
  189. /// <summary>
  190. /// Get person remote search.
  191. /// </summary>
  192. /// <param name="query">Remote search query.</param>
  193. /// <response code="200">Person remote search executed.</response>
  194. /// <returns>
  195. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  196. /// The task result contains an <see cref="OkResult"/> containing the list of remote search results.
  197. /// </returns>
  198. [HttpPost("Items/RemoteSearch/Person")]
  199. [Authorize(Policy = Policies.RequiresElevation)]
  200. public async Task<ActionResult<IEnumerable<RemoteSearchResult>>> GetPersonRemoteSearchResults([FromBody, Required] RemoteSearchQuery<PersonLookupInfo> query)
  201. {
  202. var results = await _providerManager.GetRemoteSearchResults<Person, PersonLookupInfo>(query, CancellationToken.None)
  203. .ConfigureAwait(false);
  204. return Ok(results);
  205. }
  206. /// <summary>
  207. /// Get book remote search.
  208. /// </summary>
  209. /// <param name="query">Remote search query.</param>
  210. /// <response code="200">Book remote search executed.</response>
  211. /// <returns>
  212. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  213. /// The task result contains an <see cref="OkResult"/> containing the list of remote search results.
  214. /// </returns>
  215. [HttpPost("Items/RemoteSearch/Book")]
  216. public async Task<ActionResult<IEnumerable<RemoteSearchResult>>> GetBookRemoteSearchResults([FromBody, Required] RemoteSearchQuery<BookInfo> query)
  217. {
  218. var results = await _providerManager.GetRemoteSearchResults<Book, BookInfo>(query, CancellationToken.None)
  219. .ConfigureAwait(false);
  220. return Ok(results);
  221. }
  222. /// <summary>
  223. /// Applies search criteria to an item and refreshes metadata.
  224. /// </summary>
  225. /// <param name="itemId">Item id.</param>
  226. /// <param name="searchResult">The remote search result.</param>
  227. /// <param name="replaceAllImages">Optional. Whether or not to replace all images. Default: True.</param>
  228. /// <response code="204">Item metadata refreshed.</response>
  229. /// <returns>
  230. /// A <see cref="Task" /> that represents the asynchronous operation to get the remote search results.
  231. /// The task result contains an <see cref="NoContentResult"/>.
  232. /// </returns>
  233. [HttpPost("Items/RemoteSearch/Apply/{itemId}")]
  234. [Authorize(Policy = Policies.RequiresElevation)]
  235. [ProducesResponseType(StatusCodes.Status204NoContent)]
  236. public async Task<ActionResult> ApplySearchCriteria(
  237. [FromRoute, Required] Guid itemId,
  238. [FromBody, Required] RemoteSearchResult searchResult,
  239. [FromQuery] bool replaceAllImages = true)
  240. {
  241. var item = _libraryManager.GetItemById(itemId);
  242. _logger.LogInformation(
  243. "Setting provider id's to item {0}-{1}: {2}",
  244. item.Id,
  245. item.Name,
  246. JsonSerializer.Serialize(searchResult.ProviderIds));
  247. // Since the refresh process won't erase provider Ids, we need to set this explicitly now.
  248. item.ProviderIds = searchResult.ProviderIds;
  249. await _providerManager.RefreshFullItem(
  250. item,
  251. new MetadataRefreshOptions(new DirectoryService(_fileSystem))
  252. {
  253. MetadataRefreshMode = MetadataRefreshMode.FullRefresh,
  254. ImageRefreshMode = MetadataRefreshMode.FullRefresh,
  255. ReplaceAllMetadata = true,
  256. ReplaceAllImages = replaceAllImages,
  257. SearchResult = searchResult
  258. }, CancellationToken.None).ConfigureAwait(false);
  259. return NoContent();
  260. }
  261. }
  262. }