2
0

ItemLookupController.cs 13 KB

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