using System;
using System.Collections.Generic;
using System.Linq;
using Jellyfin.Data.Enums;
using Jellyfin.Database.Implementations;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Extensions;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem;
using BaseItemEntity = Jellyfin.Database.Implementations.Entities.BaseItemEntity;
namespace Jellyfin.Server.Implementations.Item;
/*
All queries in this class and all other nullable enabled EFCore repository classes will make liberal use of the null-forgiving operator "!".
This is done as the code isn't actually executed client side, but only the expressions are interpret and the compiler cannot know that.
This is your only warning/message regarding this topic.
*/
///
/// Handles all storage logic for BaseItems.
///
public sealed partial class BaseItemRepository
: IItemRepository, IItemQueryHelpers
{
///
/// Gets the placeholder id for UserData detached items.
///
public static readonly Guid PlaceholderId = Guid.Parse("00000000-0000-0000-0000-000000000001");
private readonly IDbContextFactory _dbProvider;
private readonly IServerApplicationHost _appHost;
private readonly IItemTypeLookup _itemTypeLookup;
private readonly IServerConfigurationManager _serverConfigurationManager;
private readonly ILogger _logger;
private static readonly IReadOnlyList _getAllArtistsValueTypes = [ItemValueType.Artist, ItemValueType.AlbumArtist];
private static readonly IReadOnlyList _getArtistValueTypes = [ItemValueType.Artist];
private static readonly IReadOnlyList _getAlbumArtistValueTypes = [ItemValueType.AlbumArtist];
private static readonly IReadOnlyList _getStudiosValueTypes = [ItemValueType.Studios];
private static readonly IReadOnlyList _getGenreValueTypes = [ItemValueType.Genre];
///
/// Initializes a new instance of the class.
///
/// The db factory.
/// The Application host.
/// The static type lookup.
/// The server Configuration manager.
/// System logger.
public BaseItemRepository(
IDbContextFactory dbProvider,
IServerApplicationHost appHost,
IItemTypeLookup itemTypeLookup,
IServerConfigurationManager serverConfigurationManager,
ILogger logger)
{
_dbProvider = dbProvider;
_appHost = appHost;
_itemTypeLookup = itemTypeLookup;
_serverConfigurationManager = serverConfigurationManager;
_logger = logger;
}
///
/// Maps a Entity to the DTO. Delegates to .
///
/// The database entity.
/// The target DTO.
/// The application host.
/// The logger.
/// The mapped DTO.
public static BaseItemDto Map(BaseItemEntity entity, BaseItemDto dto, IServerApplicationHost? appHost, ILogger logger)
{
return BaseItemMapper.Map(entity, dto, appHost);
}
///
/// Maps a DTO to a database entity. Delegates to .
///
/// The DTO to map.
/// The mapped database entity.
public BaseItemEntity Map(BaseItemDto dto)
{
return BaseItemMapper.Map(dto, _appHost);
}
///
/// Deserializes a BaseItemEntity and sets all properties.
///
/// The entity to deserialize.
/// The logger.
/// The application host.
/// Whether to skip JSON deserialization.
/// The deserialized item, or null.
public static BaseItemDto? DeserializeBaseItem(BaseItemEntity baseItemEntity, ILogger logger, IServerApplicationHost? appHost, bool skipDeserialization = false)
{
return BaseItemMapper.DeserializeBaseItem(baseItemEntity, logger, appHost, skipDeserialization);
}
///
public void PrepareFilterQuery(InternalItemsQuery query)
{
if (query.Limit.HasValue && query.EnableGroupByMetadataKey)
{
query.Limit = query.Limit.Value + 4;
}
if (query.IsResumable ?? false)
{
query.IsVirtualItem = false;
}
}
private List GetItemByNameTypesInQuery(InternalItemsQuery query)
{
var list = new List();
if (IsTypeInQuery(BaseItemKind.Person, query))
{
list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.Person]!);
}
if (IsTypeInQuery(BaseItemKind.Genre, query))
{
list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.Genre]!);
}
if (IsTypeInQuery(BaseItemKind.MusicGenre, query))
{
list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicGenre]!);
}
if (IsTypeInQuery(BaseItemKind.MusicArtist, query))
{
list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.MusicArtist]!);
}
if (IsTypeInQuery(BaseItemKind.Studio, query))
{
list.Add(_itemTypeLookup.BaseItemKindNames[BaseItemKind.Studio]!);
}
return list;
}
private bool IsTypeInQuery(BaseItemKind type, InternalItemsQuery query)
{
if (query.ExcludeItemTypes.Contains(type))
{
return false;
}
return query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains(type);
}
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 static BaseItemImageInfo Map(Guid baseItemId, ItemImageInfo e)
{
return BaseItemMapper.MapImageToEntity(baseItemId, e);
}
private static ItemImageInfo Map(BaseItemImageInfo e, IServerApplicationHost? appHost)
{
return BaseItemMapper.MapImageFromEntity(e, appHost);
}
private string? GetPathToSave(string path)
{
if (path is null)
{
return null;
}
return _appHost.ReverseVirtualPath(path);
}
///
public BaseItemDto? DeserializeBaseItem(BaseItemEntity entity, bool skipDeserialization = false)
{
ArgumentNullException.ThrowIfNull(entity, nameof(entity));
if (_serverConfigurationManager?.Configuration is null)
{
throw new InvalidOperationException("Server Configuration manager or configuration is null");
}
var typeToSerialise = BaseItemMapper.GetType(entity.Type);
return BaseItemMapper.DeserializeBaseItem(
entity,
_logger,
_appHost,
skipDeserialization || (_serverConfigurationManager.Configuration.SkipDeserializationForBasicTypes && (typeToSerialise == typeof(Channel) || typeToSerialise == typeof(UserRootFolder))));
}
}