jellyfin/Jellyfin.Api/Controllers/YearsController.cs

230 lines
9.8 KiB
C#
Raw Normal View History

2020-11-21 06:26:03 -07:00
using System;
2020-06-24 12:48:22 -07:00
using System.Collections.Generic;
2020-09-05 16:11:44 -07:00
using System.ComponentModel.DataAnnotations;
2020-06-24 12:48:22 -07:00
using System.Linq;
2020-08-06 07:17:45 -07:00
using Jellyfin.Api.Constants;
2020-06-24 11:23:46 -07:00
using Jellyfin.Api.Extensions;
2020-06-24 12:48:22 -07:00
using Jellyfin.Api.Helpers;
2020-11-09 14:59:04 -07:00
using Jellyfin.Api.ModelBinders;
2020-06-24 12:48:22 -07:00
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
2020-06-24 11:23:46 -07:00
using MediaBrowser.Controller.Dto;
2020-06-24 12:48:22 -07:00
using MediaBrowser.Controller.Entities;
2020-06-24 11:23:46 -07:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
2020-10-09 16:52:39 -07:00
using MediaBrowser.Model.Entities;
2020-06-24 11:23:46 -07:00
using MediaBrowser.Model.Querying;
2020-08-06 07:17:45 -07:00
using Microsoft.AspNetCore.Authorization;
2020-06-24 11:23:46 -07:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
2020-06-24 12:48:22 -07:00
/// <summary>
/// Years controller.
/// </summary>
2020-08-06 07:17:45 -07:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-06-24 11:23:46 -07:00
public class YearsController : BaseJellyfinApiController
{
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
private readonly IDtoService _dtoService;
2020-06-24 12:48:22 -07: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 11:23:46 -07:00
/// <summary>
/// Get years.
/// </summary>
2020-06-24 12:48:22 -07: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 16:35:08 -07:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-06-24 12:48:22 -07: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 11:23:46 -07:00
[HttpGet]
2020-06-24 12:48:22 -07:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-06-24 11:23:46 -07:00
public ActionResult<QueryResult<BaseItemDto>> GetYears(
[FromQuery] int? startIndex,
[FromQuery] int? limit,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] SortOrder[] sortOrder,
[FromQuery] Guid? parentId,
2020-11-09 14:59:04 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
2021-02-10 17:09:23 -07: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 11:23:46 -07:00
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
2020-11-09 14:53:23 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
[FromQuery] Guid? userId,
2020-06-24 11:23:46 -07:00
[FromQuery] bool recursive = true,
2020-06-24 13:35:57 -07:00
[FromQuery] bool? enableImages = true)
2020-06-24 11:23:46 -07:00
{
2020-10-29 10:36:45 -07:00
var dtoOptions = new DtoOptions { Fields = fields }
2020-06-24 12:48:22 -07:00
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
User? user = null;
2020-12-01 16:16:17 -07:00
BaseItem parentItem = _libraryManager.GetParentItem(parentId, userId);
2020-06-24 12:48:22 -07:00
if (userId.HasValue && !userId.Equals(Guid.Empty))
2020-06-24 12:48:22 -07:00
{
user = _userManager.GetUserById(userId.Value);
2020-06-24 12:48:22 -07:00
}
IList<BaseItem> items;
var query = new InternalItemsQuery(user)
{
2021-02-10 17:09:23 -07:00
ExcludeItemTypes = RequestHelpers.GetItemTypeStrings(excludeItemTypes),
IncludeItemTypes = RequestHelpers.GetItemTypeStrings(includeItemTypes),
MediaTypes = mediaTypes,
2020-06-24 12:48:22 -07:00
DtoOptions = dtoOptions
};
bool Filter(BaseItem i) => FilterItem(i, excludeItemTypes, includeItemTypes, mediaTypes);
2020-06-24 12:48:22 -07:00
if (parentItem.IsFolder)
{
var folder = (Folder)parentItem;
if (!userId.Equals(Guid.Empty))
{
items = recursive ? folder.GetRecursiveChildren(user, query).ToList() : folder.GetChildren(user, true).Where(Filter).ToList();
}
else
{
items = recursive ? folder.GetRecursiveChildren(Filter) : folder.Children.Where(Filter).ToList();
}
}
else
{
items = new[] { parentItem }.Where(Filter).ToList();
}
var extractedItems = GetAllItems(items);
2020-06-24 11:23:46 -07:00
2020-06-24 12:48:22 -07:00
var filteredItems = _libraryManager.Sort(extractedItems, user, RequestHelpers.GetOrderBy(sortBy, sortOrder));
var ibnItemsArray = filteredItems.ToList();
IEnumerable<BaseItem> ibnItems = ibnItemsArray;
var result = new QueryResult<BaseItemDto> { TotalRecordCount = ibnItemsArray.Count };
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));
result.Items = dtos.Where(i => i != null).ToArray();
return result;
2020-06-24 11:23:46 -07: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 08:07:27 -07:00
public ActionResult<BaseItemDto> GetYear([FromRoute, Required] int year, [FromQuery] Guid? userId)
2020-06-24 11:23:46 -07:00
{
var item = _libraryManager.GetYear(year);
if (item == null)
{
return NotFound();
}
var dtoOptions = new DtoOptions()
.AddClientFields(Request);
if (userId.HasValue && !userId.Equals(Guid.Empty))
2020-06-24 11:23:46 -07:00
{
var user = _userManager.GetUserById(userId.Value);
2020-06-24 11:23:46 -07:00
return _dtoService.GetBaseItemDto(item, dtoOptions, user);
}
return _dtoService.GetBaseItemDto(item, dtoOptions);
}
2020-06-24 12:48:22 -07:00
2021-02-10 17:09:23 -07:00
private bool FilterItem(BaseItem f, IReadOnlyCollection<BaseItemKind> excludeItemTypes, IReadOnlyCollection<BaseItemKind> includeItemTypes, IReadOnlyCollection<string> mediaTypes)
2020-06-24 12:48:22 -07:00
{
2021-02-10 17:09:23 -07:00
var baseItemKind = f.GetBaseItemKind();
2020-06-24 12:48:22 -07:00
// Exclude item types
2021-02-10 17:09:23 -07:00
if (excludeItemTypes.Count > 0 && excludeItemTypes.Contains(baseItemKind))
2020-06-24 12:48:22 -07:00
{
return false;
}
// Include item types
2021-02-10 17:09:23 -07:00
if (includeItemTypes.Count > 0 && !includeItemTypes.Contains(baseItemKind))
2020-06-24 12:48:22 -07:00
{
return false;
}
// Include MediaTypes
if (mediaTypes.Count > 0 && !mediaTypes.Contains(f.MediaType ?? string.Empty, StringComparer.OrdinalIgnoreCase))
{
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 11:23:46 -07:00
}
}