2
0

ItemLookupController.cs 12 KB

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