ItemLookupController.cs 12 KB

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