mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-06-04 22:24:35 -04:00
WIP migration sqlite item repository to efcore
This commit is contained in:
parent
ea81db67f4
commit
6acd146d17
File diff suppressed because it is too large
Load Diff
133
Jellyfin.Data/Entities/PeopleKind.cs
Normal file
133
Jellyfin.Data/Entities/PeopleKind.cs
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
namespace Jellyfin.Data.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The person kind.
|
||||||
|
/// </summary>
|
||||||
|
public enum PeopleKind
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An unknown person kind.
|
||||||
|
/// </summary>
|
||||||
|
Unknown,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person whose profession is acting on the stage, in films, or on television.
|
||||||
|
/// </summary>
|
||||||
|
Actor,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who supervises the actors and other staff in a film, play, or similar production.
|
||||||
|
/// </summary>
|
||||||
|
Director,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who writes music, especially as a professional occupation.
|
||||||
|
/// </summary>
|
||||||
|
Composer,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A writer of a book, article, or document. Can also be used as a generic term for music writer if there is a lack of specificity.
|
||||||
|
/// </summary>
|
||||||
|
Writer,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A well-known actor or other performer who appears in a work in which they do not have a regular role.
|
||||||
|
/// </summary>
|
||||||
|
GuestStar,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person responsible for the financial and managerial aspects of the making of a film or broadcast or for staging a play, opera, etc.
|
||||||
|
/// </summary>
|
||||||
|
Producer,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who directs the performance of an orchestra or choir.
|
||||||
|
/// </summary>
|
||||||
|
Conductor,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who writes the words to a song or musical.
|
||||||
|
/// </summary>
|
||||||
|
Lyricist,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who adapts a musical composition for performance.
|
||||||
|
/// </summary>
|
||||||
|
Arranger,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An audio engineer who performed a general engineering role.
|
||||||
|
/// </summary>
|
||||||
|
Engineer,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An engineer responsible for using a mixing console to mix a recorded track into a single piece of music suitable for release.
|
||||||
|
/// </summary>
|
||||||
|
Mixer,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who remixed a recording by taking one or more other tracks, substantially altering them and mixing them together with other material.
|
||||||
|
/// </summary>
|
||||||
|
Remixer,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who created the material.
|
||||||
|
/// </summary>
|
||||||
|
Creator,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who was the artist.
|
||||||
|
/// </summary>
|
||||||
|
Artist,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who was the album artist.
|
||||||
|
/// </summary>
|
||||||
|
AlbumArtist,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who was the author.
|
||||||
|
/// </summary>
|
||||||
|
Author,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who was the illustrator.
|
||||||
|
/// </summary>
|
||||||
|
Illustrator,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person responsible for drawing the art.
|
||||||
|
/// </summary>
|
||||||
|
Penciller,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person responsible for inking the pencil art.
|
||||||
|
/// </summary>
|
||||||
|
Inker,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person responsible for applying color to drawings.
|
||||||
|
/// </summary>
|
||||||
|
Colorist,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person responsible for drawing text and speech bubbles.
|
||||||
|
/// </summary>
|
||||||
|
Letterer,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person responsible for drawing the cover art.
|
||||||
|
/// </summary>
|
||||||
|
CoverArtist,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person contributing to a resource by revising or elucidating the content, e.g., adding an introduction, notes, or other critical matter.
|
||||||
|
/// An editor may also prepare a resource for production, publication, or distribution.
|
||||||
|
/// </summary>
|
||||||
|
Editor,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A person who renders a text from one language into another.
|
||||||
|
/// </summary>
|
||||||
|
Translator
|
||||||
|
}
|
@ -19,10 +19,12 @@ using MediaBrowser.Controller.Entities.TV;
|
|||||||
using MediaBrowser.Controller.LiveTv;
|
using MediaBrowser.Controller.LiveTv;
|
||||||
using MediaBrowser.Controller.Persistence;
|
using MediaBrowser.Controller.Persistence;
|
||||||
using MediaBrowser.Controller.Playlists;
|
using MediaBrowser.Controller.Playlists;
|
||||||
|
using MediaBrowser.Model.Dto;
|
||||||
using MediaBrowser.Model.Entities;
|
using MediaBrowser.Model.Entities;
|
||||||
using MediaBrowser.Model.LiveTv;
|
using MediaBrowser.Model.LiveTv;
|
||||||
using MediaBrowser.Model.Querying;
|
using MediaBrowser.Model.Querying;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Internal;
|
||||||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
|
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
|
||||||
using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem;
|
using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem;
|
||||||
using BaseItemEntity = Jellyfin.Data.Entities.BaseItem;
|
using BaseItemEntity = Jellyfin.Data.Entities.BaseItem;
|
||||||
@ -145,8 +147,72 @@ public class BaseItemManager : IItemRepository
|
|||||||
_appHost = appHost;
|
_appHost = appHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IQueryable<T> Pageinate<T>(IQueryable<T> query, InternalItemsQuery filter)
|
private QueryResult<(BaseItemDto Item, ItemCounts ItemCounts)> GetItemValues(InternalItemsQuery filter, int[] itemValueTypes, string returnType)
|
||||||
{
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(filter);
|
||||||
|
|
||||||
|
if (!filter.Limit.HasValue)
|
||||||
|
{
|
||||||
|
filter.EnableTotalRecordCount = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var context = _dbProvider.CreateDbContext();
|
||||||
|
|
||||||
|
var innerQuery = new InternalItemsQuery(filter.User)
|
||||||
|
{
|
||||||
|
ExcludeItemTypes = filter.ExcludeItemTypes,
|
||||||
|
IncludeItemTypes = filter.IncludeItemTypes,
|
||||||
|
MediaTypes = filter.MediaTypes,
|
||||||
|
AncestorIds = filter.AncestorIds,
|
||||||
|
ItemIds = filter.ItemIds,
|
||||||
|
TopParentIds = filter.TopParentIds,
|
||||||
|
ParentId = filter.ParentId,
|
||||||
|
IsAiring = filter.IsAiring,
|
||||||
|
IsMovie = filter.IsMovie,
|
||||||
|
IsSports = filter.IsSports,
|
||||||
|
IsKids = filter.IsKids,
|
||||||
|
IsNews = filter.IsNews,
|
||||||
|
IsSeries = filter.IsSeries
|
||||||
|
};
|
||||||
|
var query = TranslateQuery(context.BaseItems, context, innerQuery);
|
||||||
|
|
||||||
|
query = query.Where(e => e.Type == returnType && e.ItemValues!.Any(f => e.CleanName == f.CleanValue && itemValueTypes.Contains(f.Type)));
|
||||||
|
|
||||||
|
var outerQuery = new InternalItemsQuery(filter.User)
|
||||||
|
{
|
||||||
|
IsPlayed = filter.IsPlayed,
|
||||||
|
IsFavorite = filter.IsFavorite,
|
||||||
|
IsFavoriteOrLiked = filter.IsFavoriteOrLiked,
|
||||||
|
IsLiked = filter.IsLiked,
|
||||||
|
IsLocked = filter.IsLocked,
|
||||||
|
NameLessThan = filter.NameLessThan,
|
||||||
|
NameStartsWith = filter.NameStartsWith,
|
||||||
|
NameStartsWithOrGreater = filter.NameStartsWithOrGreater,
|
||||||
|
Tags = filter.Tags,
|
||||||
|
OfficialRatings = filter.OfficialRatings,
|
||||||
|
StudioIds = filter.StudioIds,
|
||||||
|
GenreIds = filter.GenreIds,
|
||||||
|
Genres = filter.Genres,
|
||||||
|
Years = filter.Years,
|
||||||
|
NameContains = filter.NameContains,
|
||||||
|
SearchTerm = filter.SearchTerm,
|
||||||
|
SimilarTo = filter.SimilarTo,
|
||||||
|
ExcludeItemIds = filter.ExcludeItemIds
|
||||||
|
};
|
||||||
|
query = TranslateQuery(query, context, outerQuery)
|
||||||
|
.OrderBy(e => e.PresentationUniqueKey);
|
||||||
|
|
||||||
|
if (filter.OrderBy.Count != 0
|
||||||
|
|| filter.SimilarTo is not null
|
||||||
|
|| !string.IsNullOrEmpty(filter.SearchTerm))
|
||||||
|
{
|
||||||
|
query = ApplyOrder(query, filter);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
query = query.OrderBy(e => e.SortName);
|
||||||
|
}
|
||||||
|
|
||||||
if (filter.Limit.HasValue || filter.StartIndex.HasValue)
|
if (filter.Limit.HasValue || filter.StartIndex.HasValue)
|
||||||
{
|
{
|
||||||
var offset = filter.StartIndex ?? 0;
|
var offset = filter.StartIndex ?? 0;
|
||||||
@ -158,131 +224,96 @@ public class BaseItemManager : IItemRepository
|
|||||||
|
|
||||||
if (filter.Limit.HasValue)
|
if (filter.Limit.HasValue)
|
||||||
{
|
{
|
||||||
query = query.Take(filter.Limit.Value);
|
query.Take(filter.Limit.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return query;
|
var result = new QueryResult<(BaseItem, ItemCounts)>();
|
||||||
|
string countText = string.Empty;
|
||||||
|
if (filter.EnableTotalRecordCount)
|
||||||
|
{
|
||||||
|
result.TotalRecordCount = query.DistinctBy(e => e.PresentationUniqueKey).Count();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Expression<Func<BaseItemEntity, object>> MapOrderByField(ItemSortBy sortBy, InternalItemsQuery query)
|
var resultQuery = query.Select(e => new
|
||||||
{
|
{
|
||||||
#pragma warning disable CS8603 // Possible null reference return.
|
item = e,
|
||||||
return sortBy switch
|
itemCount = new ItemCounts()
|
||||||
{
|
{
|
||||||
ItemSortBy.AirTime => e => e.SortName, // TODO
|
SeriesCount = e.ItemValues!.Count(e => e.Type == (int)BaseItemKind.Series),
|
||||||
ItemSortBy.Runtime => e => e.RunTimeTicks,
|
EpisodeCount = e.ItemValues!.Count(e => e.Type == (int)BaseItemKind.Episode),
|
||||||
ItemSortBy.Random => e => EF.Functions.Random(),
|
MovieCount = e.ItemValues!.Count(e => e.Type == (int)BaseItemKind.Movie),
|
||||||
ItemSortBy.DatePlayed => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.LastPlayedDate,
|
AlbumCount = e.ItemValues!.Count(e => e.Type == (int)BaseItemKind.MusicAlbum),
|
||||||
ItemSortBy.PlayCount => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.PlayCount,
|
ArtistCount = e.ItemValues!.Count(e => e.Type == 0 || e.Type == 1),
|
||||||
ItemSortBy.IsFavoriteOrLiked => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.IsFavorite,
|
SongCount = e.ItemValues!.Count(e => e.Type == (int)BaseItemKind.MusicAlbum),
|
||||||
ItemSortBy.IsFolder => e => e.IsFolder,
|
TrailerCount = e.ItemValues!.Count(e => e.Type == (int)BaseItemKind.Trailer),
|
||||||
ItemSortBy.IsPlayed => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.Played,
|
}
|
||||||
ItemSortBy.IsUnplayed => e => !e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.Played,
|
});
|
||||||
ItemSortBy.DateLastContentAdded => e => e.DateLastMediaAdded,
|
|
||||||
ItemSortBy.Artist => e => e.ItemValues!.Where(f => f.Type == 0).Select(f => f.CleanValue),
|
|
||||||
ItemSortBy.AlbumArtist => e => e.ItemValues!.Where(f => f.Type == 1).Select(f => f.CleanValue),
|
|
||||||
ItemSortBy.Studio => e => e.ItemValues!.Where(f => f.Type == 3).Select(f => f.CleanValue),
|
|
||||||
ItemSortBy.OfficialRating => e => e.InheritedParentalRatingValue,
|
|
||||||
// ItemSortBy.SeriesDatePlayed => "(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where Played=1 and B.SeriesPresentationUniqueKey=A.PresentationUniqueKey)",
|
|
||||||
ItemSortBy.SeriesSortName => e => e.SeriesName,
|
|
||||||
// ItemSortBy.AiredEpisodeOrder => "AiredEpisodeOrder",
|
|
||||||
ItemSortBy.Album => e => e.Album,
|
|
||||||
ItemSortBy.DateCreated => e => e.DateCreated,
|
|
||||||
ItemSortBy.PremiereDate => e => e.PremiereDate,
|
|
||||||
ItemSortBy.StartDate => e => e.StartDate,
|
|
||||||
ItemSortBy.Name => e => e.Name,
|
|
||||||
ItemSortBy.CommunityRating => e => e.CommunityRating,
|
|
||||||
ItemSortBy.ProductionYear => e => e.ProductionYear,
|
|
||||||
ItemSortBy.CriticRating => e => e.CriticRating,
|
|
||||||
ItemSortBy.VideoBitRate => e => e.TotalBitrate,
|
|
||||||
ItemSortBy.ParentIndexNumber => e => e.ParentIndexNumber,
|
|
||||||
ItemSortBy.IndexNumber => e => e.IndexNumber,
|
|
||||||
_ => e => e.SortName
|
|
||||||
};
|
|
||||||
#pragma warning restore CS8603 // Possible null reference return.
|
|
||||||
|
|
||||||
|
result.StartIndex = filter.StartIndex ?? 0;
|
||||||
|
result.Items = resultQuery.ToImmutableArray().Select(e =>
|
||||||
|
{
|
||||||
|
return (DeserialiseBaseItem(e.item), e.itemCount);
|
||||||
|
}).ToImmutableArray();
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IQueryable<BaseItemEntity> MapOrderByField(IQueryable<BaseItemEntity> dbQuery, ItemSortBy sortBy, InternalItemsQuery query)
|
/// <inheritdoc />
|
||||||
|
public void DeleteItem(Guid id)
|
||||||
{
|
{
|
||||||
return sortBy switch
|
ArgumentNullException.ThrowIfNull(id.IsEmpty() ? null : id);
|
||||||
{
|
|
||||||
ItemSortBy.AirTime => dbQuery.OrderBy(e => e.SortName), // TODO
|
using var context = _dbProvider.CreateDbContext();
|
||||||
ItemSortBy.Runtime => dbQuery.OrderBy(e => e.RunTimeTicks),
|
using var transaction = context.Database.BeginTransaction();
|
||||||
ItemSortBy.Random => dbQuery.OrderBy(e => EF.Functions.Random()),
|
context.Peoples.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
|
||||||
ItemSortBy.DatePlayed => dbQuery.OrderBy(e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.LastPlayedDate),
|
context.Chapters.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
|
||||||
ItemSortBy.PlayCount => dbQuery.OrderBy(e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.PlayCount),
|
context.MediaStreamInfos.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
|
||||||
ItemSortBy.IsFavoriteOrLiked => dbQuery.OrderBy(e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.IsFavorite),
|
context.AncestorIds.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
|
||||||
ItemSortBy.IsFolder => dbQuery.OrderBy(e => e.IsFolder),
|
context.ItemValues.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
|
||||||
ItemSortBy.IsPlayed => dbQuery.OrderBy(e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.Played),
|
context.BaseItems.Where(e => e.Id.Equals(id)).ExecuteDelete();
|
||||||
ItemSortBy.IsUnplayed => dbQuery.OrderBy(e => !e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.Played),
|
context.SaveChanges();
|
||||||
ItemSortBy.DateLastContentAdded => dbQuery.OrderBy(e => e.DateLastMediaAdded),
|
transaction.Commit();
|
||||||
ItemSortBy.Artist => dbQuery.OrderBy(e => e.ItemValues!.Where(f => f.Type == 0).Select(f => f.CleanValue)),
|
|
||||||
ItemSortBy.AlbumArtist => dbQuery.OrderBy(e => e.ItemValues!.Where(f => f.Type == 1).Select(f => f.CleanValue)),
|
|
||||||
ItemSortBy.Studio => dbQuery.OrderBy(e => e.ItemValues!.Where(f => f.Type == 3).Select(f => f.CleanValue)),
|
|
||||||
ItemSortBy.OfficialRating => dbQuery.OrderBy(e => e.InheritedParentalRatingValue),
|
|
||||||
// ItemSortBy.SeriesDatePlayed => "(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where Played=1 and B.SeriesPresentationUniqueKey=A.PresentationUniqueKey)",
|
|
||||||
ItemSortBy.SeriesSortName => dbQuery.OrderBy(e => e.SeriesName),
|
|
||||||
// ItemSortBy.AiredEpisodeOrder => "AiredEpisodeOrder",
|
|
||||||
ItemSortBy.Album => dbQuery.OrderBy(e => e.Album),
|
|
||||||
ItemSortBy.DateCreated => dbQuery.OrderBy(e => e.DateCreated),
|
|
||||||
ItemSortBy.PremiereDate => dbQuery.OrderBy(e => e.PremiereDate),
|
|
||||||
ItemSortBy.StartDate => dbQuery.OrderBy(e => e.StartDate),
|
|
||||||
ItemSortBy.Name => dbQuery.OrderBy(e => e.Name),
|
|
||||||
ItemSortBy.CommunityRating => dbQuery.OrderBy(e => e.CommunityRating),
|
|
||||||
ItemSortBy.ProductionYear => dbQuery.OrderBy(e => e.ProductionYear),
|
|
||||||
ItemSortBy.CriticRating => dbQuery.OrderBy(e => e.CriticRating),
|
|
||||||
ItemSortBy.VideoBitRate => dbQuery.OrderBy(e => e.TotalBitrate),
|
|
||||||
ItemSortBy.ParentIndexNumber => dbQuery.OrderBy(e => e.ParentIndexNumber),
|
|
||||||
ItemSortBy.IndexNumber => dbQuery.OrderBy(e => e.IndexNumber),
|
|
||||||
_ => dbQuery.OrderBy(e => e.SortName)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IQueryable<BaseItemEntity> ApplyOrder(IQueryable<BaseItemEntity> query, InternalItemsQuery filter)
|
/// <inheritdoc />
|
||||||
|
public void UpdateInheritedValues()
|
||||||
{
|
{
|
||||||
var orderBy = filter.OrderBy;
|
using var context = _dbProvider.CreateDbContext();
|
||||||
bool hasSearch = !string.IsNullOrEmpty(filter.SearchTerm);
|
using var transaction = context.Database.BeginTransaction();
|
||||||
|
|
||||||
if (hasSearch)
|
context.ItemValues.Where(e => e.Type == 6).ExecuteDelete();
|
||||||
|
context.ItemValues.AddRange(context.ItemValues.Where(e => e.Type == 4).Select(e => new Data.Entities.ItemValue()
|
||||||
{
|
{
|
||||||
List<(ItemSortBy, SortOrder)> prepend = new List<(ItemSortBy, SortOrder)>(4);
|
CleanValue = e.CleanValue,
|
||||||
if (hasSearch)
|
ItemId = e.ItemId,
|
||||||
|
Type = 6,
|
||||||
|
Value = e.Value,
|
||||||
|
Item = null!
|
||||||
|
}));
|
||||||
|
|
||||||
|
context.ItemValues.AddRange(
|
||||||
|
context.AncestorIds.Where(e => e.AncestorIdText != null).Join(context.ItemValues.Where(e => e.Value != null && e.Type == 4), e => e.Id, e => e.ItemId, (e, f) => new Data.Entities.ItemValue()
|
||||||
{
|
{
|
||||||
prepend.Add((ItemSortBy.SortName, SortOrder.Ascending));
|
CleanValue = f.CleanValue,
|
||||||
}
|
ItemId = e.ItemId,
|
||||||
|
Item = null!,
|
||||||
orderBy = filter.OrderBy = [.. prepend, .. orderBy];
|
Type = 6,
|
||||||
}
|
Value = f.Value
|
||||||
else if (orderBy.Count == 0)
|
}));
|
||||||
{
|
context.SaveChanges();
|
||||||
return query;
|
|
||||||
}
|
transaction.Commit();
|
||||||
|
|
||||||
foreach (var item in orderBy)
|
|
||||||
{
|
|
||||||
var expression = MapOrderByField(item.OrderBy, filter);
|
|
||||||
if (item.SortOrder == SortOrder.Ascending)
|
|
||||||
{
|
|
||||||
query = query.OrderBy(expression);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
query = query.OrderByDescending(expression);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return query;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IItemRepository"/>
|
||||||
public IReadOnlyList<Guid> GetItemIdsList(InternalItemsQuery filter)
|
public IReadOnlyList<Guid> GetItemIdsList(InternalItemsQuery filter)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(filter);
|
ArgumentNullException.ThrowIfNull(filter);
|
||||||
PrepareFilterQuery(filter);
|
PrepareFilterQuery(filter);
|
||||||
|
|
||||||
using var context = _dbProvider.CreateDbContext();
|
using var context = _dbProvider.CreateDbContext();
|
||||||
var dbQuery = TranslateQuery(context.BaseItems, context, filter)
|
var dbQuery = TranslateQuery(context.BaseItems.AsNoTracking(), context, filter)
|
||||||
.DistinctBy(e => e.Id);
|
.DistinctBy(e => e.Id);
|
||||||
|
|
||||||
var enableGroupByPresentationUniqueKey = EnableGroupByPresentationUniqueKey(filter);
|
var enableGroupByPresentationUniqueKey = EnableGroupByPresentationUniqueKey(filter);
|
||||||
@ -306,56 +337,57 @@ public class BaseItemManager : IItemRepository
|
|||||||
return Pageinate(dbQuery, filter).Select(e => e.Id).ToImmutableArray();
|
return Pageinate(dbQuery, filter).Select(e => e.Id).ToImmutableArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool EnableGroupByPresentationUniqueKey(InternalItemsQuery query)
|
/// <inheritdoc />
|
||||||
|
public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetAllArtists(InternalItemsQuery query)
|
||||||
{
|
{
|
||||||
if (!query.GroupByPresentationUniqueKey)
|
return GetItemValues(query, new[] { 0, 1 }, typeof(MusicArtist).FullName);
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query.GroupBySeriesPresentationUniqueKey)
|
/// <inheritdoc />
|
||||||
|
public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetArtists(InternalItemsQuery query)
|
||||||
{
|
{
|
||||||
return false;
|
return GetItemValues(query, new[] { 0 }, typeof(MusicArtist).FullName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey))
|
/// <inheritdoc />
|
||||||
|
public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetAlbumArtists(InternalItemsQuery query)
|
||||||
{
|
{
|
||||||
return false;
|
return GetItemValues(query, new[] { 1 }, typeof(MusicArtist).FullName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query.User is null)
|
/// <inheritdoc />
|
||||||
|
public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetStudios(InternalItemsQuery query)
|
||||||
{
|
{
|
||||||
return false;
|
return GetItemValues(query, new[] { 3 }, typeof(Studio).FullName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query.IncludeItemTypes.Length == 0)
|
/// <inheritdoc />
|
||||||
|
public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetGenres(InternalItemsQuery query)
|
||||||
{
|
{
|
||||||
return true;
|
return GetItemValues(query, new[] { 2 }, typeof(Genre).FullName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return query.IncludeItemTypes.Contains(BaseItemKind.Episode)
|
/// <inheritdoc />
|
||||||
|| query.IncludeItemTypes.Contains(BaseItemKind.Video)
|
public QueryResult<(BaseItem Item, ItemCounts ItemCounts)> GetMusicGenres(InternalItemsQuery query)
|
||||||
|| query.IncludeItemTypes.Contains(BaseItemKind.Movie)
|
{
|
||||||
|| query.IncludeItemTypes.Contains(BaseItemKind.MusicVideo)
|
return GetItemValues(query, new[] { 2 }, typeof(MusicGenre).FullName);
|
||||||
|| query.IncludeItemTypes.Contains(BaseItemKind.Series)
|
|
||||||
|| query.IncludeItemTypes.Contains(BaseItemKind.Season);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="IItemRepository"/>
|
/// <inheritdoc cref="IItemRepository"/>
|
||||||
public QueryResult<BaseItem> GetItems(InternalItemsQuery query)
|
public QueryResult<BaseItemDto> GetItems(InternalItemsQuery query)
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(query);
|
ArgumentNullException.ThrowIfNull(query);
|
||||||
if (!query.EnableTotalRecordCount || (!query.Limit.HasValue && (query.StartIndex ?? 0) == 0))
|
if (!query.EnableTotalRecordCount || (!query.Limit.HasValue && (query.StartIndex ?? 0) == 0))
|
||||||
{
|
{
|
||||||
var returnList = GetItemList(query);
|
var returnList = GetItemList(query);
|
||||||
return new QueryResult<BaseItem>(
|
return new QueryResult<BaseItemDto>(
|
||||||
query.StartIndex,
|
query.StartIndex,
|
||||||
returnList.Count,
|
returnList.Count,
|
||||||
returnList);
|
returnList);
|
||||||
}
|
}
|
||||||
|
|
||||||
PrepareFilterQuery(query);
|
PrepareFilterQuery(query);
|
||||||
var result = new QueryResult<BaseItem>();
|
var result = new QueryResult<BaseItemDto>();
|
||||||
|
|
||||||
using var context = _dbProvider.CreateDbContext();
|
using var context = _dbProvider.CreateDbContext();
|
||||||
var dbQuery = TranslateQuery(context.BaseItems, context, query)
|
var dbQuery = TranslateQuery(context.BaseItems, context, query)
|
||||||
@ -2094,4 +2126,134 @@ public class BaseItemManager : IItemRepository
|
|||||||
|
|
||||||
return query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(type);
|
return query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IQueryable<T> Pageinate<T>(IQueryable<T> query, InternalItemsQuery filter)
|
||||||
|
{
|
||||||
|
if (filter.Limit.HasValue || filter.StartIndex.HasValue)
|
||||||
|
{
|
||||||
|
var offset = filter.StartIndex ?? 0;
|
||||||
|
|
||||||
|
if (offset > 0)
|
||||||
|
{
|
||||||
|
query = query.Skip(offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.Limit.HasValue)
|
||||||
|
{
|
||||||
|
query = query.Take(filter.Limit.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Expression<Func<BaseItemEntity, object>> MapOrderByField(ItemSortBy sortBy, InternalItemsQuery query)
|
||||||
|
{
|
||||||
|
#pragma warning disable CS8603 // Possible null reference return.
|
||||||
|
return sortBy switch
|
||||||
|
{
|
||||||
|
ItemSortBy.AirTime => e => e.SortName, // TODO
|
||||||
|
ItemSortBy.Runtime => e => e.RunTimeTicks,
|
||||||
|
ItemSortBy.Random => e => EF.Functions.Random(),
|
||||||
|
ItemSortBy.DatePlayed => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.LastPlayedDate,
|
||||||
|
ItemSortBy.PlayCount => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.PlayCount,
|
||||||
|
ItemSortBy.IsFavoriteOrLiked => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.IsFavorite,
|
||||||
|
ItemSortBy.IsFolder => e => e.IsFolder,
|
||||||
|
ItemSortBy.IsPlayed => e => e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.Played,
|
||||||
|
ItemSortBy.IsUnplayed => e => !e.UserData!.FirstOrDefault(f => f.UserId.Equals(query.User!.Id) && f.Key == e.UserDataKey)!.Played,
|
||||||
|
ItemSortBy.DateLastContentAdded => e => e.DateLastMediaAdded,
|
||||||
|
ItemSortBy.Artist => e => e.ItemValues!.Where(f => f.Type == 0).Select(f => f.CleanValue),
|
||||||
|
ItemSortBy.AlbumArtist => e => e.ItemValues!.Where(f => f.Type == 1).Select(f => f.CleanValue),
|
||||||
|
ItemSortBy.Studio => e => e.ItemValues!.Where(f => f.Type == 3).Select(f => f.CleanValue),
|
||||||
|
ItemSortBy.OfficialRating => e => e.InheritedParentalRatingValue,
|
||||||
|
// ItemSortBy.SeriesDatePlayed => "(Select MAX(LastPlayedDate) from TypedBaseItems B" + GetJoinUserDataText(query) + " where Played=1 and B.SeriesPresentationUniqueKey=A.PresentationUniqueKey)",
|
||||||
|
ItemSortBy.SeriesSortName => e => e.SeriesName,
|
||||||
|
// ItemSortBy.AiredEpisodeOrder => "AiredEpisodeOrder",
|
||||||
|
ItemSortBy.Album => e => e.Album,
|
||||||
|
ItemSortBy.DateCreated => e => e.DateCreated,
|
||||||
|
ItemSortBy.PremiereDate => e => e.PremiereDate,
|
||||||
|
ItemSortBy.StartDate => e => e.StartDate,
|
||||||
|
ItemSortBy.Name => e => e.Name,
|
||||||
|
ItemSortBy.CommunityRating => e => e.CommunityRating,
|
||||||
|
ItemSortBy.ProductionYear => e => e.ProductionYear,
|
||||||
|
ItemSortBy.CriticRating => e => e.CriticRating,
|
||||||
|
ItemSortBy.VideoBitRate => e => e.TotalBitrate,
|
||||||
|
ItemSortBy.ParentIndexNumber => e => e.ParentIndexNumber,
|
||||||
|
ItemSortBy.IndexNumber => e => e.IndexNumber,
|
||||||
|
_ => e => e.SortName
|
||||||
|
};
|
||||||
|
#pragma warning restore CS8603 // Possible null reference return.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool EnableGroupByPresentationUniqueKey(InternalItemsQuery query)
|
||||||
|
{
|
||||||
|
if (!query.GroupByPresentationUniqueKey)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query.GroupBySeriesPresentationUniqueKey)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(query.PresentationUniqueKey))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query.User is null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query.IncludeItemTypes.Length == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return query.IncludeItemTypes.Contains(BaseItemKind.Episode)
|
||||||
|
|| query.IncludeItemTypes.Contains(BaseItemKind.Video)
|
||||||
|
|| query.IncludeItemTypes.Contains(BaseItemKind.Movie)
|
||||||
|
|| query.IncludeItemTypes.Contains(BaseItemKind.MusicVideo)
|
||||||
|
|| query.IncludeItemTypes.Contains(BaseItemKind.Series)
|
||||||
|
|| query.IncludeItemTypes.Contains(BaseItemKind.Season);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IQueryable<BaseItemEntity> ApplyOrder(IQueryable<BaseItemEntity> query, InternalItemsQuery filter)
|
||||||
|
{
|
||||||
|
var orderBy = filter.OrderBy;
|
||||||
|
bool hasSearch = !string.IsNullOrEmpty(filter.SearchTerm);
|
||||||
|
|
||||||
|
if (hasSearch)
|
||||||
|
{
|
||||||
|
List<(ItemSortBy, SortOrder)> prepend = new List<(ItemSortBy, SortOrder)>(4);
|
||||||
|
if (hasSearch)
|
||||||
|
{
|
||||||
|
prepend.Add((ItemSortBy.SortName, SortOrder.Ascending));
|
||||||
|
}
|
||||||
|
|
||||||
|
orderBy = filter.OrderBy = [.. prepend, .. orderBy];
|
||||||
|
}
|
||||||
|
else if (orderBy.Count == 0)
|
||||||
|
{
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var item in orderBy)
|
||||||
|
{
|
||||||
|
var expression = MapOrderByField(item.OrderBy, filter);
|
||||||
|
if (item.SortOrder == SortOrder.Ascending)
|
||||||
|
{
|
||||||
|
query = query.OrderBy(expression);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
query = query.OrderByDescending(expression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
201
Jellyfin.Server.Implementations/Item/MediaStreamManager.cs
Normal file
201
Jellyfin.Server.Implementations/Item/MediaStreamManager.cs
Normal file
@ -0,0 +1,201 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using Jellyfin.Data.Entities;
|
||||||
|
using MediaBrowser.Controller;
|
||||||
|
using MediaBrowser.Controller.Persistence;
|
||||||
|
using MediaBrowser.Model.Entities;
|
||||||
|
using MediaBrowser.Model.Globalization;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Jellyfin.Server.Implementations.Item;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="MediaStreamManager"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dbProvider"></param>
|
||||||
|
/// <param name="serverApplicationHost"></param>
|
||||||
|
/// <param name="localization"></param>
|
||||||
|
public class MediaStreamManager(IDbContextFactory<JellyfinDbContext> dbProvider, IServerApplicationHost serverApplicationHost, ILocalizationManager localization)
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void SaveMediaStreams(Guid id, IReadOnlyList<MediaStream> streams, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
using var context = dbProvider.CreateDbContext();
|
||||||
|
using var transaction = context.Database.BeginTransaction();
|
||||||
|
|
||||||
|
context.MediaStreamInfos.Where(e => e.ItemId.Equals(id)).ExecuteDelete();
|
||||||
|
context.MediaStreamInfos.AddRange(streams.Select(f => Map(f, id)));
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IReadOnlyList<MediaStream> GetMediaStreams(MediaStreamQuery filter)
|
||||||
|
{
|
||||||
|
using var context = dbProvider.CreateDbContext();
|
||||||
|
return TranslateQuery(context.MediaStreamInfos, filter).ToList().Select(Map).ToImmutableArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private string? GetPathToSave(string? path)
|
||||||
|
{
|
||||||
|
if (path is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return serverApplicationHost.ReverseVirtualPath(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string? RestorePath(string? path)
|
||||||
|
{
|
||||||
|
if (path is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return serverApplicationHost.ExpandVirtualPath(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private IQueryable<MediaStreamInfo> TranslateQuery(IQueryable<MediaStreamInfo> query, MediaStreamQuery filter)
|
||||||
|
{
|
||||||
|
query = query.Where(e => e.ItemId.Equals(filter.ItemId));
|
||||||
|
if (filter.Index.HasValue)
|
||||||
|
{
|
||||||
|
query = query.Where(e => e.StreamIndex == filter.Index);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.Type.HasValue)
|
||||||
|
{
|
||||||
|
query = query.Where(e => e.StreamType == filter.Type.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
private MediaStream Map(MediaStreamInfo entity)
|
||||||
|
{
|
||||||
|
var dto = new MediaStream();
|
||||||
|
dto.Index = entity.StreamIndex;
|
||||||
|
if (entity.StreamType != null)
|
||||||
|
{
|
||||||
|
dto.Type = Enum.Parse<MediaStreamType>(entity.StreamType);
|
||||||
|
}
|
||||||
|
|
||||||
|
dto.IsAVC = entity.IsAvc;
|
||||||
|
dto.Codec = entity.Codec;
|
||||||
|
dto.Language = entity.Language;
|
||||||
|
dto.ChannelLayout = entity.ChannelLayout;
|
||||||
|
dto.Profile = entity.Profile;
|
||||||
|
dto.AspectRatio = entity.AspectRatio;
|
||||||
|
dto.Path = RestorePath(entity.Path);
|
||||||
|
dto.IsInterlaced = entity.IsInterlaced;
|
||||||
|
dto.BitRate = entity.BitRate;
|
||||||
|
dto.Channels = entity.Channels;
|
||||||
|
dto.SampleRate = entity.SampleRate;
|
||||||
|
dto.IsDefault = entity.IsDefault;
|
||||||
|
dto.IsForced = entity.IsForced;
|
||||||
|
dto.IsExternal = entity.IsExternal;
|
||||||
|
dto.Height = entity.Height;
|
||||||
|
dto.Width = entity.Width;
|
||||||
|
dto.AverageFrameRate = entity.AverageFrameRate;
|
||||||
|
dto.RealFrameRate = entity.RealFrameRate;
|
||||||
|
dto.Level = entity.Level;
|
||||||
|
dto.PixelFormat = entity.PixelFormat;
|
||||||
|
dto.BitDepth = entity.BitDepth;
|
||||||
|
dto.IsAnamorphic = entity.IsAnamorphic;
|
||||||
|
dto.RefFrames = entity.RefFrames;
|
||||||
|
dto.CodecTag = entity.CodecTag;
|
||||||
|
dto.Comment = entity.Comment;
|
||||||
|
dto.NalLengthSize = entity.NalLengthSize;
|
||||||
|
dto.Title = entity.Title;
|
||||||
|
dto.TimeBase = entity.TimeBase;
|
||||||
|
dto.CodecTimeBase = entity.CodecTimeBase;
|
||||||
|
dto.ColorPrimaries = entity.ColorPrimaries;
|
||||||
|
dto.ColorSpace = entity.ColorSpace;
|
||||||
|
dto.ColorTransfer = entity.ColorTransfer;
|
||||||
|
dto.DvVersionMajor = entity.DvVersionMajor;
|
||||||
|
dto.DvVersionMinor = entity.DvVersionMinor;
|
||||||
|
dto.DvProfile = entity.DvProfile;
|
||||||
|
dto.DvLevel = entity.DvLevel;
|
||||||
|
dto.RpuPresentFlag = entity.RpuPresentFlag;
|
||||||
|
dto.ElPresentFlag = entity.ElPresentFlag;
|
||||||
|
dto.BlPresentFlag = entity.BlPresentFlag;
|
||||||
|
dto.DvBlSignalCompatibilityId = entity.DvBlSignalCompatibilityId;
|
||||||
|
dto.IsHearingImpaired = entity.IsHearingImpaired;
|
||||||
|
dto.Rotation = entity.Rotation;
|
||||||
|
|
||||||
|
if (dto.Type is MediaStreamType.Audio or MediaStreamType.Subtitle)
|
||||||
|
{
|
||||||
|
dto.LocalizedDefault = localization.GetLocalizedString("Default");
|
||||||
|
dto.LocalizedExternal = localization.GetLocalizedString("External");
|
||||||
|
|
||||||
|
if (dto.Type is MediaStreamType.Subtitle)
|
||||||
|
{
|
||||||
|
dto.LocalizedUndefined = localization.GetLocalizedString("Undefined");
|
||||||
|
dto.LocalizedForced = localization.GetLocalizedString("Forced");
|
||||||
|
dto.LocalizedHearingImpaired = localization.GetLocalizedString("HearingImpaired");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
private MediaStreamInfo Map(MediaStream dto, Guid itemId)
|
||||||
|
{
|
||||||
|
var entity = new MediaStreamInfo
|
||||||
|
{
|
||||||
|
Item = null!,
|
||||||
|
ItemId = itemId,
|
||||||
|
StreamIndex = dto.Index,
|
||||||
|
StreamType = dto.Type.ToString(),
|
||||||
|
IsAvc = dto.IsAVC.GetValueOrDefault(),
|
||||||
|
|
||||||
|
Codec = dto.Codec,
|
||||||
|
Language = dto.Language,
|
||||||
|
ChannelLayout = dto.ChannelLayout,
|
||||||
|
Profile = dto.Profile,
|
||||||
|
AspectRatio = dto.AspectRatio,
|
||||||
|
Path = GetPathToSave(dto.Path),
|
||||||
|
IsInterlaced = dto.IsInterlaced,
|
||||||
|
BitRate = dto.BitRate.GetValueOrDefault(0),
|
||||||
|
Channels = dto.Channels.GetValueOrDefault(0),
|
||||||
|
SampleRate = dto.SampleRate.GetValueOrDefault(0),
|
||||||
|
IsDefault = dto.IsDefault,
|
||||||
|
IsForced = dto.IsForced,
|
||||||
|
IsExternal = dto.IsExternal,
|
||||||
|
Height = dto.Height.GetValueOrDefault(0),
|
||||||
|
Width = dto.Width.GetValueOrDefault(0),
|
||||||
|
AverageFrameRate = dto.AverageFrameRate.GetValueOrDefault(0),
|
||||||
|
RealFrameRate = dto.RealFrameRate.GetValueOrDefault(0),
|
||||||
|
Level = (float)dto.Level.GetValueOrDefault(),
|
||||||
|
PixelFormat = dto.PixelFormat,
|
||||||
|
BitDepth = dto.BitDepth.GetValueOrDefault(0),
|
||||||
|
IsAnamorphic = dto.IsAnamorphic.GetValueOrDefault(0),
|
||||||
|
RefFrames = dto.RefFrames.GetValueOrDefault(0),
|
||||||
|
CodecTag = dto.CodecTag,
|
||||||
|
Comment = dto.Comment,
|
||||||
|
NalLengthSize = dto.NalLengthSize,
|
||||||
|
Title = dto.Title,
|
||||||
|
TimeBase = dto.TimeBase,
|
||||||
|
CodecTimeBase = dto.CodecTimeBase,
|
||||||
|
ColorPrimaries = dto.ColorPrimaries,
|
||||||
|
ColorSpace = dto.ColorSpace,
|
||||||
|
ColorTransfer = dto.ColorTransfer,
|
||||||
|
DvVersionMajor = dto.DvVersionMajor.GetValueOrDefault(0),
|
||||||
|
DvVersionMinor = dto.DvVersionMinor.GetValueOrDefault(0),
|
||||||
|
DvProfile = dto.DvProfile.GetValueOrDefault(0),
|
||||||
|
DvLevel = dto.DvLevel.GetValueOrDefault(0),
|
||||||
|
RpuPresentFlag = dto.RpuPresentFlag.GetValueOrDefault(0),
|
||||||
|
ElPresentFlag = dto.ElPresentFlag.GetValueOrDefault(0),
|
||||||
|
BlPresentFlag = dto.BlPresentFlag.GetValueOrDefault(0),
|
||||||
|
DvBlSignalCompatibilityId = dto.DvBlSignalCompatibilityId.GetValueOrDefault(0),
|
||||||
|
IsHearingImpaired = dto.IsHearingImpaired,
|
||||||
|
Rotation = dto.Rotation.GetValueOrDefault(0)
|
||||||
|
};
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
}
|
164
Jellyfin.Server.Implementations/Item/PeopleManager.cs
Normal file
164
Jellyfin.Server.Implementations/Item/PeopleManager.cs
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using Jellyfin.Data.Entities;
|
||||||
|
using Jellyfin.Data.Enums;
|
||||||
|
using Jellyfin.Extensions;
|
||||||
|
using MediaBrowser.Controller.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Jellyfin.Server.Implementations.Item;
|
||||||
|
|
||||||
|
public class PeopleManager
|
||||||
|
{
|
||||||
|
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="PeopleManager"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dbProvider">The EFCore Context factory.</param>
|
||||||
|
public PeopleManager(IDbContextFactory<JellyfinDbContext> dbProvider)
|
||||||
|
{
|
||||||
|
_dbProvider = dbProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IReadOnlyList<PersonInfo> GetPeople(InternalPeopleQuery filter)
|
||||||
|
{
|
||||||
|
using var context = _dbProvider.CreateDbContext();
|
||||||
|
var dbQuery = TranslateQuery(context.Peoples.AsNoTracking(), context, filter);
|
||||||
|
|
||||||
|
dbQuery = dbQuery.OrderBy(e => e.ListOrder);
|
||||||
|
if (filter.Limit > 0)
|
||||||
|
{
|
||||||
|
dbQuery = dbQuery.Take(filter.Limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dbQuery.ToList().Select(Map).ToImmutableArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IReadOnlyList<string> GetPeopleNames(InternalPeopleQuery filter)
|
||||||
|
{
|
||||||
|
using var context = _dbProvider.CreateDbContext();
|
||||||
|
var dbQuery = TranslateQuery(context.Peoples.AsNoTracking(), context, filter);
|
||||||
|
|
||||||
|
dbQuery = dbQuery.OrderBy(e => e.ListOrder);
|
||||||
|
if (filter.Limit > 0)
|
||||||
|
{
|
||||||
|
dbQuery = dbQuery.Take(filter.Limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return dbQuery.Select(e => e.Name).ToImmutableArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void UpdatePeople(Guid itemId, IReadOnlyList<PersonInfo> people)
|
||||||
|
{
|
||||||
|
using var context = _dbProvider.CreateDbContext();
|
||||||
|
using var transaction = context.Database.BeginTransaction();
|
||||||
|
|
||||||
|
context.Peoples.Where(e => e.ItemId.Equals(itemId)).ExecuteDelete();
|
||||||
|
context.Peoples.AddRange(people.Select(Map));
|
||||||
|
context.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
private PersonInfo Map(People people)
|
||||||
|
{
|
||||||
|
var personInfo = new PersonInfo()
|
||||||
|
{
|
||||||
|
ItemId = people.ItemId,
|
||||||
|
Name = people.Name,
|
||||||
|
Role = people.Role,
|
||||||
|
SortOrder = people.SortOrder,
|
||||||
|
};
|
||||||
|
if (Enum.TryParse<PersonKind>(people.PersonType, out var kind))
|
||||||
|
{
|
||||||
|
personInfo.Type = kind;
|
||||||
|
}
|
||||||
|
|
||||||
|
return personInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private People Map(PersonInfo people)
|
||||||
|
{
|
||||||
|
var personInfo = new People()
|
||||||
|
{
|
||||||
|
ItemId = people.ItemId,
|
||||||
|
Name = people.Name,
|
||||||
|
Role = people.Role,
|
||||||
|
SortOrder = people.SortOrder,
|
||||||
|
PersonType = people.Type.ToString()
|
||||||
|
};
|
||||||
|
|
||||||
|
return personInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IQueryable<People> TranslateQuery(IQueryable<People> query, JellyfinDbContext context, InternalPeopleQuery filter)
|
||||||
|
{
|
||||||
|
if (filter.User is not null && filter.IsFavorite.HasValue)
|
||||||
|
{
|
||||||
|
query = query.Where(e => e.PersonType == typeof(Person).FullName)
|
||||||
|
.Where(e => context.BaseItems.Where(d => context.UserData.Where(e => e.IsFavorite == filter.IsFavorite && e.UserId.Equals(filter.User.Id)).Any(f => f.Key == d.UserDataKey))
|
||||||
|
.Select(f => f.Name).Contains(e.Name));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!filter.ItemId.IsEmpty())
|
||||||
|
{
|
||||||
|
query = query.Where(e => e.ItemId.Equals(filter.ItemId));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!filter.AppearsInItemId.IsEmpty())
|
||||||
|
{
|
||||||
|
query = query.Where(e => context.Peoples.Where(f => f.ItemId.Equals(filter.AppearsInItemId)).Select(e => e.Name).Contains(e.Name));
|
||||||
|
}
|
||||||
|
|
||||||
|
var queryPersonTypes = filter.PersonTypes.Where(IsValidPersonType).ToList();
|
||||||
|
if (queryPersonTypes.Count > 0)
|
||||||
|
{
|
||||||
|
query = query.Where(e => queryPersonTypes.Contains(e.PersonType));
|
||||||
|
}
|
||||||
|
|
||||||
|
var queryExcludePersonTypes = filter.ExcludePersonTypes.Where(IsValidPersonType).ToList();
|
||||||
|
|
||||||
|
if (queryExcludePersonTypes.Count > 0)
|
||||||
|
{
|
||||||
|
query = query.Where(e => !queryPersonTypes.Contains(e.PersonType));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.MaxListOrder.HasValue)
|
||||||
|
{
|
||||||
|
query = query.Where(e => e.ListOrder <= filter.MaxListOrder.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(filter.NameContains))
|
||||||
|
{
|
||||||
|
query = query.Where(e => e.Name.Contains(filter.NameContains));
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsAlphaNumeric(string str)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(str))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < str.Length; i++)
|
||||||
|
{
|
||||||
|
if (!char.IsLetter(str[i]) && !char.IsNumber(str[i]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsValidPersonType(string value)
|
||||||
|
{
|
||||||
|
return IsAlphaNumeric(value);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user