jellyfin/Jellyfin.Api/Controllers/YearsController.cs

227 lines
9.1 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-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;
2021-12-20 05:31:07 -07:00
using Jellyfin.Extensions;
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;
2023-01-31 04:18:10 -07:00
namespace Jellyfin.Api.Controllers;
/// <summary>
/// Years controller.
/// </summary>
2023-02-08 15:55:26 -07:00
[Authorize]
2023-01-31 04:18:10 -07:00
public class YearsController : BaseJellyfinApiController
2020-06-24 11:23:46 -07:00
{
2023-01-31 04:18:10 -07:00
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
private readonly IDtoService _dtoService;
2020-06-24 12:48:22 -07:00
/// <summary>
2023-01-31 04:18:10 -07:00
/// Initializes a new instance of the <see cref="YearsController"/> class.
2020-06-24 12:48:22 -07:00
/// </summary>
2023-01-31 04:18:10 -07:00
/// <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)
2020-06-24 11:23:46 -07:00
{
2023-01-31 04:18:10 -07:00
_libraryManager = libraryManager;
_userManager = userManager;
_dtoService = dtoService;
}
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
/// <summary>
/// Get years.
/// </summary>
/// <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>
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
/// <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>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<QueryResult<BaseItemDto>> GetYears(
[FromQuery] int? startIndex,
[FromQuery] int? limit,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] SortOrder[] sortOrder,
[FromQuery] Guid? parentId,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] mediaTypes,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] sortBy,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
[FromQuery] Guid? userId,
[FromQuery] bool recursive = true,
[FromQuery] bool? enableImages = true)
{
2023-02-17 15:16:08 -07:00
userId = RequestHelpers.GetUserId(User, userId);
2023-01-31 04:18:10 -07:00
var dtoOptions = new DtoOptions { Fields = fields }
.AddClientFields(User)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
2020-06-24 12:48:22 -07:00
2023-02-17 15:16:08 -07:00
User? user = userId.Value.Equals(default)
2023-01-31 04:18:10 -07:00
? null
: _userManager.GetUserById(userId.Value);
BaseItem parentItem = _libraryManager.GetParentItem(parentId, userId);
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
var query = new InternalItemsQuery(user)
{
ExcludeItemTypes = excludeItemTypes,
IncludeItemTypes = includeItemTypes,
MediaTypes = mediaTypes,
DtoOptions = dtoOptions
};
bool Filter(BaseItem i) => FilterItem(i, excludeItemTypes, includeItemTypes, mediaTypes);
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
IList<BaseItem> items;
if (parentItem.IsFolder)
{
var folder = (Folder)parentItem;
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
if (userId.Equals(default))
2020-06-24 12:48:22 -07:00
{
2023-01-31 04:18:10 -07:00
items = recursive ? folder.GetRecursiveChildren(Filter) : folder.Children.Where(Filter).ToList();
2020-06-24 12:48:22 -07:00
}
else
{
2023-01-31 04:18:10 -07:00
items = recursive ? folder.GetRecursiveChildren(user, query).ToList() : folder.GetChildren(user, true).Where(Filter).ToList();
2020-06-24 12:48:22 -07:00
}
2023-01-31 04:18:10 -07:00
}
else
{
items = new[] { parentItem }.Where(Filter).ToList();
}
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
var extractedItems = GetAllItems(items);
2020-06-24 11:23:46 -07:00
2023-01-31 04:18:10 -07:00
var filteredItems = _libraryManager.Sort(extractedItems, user, RequestHelpers.GetOrderBy(sortBy, sortOrder));
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
var ibnItemsArray = filteredItems.ToList();
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
IEnumerable<BaseItem> ibnItems = ibnItemsArray;
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
if (startIndex.HasValue || limit.HasValue)
{
if (startIndex.HasValue)
2020-06-24 12:48:22 -07:00
{
2023-01-31 04:18:10 -07:00
ibnItems = ibnItems.Skip(startIndex.Value);
2020-06-24 12:48:22 -07:00
}
2023-01-31 04:18:10 -07:00
if (limit.HasValue)
2020-06-24 11:23:46 -07:00
{
2023-01-31 04:18:10 -07:00
ibnItems = ibnItems.Take(limit.Value);
2020-06-24 11:23:46 -07:00
}
2023-01-31 04:18:10 -07:00
}
2020-06-24 11:23:46 -07:00
2023-01-31 04:18:10 -07:00
var tuples = ibnItems.Select(i => new Tuple<BaseItem, List<BaseItem>>(i, new List<BaseItem>()));
2020-06-24 11:23:46 -07:00
2023-01-31 04:18:10 -07:00
var dtos = tuples.Select(i => _dtoService.GetItemByNameDto(i.Item1, dtoOptions, i.Item2, user));
var result = new QueryResult<BaseItemDto>(
startIndex,
ibnItemsArray.Count,
dtos.Where(i => i is not null).ToArray());
return result;
}
2020-06-24 11:23:46 -07:00
2023-01-31 04:18:10 -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)]
public ActionResult<BaseItemDto> GetYear([FromRoute, Required] int year, [FromQuery] Guid? userId)
{
2023-02-17 15:16:08 -07:00
userId = RequestHelpers.GetUserId(User, userId);
2023-01-31 04:18:10 -07:00
var item = _libraryManager.GetYear(year);
if (item is null)
{
return NotFound();
2020-06-24 11:23:46 -07:00
}
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
var dtoOptions = new DtoOptions()
.AddClientFields(User);
2023-02-17 15:16:08 -07:00
if (!userId.Value.Equals(default))
2020-06-24 12:48:22 -07:00
{
2023-01-31 04:18:10 -07:00
var user = _userManager.GetUserById(userId.Value);
return _dtoService.GetBaseItemDto(item, dtoOptions, user);
}
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
return _dtoService.GetBaseItemDto(item, dtoOptions);
}
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
private bool FilterItem(BaseItem f, IReadOnlyCollection<BaseItemKind> excludeItemTypes, IReadOnlyCollection<BaseItemKind> includeItemTypes, IReadOnlyCollection<string> mediaTypes)
{
var baseItemKind = f.GetBaseItemKind();
// Exclude item types
if (excludeItemTypes.Count > 0 && excludeItemTypes.Contains(baseItemKind))
{
return false;
}
2020-06-24 12:48:22 -07:00
2023-01-31 04:18:10 -07:00
// Include item types
if (includeItemTypes.Count > 0 && !includeItemTypes.Contains(baseItemKind))
{
return false;
2020-06-24 12:48:22 -07:00
}
2023-01-31 04:18:10 -07:00
// Include MediaTypes
if (mediaTypes.Count > 0 && !mediaTypes.Contains(f.MediaType ?? string.Empty, StringComparison.OrdinalIgnoreCase))
2020-06-24 12:48:22 -07:00
{
2023-01-31 04:18:10 -07:00
return false;
2020-06-24 12:48:22 -07:00
}
2023-01-31 04:18:10 -07:00
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
}
}