jellyfin-server/Jellyfin.Api/Controllers/YearsController.cs

227 lines
9.8 KiB
C#
Raw Normal View History

2020-11-21 13:26:03 +00:00
using System;
2020-06-24 19:48:22 +00:00
using System.Collections.Generic;
2020-09-05 23:11:44 +00:00
using System.ComponentModel.DataAnnotations;
2020-06-24 19:48:22 +00:00
using System.Linq;
2020-08-06 14:17:45 +00:00
using Jellyfin.Api.Constants;
2020-06-24 18:23:46 +00:00
using Jellyfin.Api.Extensions;
2020-06-24 19:48:22 +00:00
using Jellyfin.Api.Helpers;
2020-11-09 21:59:04 +00:00
using Jellyfin.Api.ModelBinders;
2020-06-24 19:48:22 +00:00
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
2021-12-20 12:31:07 +00:00
using Jellyfin.Extensions;
2020-06-24 18:23:46 +00:00
using MediaBrowser.Controller.Dto;
2020-06-24 19:48:22 +00:00
using MediaBrowser.Controller.Entities;
2020-06-24 18:23:46 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
2020-10-09 23:52:39 +00:00
using MediaBrowser.Model.Entities;
2020-06-24 18:23:46 +00:00
using MediaBrowser.Model.Querying;
2020-08-06 14:17:45 +00:00
using Microsoft.AspNetCore.Authorization;
2020-06-24 18:23:46 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
2020-06-24 19:48:22 +00:00
/// <summary>
/// Years controller.
/// </summary>
2020-08-06 14:17:45 +00:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-06-24 18:23:46 +00:00
public class YearsController : BaseJellyfinApiController
{
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
private readonly IDtoService _dtoService;
2020-06-24 19:48:22 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="YearsController"/> class.
/// </summary>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
public YearsController(
ILibraryManager libraryManager,
IUserManager userManager,
IDtoService dtoService)
{
_libraryManager = libraryManager;
_userManager = userManager;
_dtoService = dtoService;
}
2020-06-24 18:23:46 +00:00
/// <summary>
/// Get years.
/// </summary>
2020-06-24 19:48:22 +00:00
/// <param name="startIndex">Skips over a given number of items within the results. Use for paging.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
/// <param name="sortOrder">Sort Order - Ascending,Descending.</param>
/// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
2020-10-09 23:35:08 +00:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-06-24 19:48:22 +00:00
/// <param name="excludeItemTypes">Optional. If specified, results will be excluded based on item type. This allows multiple, comma delimited.</param>
/// <param name="includeItemTypes">Optional. If specified, results will be included based on item type. This allows multiple, comma delimited.</param>
/// <param name="mediaTypes">Optional. Filter by MediaType. Allows multiple, comma delimited.</param>
/// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited. Options: Album, AlbumArtist, Artist, Budget, CommunityRating, CriticRating, DateCreated, DatePlayed, PlayCount, PremiereDate, ProductionYear, SortName, Random, Revenue, Runtime.</param>
/// <param name="enableUserData">Optional. Include user data.</param>
/// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
/// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
/// <param name="userId">User Id.</param>
/// <param name="recursive">Search recursively.</param>
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <response code="200">Year query returned.</response>
/// <returns> A <see cref="QueryResult{BaseItemDto}"/> containing the year result.</returns>
2020-06-24 18:23:46 +00:00
[HttpGet]
2020-06-24 19:48:22 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-06-24 18:23:46 +00:00
public ActionResult<QueryResult<BaseItemDto>> GetYears(
[FromQuery] int? startIndex,
[FromQuery] int? limit,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] SortOrder[] sortOrder,
[FromQuery] Guid? parentId,
2020-11-09 21:59:04 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
2021-02-11 00:09:23 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] mediaTypes,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] sortBy,
2020-06-24 18:23:46 +00:00
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
2020-11-09 21:53:23 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
[FromQuery] Guid? userId,
2020-06-24 18:23:46 +00:00
[FromQuery] bool recursive = true,
2020-06-24 20:35:57 +00:00
[FromQuery] bool? enableImages = true)
2020-06-24 18:23:46 +00:00
{
2020-10-29 17:36:45 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
2020-06-24 19:48:22 +00:00
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
User? user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
2020-12-01 23:16:17 +00:00
BaseItem parentItem = _libraryManager.GetParentItem(parentId, userId);
2020-06-24 19:48:22 +00:00
var query = new InternalItemsQuery(user)
{
2021-12-12 02:31:30 +00:00
ExcludeItemTypes = excludeItemTypes,
IncludeItemTypes = includeItemTypes,
MediaTypes = mediaTypes,
2020-06-24 19:48:22 +00:00
DtoOptions = dtoOptions
};
bool Filter(BaseItem i) => FilterItem(i, excludeItemTypes, includeItemTypes, mediaTypes);
2020-06-24 19:48:22 +00:00
IList<BaseItem> items;
2020-06-24 19:48:22 +00:00
if (parentItem.IsFolder)
{
var folder = (Folder)parentItem;
if (userId.Equals(default))
2020-06-24 19:48:22 +00:00
{
items = recursive ? folder.GetRecursiveChildren(Filter) : folder.Children.Where(Filter).ToList();
2020-06-24 19:48:22 +00:00
}
else
{
items = recursive ? folder.GetRecursiveChildren(user, query).ToList() : folder.GetChildren(user, true).Where(Filter).ToList();
2020-06-24 19:48:22 +00:00
}
}
else
{
items = new[] { parentItem }.Where(Filter).ToList();
}
var extractedItems = GetAllItems(items);
2020-06-24 18:23:46 +00:00
2020-06-24 19:48:22 +00:00
var filteredItems = _libraryManager.Sort(extractedItems, user, RequestHelpers.GetOrderBy(sortBy, sortOrder));
var ibnItemsArray = filteredItems.ToList();
IEnumerable<BaseItem> ibnItems = ibnItemsArray;
if (startIndex.HasValue || limit.HasValue)
{
if (startIndex.HasValue)
{
ibnItems = ibnItems.Skip(startIndex.Value);
}
if (limit.HasValue)
{
ibnItems = ibnItems.Take(limit.Value);
}
}
var tuples = ibnItems.Select(i => new Tuple<BaseItem, List<BaseItem>>(i, new List<BaseItem>()));
var dtos = tuples.Select(i => _dtoService.GetItemByNameDto(i.Item1, dtoOptions, i.Item2, user));
2022-01-20 15:46:17 +00:00
var result = new QueryResult<BaseItemDto>(
startIndex,
ibnItemsArray.Count,
dtos.Where(i => i != null).ToArray());
2020-06-24 19:48:22 +00:00
return result;
2020-06-24 18:23:46 +00:00
}
/// <summary>
/// Gets a year.
/// </summary>
/// <param name="year">The year.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <response code="200">Year returned.</response>
/// <response code="404">Year not found.</response>
/// <returns>
/// An <see cref="OkResult"/> containing the year,
/// or a <see cref="NotFoundResult"/> if year not found.
/// </returns>
[HttpGet("{year}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-09-06 15:07:27 +00:00
public ActionResult<BaseItemDto> GetYear([FromRoute, Required] int year, [FromQuery] Guid? userId)
2020-06-24 18:23:46 +00:00
{
var item = _libraryManager.GetYear(year);
if (item == null)
{
return NotFound();
}
var dtoOptions = new DtoOptions()
.AddClientFields(Request);
if (userId.HasValue && !userId.Value.Equals(default))
2020-06-24 18:23:46 +00:00
{
var user = _userManager.GetUserById(userId.Value);
2020-06-24 18:23:46 +00:00
return _dtoService.GetBaseItemDto(item, dtoOptions, user);
}
return _dtoService.GetBaseItemDto(item, dtoOptions);
}
2020-06-24 19:48:22 +00:00
2021-02-11 00:09:23 +00:00
private bool FilterItem(BaseItem f, IReadOnlyCollection<BaseItemKind> excludeItemTypes, IReadOnlyCollection<BaseItemKind> includeItemTypes, IReadOnlyCollection<string> mediaTypes)
2020-06-24 19:48:22 +00:00
{
2021-02-11 00:09:23 +00:00
var baseItemKind = f.GetBaseItemKind();
2020-06-24 19:48:22 +00:00
// Exclude item types
2021-02-11 00:09:23 +00:00
if (excludeItemTypes.Count > 0 && excludeItemTypes.Contains(baseItemKind))
2020-06-24 19:48:22 +00:00
{
return false;
}
// Include item types
2021-02-11 00:09:23 +00:00
if (includeItemTypes.Count > 0 && !includeItemTypes.Contains(baseItemKind))
2020-06-24 19:48:22 +00:00
{
return false;
}
// Include MediaTypes
2021-12-20 12:31:07 +00:00
if (mediaTypes.Count > 0 && !mediaTypes.Contains(f.MediaType ?? string.Empty, StringComparison.OrdinalIgnoreCase))
2020-06-24 19:48:22 +00:00
{
return false;
}
return true;
}
private IEnumerable<BaseItem> GetAllItems(IEnumerable<BaseItem> items)
{
return items
.Select(i => i.ProductionYear ?? 0)
.Where(i => i > 0)
.Distinct()
.Select(year => _libraryManager.GetYear(year));
}
2020-06-24 18:23:46 +00:00
}
}