ItemLookupController.cs 12 KB

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