using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Search;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
///
/// Search controller.
///
[Route("/Search/Hints")]
[Authenticated]
public class SearchController : BaseJellyfinApiController
{
private readonly ISearchEngine _searchEngine;
private readonly ILibraryManager _libraryManager;
private readonly IDtoService _dtoService;
private readonly IImageProcessor _imageProcessor;
///
/// Initializes a new instance of the class.
///
/// Instance of interface.
/// Instance of interface.
/// Instance of interface.
/// Instance of interface.
public SearchController(
ISearchEngine searchEngine,
ILibraryManager libraryManager,
IDtoService dtoService,
IImageProcessor imageProcessor)
{
_searchEngine = searchEngine;
_libraryManager = libraryManager;
_dtoService = dtoService;
_imageProcessor = imageProcessor;
}
///
/// Gets the search hint result.
///
/// The record index to start at. All items with a lower index will be dropped from the results.
/// The maximum number of records to return.
/// Supply a user id to search within a user's library or omit to search all.
/// The search term to filter on.
/// Optional filter whether to include people.
/// Optional filter whether to include media.
/// Optional filter whether to include genres.
/// Optional filter whether to include studios.
/// Optional filter whether to include artists.
/// If specified, only results with the specified item types are returned. This allows multiple, comma delimeted.
/// If specified, results with these item types are filtered out. This allows multiple, comma delimeted.
/// If specified, only results with the specified media types are returned. This allows multiple, comma delimeted.
/// If specified, only children of the parent are returned.
/// Optional filter for movies.
/// Optional filter for series.
/// Optional filter for news.
/// Optional filter for kids.
/// Optional filter for sports.
/// An with the results of the search.
[HttpGet]
[Description("Gets search hints based on a search term")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult Get(
[FromQuery] int? startIndex,
[FromQuery] int? limit,
[FromQuery] Guid userId,
[FromQuery, Required] string searchTerm,
[FromQuery] bool includePeople,
[FromQuery] bool includeMedia,
[FromQuery] bool includeGenres,
[FromQuery] bool includeStudios,
[FromQuery] bool includeArtists,
[FromQuery] string includeItemTypes,
[FromQuery] string excludeItemTypes,
[FromQuery] string mediaTypes,
[FromQuery] string parentId,
[FromQuery] bool? isMovie,
[FromQuery] bool? isSeries,
[FromQuery] bool? isNews,
[FromQuery] bool? isKids,
[FromQuery] bool? isSports)
{
var result = _searchEngine.GetSearchHints(new SearchQuery
{
Limit = limit,
SearchTerm = searchTerm,
IncludeArtists = includeArtists,
IncludeGenres = includeGenres,
IncludeMedia = includeMedia,
IncludePeople = includePeople,
IncludeStudios = includeStudios,
StartIndex = startIndex,
UserId = userId,
IncludeItemTypes = Split(includeItemTypes, ',', true),
ExcludeItemTypes = Split(excludeItemTypes, ',', true),
MediaTypes = Split(mediaTypes, ',', true),
ParentId = parentId,
IsKids = isKids,
IsMovie = isMovie,
IsNews = isNews,
IsSeries = isSeries,
IsSports = isSports
});
return Ok(new SearchHintResult
{
TotalRecordCount = result.TotalRecordCount,
SearchHints = result.Items.Select(GetSearchHintResult).ToArray()
});
}
///
/// Gets the search hint result.
///
/// The hint info.
/// SearchHintResult.
private SearchHint GetSearchHintResult(SearchHintInfo hintInfo)
{
var item = hintInfo.Item;
var result = new SearchHint
{
Name = item.Name,
IndexNumber = item.IndexNumber,
ParentIndexNumber = item.ParentIndexNumber,
Id = item.Id,
Type = item.GetClientTypeName(),
MediaType = item.MediaType,
MatchedTerm = hintInfo.MatchedTerm,
RunTimeTicks = item.RunTimeTicks,
ProductionYear = item.ProductionYear,
ChannelId = item.ChannelId,
EndDate = item.EndDate
};
// legacy
result.ItemId = result.Id;
if (item.IsFolder)
{
result.IsFolder = true;
}
var primaryImageTag = _imageProcessor.GetImageCacheTag(item, ImageType.Primary);
if (primaryImageTag != null)
{
result.PrimaryImageTag = primaryImageTag;
result.PrimaryImageAspectRatio = _dtoService.GetPrimaryImageAspectRatio(item);
}
SetThumbImageInfo(result, item);
SetBackdropImageInfo(result, item);
switch (item)
{
case IHasSeries hasSeries:
result.Series = hasSeries.SeriesName;
break;
case LiveTvProgram program:
result.StartDate = program.StartDate;
break;
case Series series:
if (series.Status.HasValue)
{
result.Status = series.Status.Value.ToString();
}
break;
case MusicAlbum album:
result.Artists = album.Artists;
result.AlbumArtist = album.AlbumArtist;
break;
case Audio song:
result.AlbumArtist = song.AlbumArtists?[0];
result.Artists = song.Artists;
MusicAlbum musicAlbum = song.AlbumEntity;
if (musicAlbum != null)
{
result.Album = musicAlbum.Name;
result.AlbumId = musicAlbum.Id;
}
else
{
result.Album = song.Album;
}
break;
}
if (!item.ChannelId.Equals(Guid.Empty))
{
var channel = _libraryManager.GetItemById(item.ChannelId);
result.ChannelName = channel?.Name;
}
return result;
}
private void SetThumbImageInfo(SearchHint hint, BaseItem item)
{
var itemWithImage = item.HasImage(ImageType.Thumb) ? item : null;
if (itemWithImage == null && item is Episode)
{
itemWithImage = GetParentWithImage(item, ImageType.Thumb);
}
if (itemWithImage == null)
{
itemWithImage = GetParentWithImage(item, ImageType.Thumb);
}
if (itemWithImage != null)
{
var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Thumb);
if (tag != null)
{
hint.ThumbImageTag = tag;
hint.ThumbImageItemId = itemWithImage.Id.ToString("N", CultureInfo.InvariantCulture);
}
}
}
private void SetBackdropImageInfo(SearchHint hint, BaseItem item)
{
var itemWithImage = (item.HasImage(ImageType.Backdrop) ? item : null)
?? GetParentWithImage(item, ImageType.Backdrop);
if (itemWithImage != null)
{
var tag = _imageProcessor.GetImageCacheTag(itemWithImage, ImageType.Backdrop);
if (tag != null)
{
hint.BackdropImageTag = tag;
hint.BackdropImageItemId = itemWithImage.Id.ToString("N", CultureInfo.InvariantCulture);
}
}
}
private T GetParentWithImage(BaseItem item, ImageType type)
where T : BaseItem
{
return item.GetParents().OfType().FirstOrDefault(i => i.HasImage(type));
}
}
}