Răsfoiți Sursa

WIP move baseitem to jellyfin.db

JPVenson 9 luni în urmă
părinte
comite
ee1bdf4e22

Fișier diff suprimat deoarece este prea mare
+ 5 - 1071
Emby.Server.Implementations/Data/SqliteItemRepository.cs


+ 19 - 0
Jellyfin.Data/Entities/AncestorId.cs

@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace Jellyfin.Data.Entities;
+
+#pragma warning disable CA1708 // Identifiers should differ by more than case
+#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
+public class AncestorId
+{
+    public Guid Id { get; set; }
+
+    public Guid ItemId { get; set; }
+
+    public required BaseItem Item { get; set; }
+
+    public string? AncestorIdText { get; set; }
+}

+ 21 - 0
Jellyfin.Data/Entities/AttachmentStreamInfo.cs

@@ -0,0 +1,21 @@
+using System;
+
+namespace Jellyfin.Data.Entities;
+
+#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
+public class AttachmentStreamInfo
+{
+    public required Guid ItemId { get; set; }
+
+    public required int Index { get; set; }
+
+    public required string Codec { get; set; }
+
+    public string? CodecTag { get; set; }
+
+    public string? Comment { get; set; }
+
+    public string? Filename { get; set; }
+
+    public string? MimeType { get; set; }
+}

+ 164 - 0
Jellyfin.Data/Entities/BaseItem.cs

@@ -0,0 +1,164 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace Jellyfin.Data.Entities;
+
+#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
+public class BaseItem
+{
+    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
+
+    public Guid Id { get; set; }
+
+    public required string Type { get; set; }
+
+    public IReadOnlyList<byte>? Data { get; set; }
+
+    public Guid? ParentId { get; set; }
+
+    public string? Path { get; set; }
+
+    public DateTime StartDate { get; set; }
+
+    public DateTime EndDate { get; set; }
+
+    public string? ChannelId { get; set; }
+
+    public bool IsMovie { get; set; }
+
+    public float? CommunityRating { get; set; }
+
+    public string? CustomRating { get; set; }
+
+    public int? IndexNumber { get; set; }
+
+    public bool IsLocked { get; set; }
+
+    public string? Name { get; set; }
+
+    public string? OfficialRating { get; set; }
+
+    public string? MediaType { get; set; }
+
+    public string? Overview { get; set; }
+
+    public int? ParentIndexNumber { get; set; }
+
+    public DateTime? PremiereDate { get; set; }
+
+    public int? ProductionYear { get; set; }
+
+    public string? Genres { get; set; }
+
+    public string? SortName { get; set; }
+
+    public string? ForcedSortName { get; set; }
+
+    public long? RunTimeTicks { get; set; }
+
+    public DateTime? DateCreated { get; set; }
+
+    public DateTime? DateModified { get; set; }
+
+    public bool IsSeries { get; set; }
+
+    public string? EpisodeTitle { get; set; }
+
+    public bool IsRepeat { get; set; }
+
+    public string? PreferredMetadataLanguage { get; set; }
+
+    public string? PreferredMetadataCountryCode { get; set; }
+
+    public DateTime? DateLastRefreshed { get; set; }
+
+    public DateTime? DateLastSaved { get; set; }
+
+    public bool IsInMixedFolder { get; set; }
+
+    public string? LockedFields { get; set; }
+
+    public string? Studios { get; set; }
+
+    public string? Audio { get; set; }
+
+    public string? ExternalServiceId { get; set; }
+
+    public string? Tags { get; set; }
+
+    public bool IsFolder { get; set; }
+
+    public int? InheritedParentalRatingValue { get; set; }
+
+    public string? UnratedType { get; set; }
+
+    public string? TopParentId { get; set; }
+
+    public string? TrailerTypes { get; set; }
+
+    public float? CriticRating { get; set; }
+
+    public string? CleanName { get; set; }
+
+    public string? PresentationUniqueKey { get; set; }
+
+    public string? OriginalTitle { get; set; }
+
+    public string? PrimaryVersionId { get; set; }
+
+    public DateTime? DateLastMediaAdded { get; set; }
+
+    public string? Album { get; set; }
+
+    public float? LUFS { get; set; }
+
+    public float? NormalizationGain { get; set; }
+
+    public bool IsVirtualItem { get; set; }
+
+    public string? SeriesName { get; set; }
+
+    public string? UserDataKey { get; set; }
+
+    public string? SeasonName { get; set; }
+
+    public Guid? SeasonId { get; set; }
+
+    public Guid? SeriesId { get; set; }
+
+    public string? ExternalSeriesId { get; set; }
+
+    public string? Tagline { get; set; }
+
+    public string? ProviderIds { get; set; }
+
+    public string? Images { get; set; }
+
+    public string? ProductionLocations { get; set; }
+
+    public string? ExtraIds { get; set; }
+
+    public int? TotalBitrate { get; set; }
+
+    public string? ExtraType { get; set; }
+
+    public string? Artists { get; set; }
+
+    public string? AlbumArtists { get; set; }
+
+    public string? ExternalId { get; set; }
+
+    public string? SeriesPresentationUniqueKey { get; set; }
+
+    public string? ShowId { get; set; }
+
+    public string? OwnerId { get; set; }
+
+    public int? Width { get; set; }
+
+    public int? Height { get; set; }
+
+    public long? Size { get; set; }
+}

+ 22 - 0
Jellyfin.Data/Entities/Chapter.cs

@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace Jellyfin.Data.Entities;
+
+#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
+public class Chapter
+{
+    public Guid ItemId { get; set; }
+
+    public required int ChapterIndex { get; set; }
+
+    public required long StartPositionTicks { get; set; }
+
+    public string? Name { get; set; }
+
+    public string? ImagePath { get; set; }
+
+    public DateTime? ImageDateModified { get; set; }
+}

+ 16 - 0
Jellyfin.Data/Entities/ItemValue.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace Jellyfin.Data.Entities;
+
+public class ItemValue
+{
+    public Guid ItemId { get; set; }
+    public required BaseItem Item { get; set; }
+
+    public required int Type { get; set; }
+    public required string Value { get; set; }
+    public required string CleanValue { get; set; }
+}

+ 101 - 0
Jellyfin.Data/Entities/MediaStreamInfo.cs

@@ -0,0 +1,101 @@
+using System;
+
+namespace Jellyfin.Data.Entities;
+
+#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
+public class MediaStreamInfo
+{
+    public Guid ItemId { get; set; }
+
+    public required BaseItem Item { get; set; }
+
+    public int StreamIndex { get; set; }
+
+    public string? StreamType { get; set; }
+
+    public string? Codec { get; set; }
+
+    public string? Language { get; set; }
+
+    public string? ChannelLayout { get; set; }
+
+    public string? Profile { get; set; }
+
+    public string? AspectRatio { get; set; }
+
+    public string? Path { get; set; }
+
+    public bool IsInterlaced { get; set; }
+
+    public required int BitRate { get; set; }
+
+    public required int Channels { get; set; }
+
+    public required int SampleRate { get; set; }
+
+    public bool IsDefault { get; set; }
+
+    public bool IsForced { get; set; }
+
+    public bool IsExternal { get; set; }
+
+    public required int Height { get; set; }
+
+    public required int Width { get; set; }
+
+    public required float AverageFrameRate { get; set; }
+
+    public required float RealFrameRate { get; set; }
+
+    public required float Level { get; set; }
+
+    public string? PixelFormat { get; set; }
+
+    public required int BitDepth { get; set; }
+
+    public required bool IsAnamorphic { get; set; }
+
+    public required int RefFrames { get; set; }
+
+    public required string CodecTag { get; set; }
+
+    public required string Comment { get; set; }
+
+    public required string NalLengthSize { get; set; }
+
+    public required bool IsAvc { get; set; }
+
+    public required string Title { get; set; }
+
+    public required string TimeBase { get; set; }
+
+    public required string CodecTimeBase { get; set; }
+
+    public required string ColorPrimaries { get; set; }
+
+    public required string ColorSpace { get; set; }
+
+    public required string ColorTransfer { get; set; }
+
+    public required int DvVersionMajor { get; set; }
+
+    public required int DvVersionMinor { get; set; }
+
+    public required int DvProfile { get; set; }
+
+    public required int DvLevel { get; set; }
+
+    public required int RpuPresentFlag { get; set; }
+
+    public required int ElPresentFlag { get; set; }
+
+    public required int BlPresentFlag { get; set; }
+
+    public required int DvBlSignalCompatibilityId { get; set; }
+
+    public required bool IsHearingImpaired { get; set; }
+
+    public required int Rotation { get; set; }
+
+    public string? KeyFrames { get; set; }
+}

+ 16 - 0
Jellyfin.Data/Entities/People.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace Jellyfin.Data.Entities;
+public class People
+{
+    public Guid ItemId { get; set; }
+
+    public required string Name { get; set; }
+    public string? Role { get; set; }
+    public string? PersonType { get; set; }
+    public int? SortOrder { get; set; }
+    public int? ListOrder { get; set; }
+}

+ 753 - 0
Jellyfin.Server.Implementations/Item/BaseItemManager.cs

@@ -0,0 +1,753 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using Jellyfin.Extensions;
+using MediaBrowser.Controller;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.Audio;
+using MediaBrowser.Controller.Entities.TV;
+using MediaBrowser.Controller.LiveTv;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.LiveTv;
+using Microsoft.EntityFrameworkCore;
+using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem;
+using BaseItemEntity = Jellyfin.Data.Entities.BaseItem;
+
+namespace Jellyfin.Server.Implementations.Item;
+
+/// <summary>
+/// Handles all storage logic for BaseItems.
+/// </summary>
+public class BaseItemManager
+{
+    private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
+    private readonly IServerApplicationHost _appHost;
+
+     /// <summary>
+    /// This holds all the types in the running assemblies
+    /// so that we can de-serialize properly when we don't have strong types.
+    /// </summary>
+    private static readonly ConcurrentDictionary<string, Type?> _typeMap = new ConcurrentDictionary<string, Type?>();
+
+    /// <summary>
+    /// Initializes a new instance of the <see cref="BaseItemManager"/> class.
+    /// </summary>
+    /// <param name="dbProvider">The db factory.</param>
+    public BaseItemManager(IDbContextFactory<JellyfinDbContext> dbProvider, IServerApplicationHost appHost)
+    {
+        _dbProvider = dbProvider;
+        _appHost = appHost;
+    }
+
+    /// <summary>
+    /// Gets the type.
+    /// </summary>
+    /// <param name="typeName">Name of the type.</param>
+    /// <returns>Type.</returns>
+    /// <exception cref="ArgumentNullException"><c>typeName</c> is null.</exception>
+    private static Type? GetType(string typeName)
+    {
+        ArgumentException.ThrowIfNullOrEmpty(typeName);
+
+        return _typeMap.GetOrAdd(typeName, k => AppDomain.CurrentDomain.GetAssemblies()
+            .Select(a => a.GetType(k))
+            .FirstOrDefault(t => t is not null));
+    }
+
+    /// <summary>
+    /// Saves the items.
+    /// </summary>
+    /// <param name="items">The items.</param>
+    /// <param name="cancellationToken">The cancellation token.</param>
+    /// <exception cref="ArgumentNullException">
+    /// <paramref name="items"/> or <paramref name="cancellationToken"/> is <c>null</c>.
+    /// </exception>
+    public void UpdateOrInsertItems(IReadOnlyList<BaseItemDto> items, CancellationToken cancellationToken)
+    {
+        ArgumentNullException.ThrowIfNull(items);
+        cancellationToken.ThrowIfCancellationRequested();
+
+        var itemsLen = items.Count;
+        var tuples = new (BaseItemDto Item, List<Guid>? AncestorIds, BaseItemDto TopParent, string? UserDataKey, List<string> InheritedTags)[itemsLen];
+        for (int i = 0; i < itemsLen; i++)
+        {
+            var item = items[i];
+            var ancestorIds = item.SupportsAncestors ?
+                item.GetAncestorIds().Distinct().ToList() :
+                null;
+
+            var topParent = item.GetTopParent();
+
+            var userdataKey = item.GetUserDataKeys().FirstOrDefault();
+            var inheritedTags = item.GetInheritedTags();
+
+            tuples[i] = (item, ancestorIds, topParent, userdataKey, inheritedTags);
+        }
+
+        using var context = _dbProvider.CreateDbContext();
+        foreach (var item in tuples)
+        {
+            var entity = Map(item.Item);
+            context.BaseItems.Add(entity);
+
+            if (item.Item.SupportsAncestors && item.AncestorIds != null)
+            {
+                foreach (var ancestorId in item.AncestorIds)
+                {
+                    context.AncestorIds.Add(new Data.Entities.AncestorId()
+                    {
+                        Item = entity,
+                        AncestorIdText = ancestorId.ToString(),
+                        Id = ancestorId
+                    });
+                }
+            }
+
+            var itemValues = GetItemValuesToSave(item.Item, item.InheritedTags);
+            context.ItemValues.Where(e => e.ItemId.Equals(entity.Id)).ExecuteDelete();
+            foreach (var itemValue in itemValues)
+            {
+                context.ItemValues.Add(new()
+                {
+                    Item = entity,
+                    Type = itemValue.MagicNumber,
+                    Value = itemValue.Value,
+                    CleanValue = GetCleanValue(itemValue.Value)
+                });
+            }
+        }
+
+        context.SaveChanges(true);
+    }
+
+    public BaseItemDto? GetSingle(Guid id)
+    {
+        if (id.IsEmpty())
+        {
+            throw new ArgumentException("Guid can't be empty", nameof(id));
+        }
+
+        using var context = _dbProvider.CreateDbContext();
+        var item = context.BaseItems.FirstOrDefault(e => e.Id.Equals(id));
+        if (item is null)
+        {
+            return null;
+        }
+
+        return DeserialiseBaseItem(item);
+    }
+
+    private BaseItemDto DeserialiseBaseItem(BaseItemEntity baseItemEntity)
+    {
+        var type = GetType(baseItemEntity.Type) ?? throw new InvalidOperationException("Cannot deserialise unkown type.");
+        var dto = Activator.CreateInstance(type) as BaseItemDto ?? throw new InvalidOperationException("Cannot deserialise unkown type.");;
+        return Map(baseItemEntity, dto);
+    }
+
+    /// <summary>
+    /// Maps a Entity to the DTO.
+    /// </summary>
+    /// <param name="entity">The entity.</param>
+    /// <param name="dto">The dto base instance.</param>
+    /// <returns>The dto to map.</returns>
+    public BaseItemDto Map(BaseItemEntity entity, BaseItemDto dto)
+    {
+        dto.Id = entity.Id;
+        dto.ParentId = entity.ParentId.GetValueOrDefault();
+        dto.Path = entity.Path;
+        dto.EndDate = entity.EndDate;
+        dto.CommunityRating = entity.CommunityRating;
+        dto.CustomRating = entity.CustomRating;
+        dto.IndexNumber = entity.IndexNumber;
+        dto.IsLocked = entity.IsLocked;
+        dto.Name = entity.Name;
+        dto.OfficialRating = entity.OfficialRating;
+        dto.Overview = entity.Overview;
+        dto.ParentIndexNumber = entity.ParentIndexNumber;
+        dto.PremiereDate = entity.PremiereDate;
+        dto.ProductionYear = entity.ProductionYear;
+        dto.SortName = entity.SortName;
+        dto.ForcedSortName = entity.ForcedSortName;
+        dto.RunTimeTicks = entity.RunTimeTicks;
+        dto.PreferredMetadataLanguage = entity.PreferredMetadataLanguage;
+        dto.PreferredMetadataCountryCode = entity.PreferredMetadataCountryCode;
+        dto.IsInMixedFolder = entity.IsInMixedFolder;
+        dto.InheritedParentalRatingValue = entity.InheritedParentalRatingValue;
+        dto.CriticRating = entity.CriticRating;
+        dto.PresentationUniqueKey = entity.PresentationUniqueKey;
+        dto.OriginalTitle = entity.OriginalTitle;
+        dto.Album = entity.Album;
+        dto.LUFS = entity.LUFS;
+        dto.NormalizationGain = entity.NormalizationGain;
+        dto.IsVirtualItem = entity.IsVirtualItem;
+        dto.ExternalSeriesId = entity.ExternalSeriesId;
+        dto.Tagline = entity.Tagline;
+        dto.TotalBitrate = entity.TotalBitrate;
+        dto.ExternalId = entity.ExternalId;
+        dto.Size = entity.Size;
+        dto.Genres = entity.Genres?.Split('|');
+        dto.DateCreated = entity.DateCreated.GetValueOrDefault();
+        dto.DateModified = entity.DateModified.GetValueOrDefault();
+        dto.ChannelId = string.IsNullOrWhiteSpace(entity.ChannelId) ? Guid.Empty : Guid.Parse(entity.ChannelId);
+        dto.DateLastRefreshed = entity.DateLastRefreshed.GetValueOrDefault();
+        dto.DateLastSaved = entity.DateLastSaved.GetValueOrDefault();
+        dto.OwnerId = string.IsNullOrWhiteSpace(entity.OwnerId) ? Guid.Empty : Guid.Parse(entity.OwnerId);
+        dto.Width = entity.Width.GetValueOrDefault();
+        dto.Height = entity.Height.GetValueOrDefault();
+        if (entity.ProviderIds is not null)
+        {
+            DeserializeProviderIds(entity.ProviderIds, dto);
+        }
+
+        if (entity.ExtraType is not null)
+        {
+            dto.ExtraType = Enum.Parse<ExtraType>(entity.ExtraType);
+        }
+
+        if (entity.LockedFields is not null)
+        {
+            List<MetadataField>? fields = null;
+            foreach (var i in entity.LockedFields.AsSpan().Split('|'))
+            {
+                if (Enum.TryParse(i, true, out MetadataField parsedValue))
+                {
+                    (fields ??= new List<MetadataField>()).Add(parsedValue);
+                }
+            }
+
+            dto.LockedFields = fields?.ToArray() ?? Array.Empty<MetadataField>();
+        }
+
+        if (entity.Audio is not null)
+        {
+            dto.Audio = Enum.Parse<ProgramAudio>(entity.Audio);
+        }
+
+        dto.ExtraIds = entity.ExtraIds?.Split('|').Select(e => Guid.Parse(e)).ToArray();
+        dto.ProductionLocations = entity.ProductionLocations?.Split('|');
+        dto.Studios = entity.Studios?.Split('|');
+        dto.Tags = entity.Tags?.Split('|');
+
+        if (dto is IHasProgramAttributes hasProgramAttributes)
+        {
+            hasProgramAttributes.IsMovie = entity.IsMovie;
+            hasProgramAttributes.IsSeries = entity.IsSeries;
+            hasProgramAttributes.EpisodeTitle = entity.EpisodeTitle;
+            hasProgramAttributes.IsRepeat = entity.IsRepeat;
+        }
+
+        if (dto is LiveTvChannel liveTvChannel)
+        {
+            liveTvChannel.ServiceName = entity.ExternalServiceId;
+        }
+
+        if (dto is Trailer trailer)
+        {
+            List<TrailerType>? types = null;
+            foreach (var i in entity.TrailerTypes.AsSpan().Split('|'))
+            {
+                if (Enum.TryParse(i, true, out TrailerType parsedValue))
+                {
+                    (types ??= new List<TrailerType>()).Add(parsedValue);
+                }
+            }
+
+            trailer.TrailerTypes = types?.ToArray() ?? Array.Empty<TrailerType>();
+        }
+
+        if (dto is Video video)
+        {
+            video.PrimaryVersionId = entity.PrimaryVersionId;
+        }
+
+        if (dto is IHasSeries hasSeriesName)
+        {
+            hasSeriesName.SeriesName = entity.SeriesName;
+            hasSeriesName.SeriesId = entity.SeriesId.GetValueOrDefault();
+            hasSeriesName.SeriesPresentationUniqueKey = entity.SeriesPresentationUniqueKey;
+        }
+
+        if (dto is Episode episode)
+        {
+            episode.SeasonName = entity.SeasonName;
+            episode.SeasonId = entity.SeasonId.GetValueOrDefault();
+        }
+
+        if (dto is IHasArtist hasArtists)
+        {
+            hasArtists.Artists = entity.Artists?.Split('|', StringSplitOptions.RemoveEmptyEntries);
+        }
+
+        if (dto is IHasAlbumArtist hasAlbumArtists)
+        {
+            hasAlbumArtists.AlbumArtists = entity.AlbumArtists?.Split('|', StringSplitOptions.RemoveEmptyEntries);
+        }
+
+        if (dto is LiveTvProgram program)
+        {
+            program.ShowId = entity.ShowId;
+        }
+
+        if (entity.Images is not null)
+        {
+            dto.ImageInfos = DeserializeImages(entity.Images);
+        }
+
+        // dto.Type = entity.Type;
+        // dto.Data = entity.Data;
+        // dto.MediaType = entity.MediaType;
+        if (dto is IHasStartDate hasStartDate)
+        {
+            hasStartDate.StartDate = entity.StartDate;
+        }
+
+        // Fields that are present in the DB but are never actually used
+        // dto.UnratedType = entity.UnratedType;
+        // dto.TopParentId = entity.TopParentId;
+        // dto.CleanName = entity.CleanName;
+        // dto.UserDataKey = entity.UserDataKey;
+
+        if (dto is Folder folder)
+        {
+            folder.DateLastMediaAdded = entity.DateLastMediaAdded;
+        }
+
+        return dto;
+    }
+
+    /// <summary>
+    /// Maps a Entity to the DTO.
+    /// </summary>
+    /// <param name="dto">The entity.</param>
+    /// <returns>The dto to map.</returns>
+    public BaseItemEntity Map(BaseItemDto dto)
+    {
+        var entity = new BaseItemEntity()
+        {
+            Type = dto.GetType().ToString(),
+        };
+        entity.Id = dto.Id;
+        entity.ParentId = dto.ParentId;
+        entity.Path = GetPathToSave(dto.Path);
+        entity.EndDate = dto.EndDate.GetValueOrDefault();
+        entity.CommunityRating = dto.CommunityRating;
+        entity.CustomRating = dto.CustomRating;
+        entity.IndexNumber = dto.IndexNumber;
+        entity.IsLocked = dto.IsLocked;
+        entity.Name = dto.Name;
+        entity.OfficialRating = dto.OfficialRating;
+        entity.Overview = dto.Overview;
+        entity.ParentIndexNumber = dto.ParentIndexNumber;
+        entity.PremiereDate = dto.PremiereDate;
+        entity.ProductionYear = dto.ProductionYear;
+        entity.SortName = dto.SortName;
+        entity.ForcedSortName = dto.ForcedSortName;
+        entity.RunTimeTicks = dto.RunTimeTicks;
+        entity.PreferredMetadataLanguage = dto.PreferredMetadataLanguage;
+        entity.PreferredMetadataCountryCode = dto.PreferredMetadataCountryCode;
+        entity.IsInMixedFolder = dto.IsInMixedFolder;
+        entity.InheritedParentalRatingValue = dto.InheritedParentalRatingValue;
+        entity.CriticRating = dto.CriticRating;
+        entity.PresentationUniqueKey = dto.PresentationUniqueKey;
+        entity.OriginalTitle = dto.OriginalTitle;
+        entity.Album = dto.Album;
+        entity.LUFS = dto.LUFS;
+        entity.NormalizationGain = dto.NormalizationGain;
+        entity.IsVirtualItem = dto.IsVirtualItem;
+        entity.ExternalSeriesId = dto.ExternalSeriesId;
+        entity.Tagline = dto.Tagline;
+        entity.TotalBitrate = dto.TotalBitrate;
+        entity.ExternalId = dto.ExternalId;
+        entity.Size = dto.Size;
+        entity.Genres = string.Join('|', dto.Genres);
+        entity.DateCreated = dto.DateCreated;
+        entity.DateModified = dto.DateModified;
+        entity.ChannelId = dto.ChannelId.ToString();
+        entity.DateLastRefreshed = dto.DateLastRefreshed;
+        entity.DateLastSaved = dto.DateLastSaved;
+        entity.OwnerId = dto.OwnerId.ToString();
+        entity.Width = dto.Width;
+        entity.Height = dto.Height;
+        entity.ProviderIds = SerializeProviderIds(dto.ProviderIds);
+
+        entity.Audio = dto.Audio?.ToString();
+        entity.ExtraType = dto.ExtraType?.ToString();
+
+        entity.ExtraIds = string.Join('|', dto.ExtraIds);
+        entity.ProductionLocations = string.Join('|', dto.ProductionLocations);
+        entity.Studios = dto.Studios is not null ? string.Join('|', dto.Studios) : null;
+        entity.Tags = dto.Tags is not null ? string.Join('|', dto.Tags) : null;
+        entity.LockedFields = dto.LockedFields is not null ? string.Join('|', dto.LockedFields) : null;
+
+        if (dto is IHasProgramAttributes hasProgramAttributes)
+        {
+            entity.IsMovie = hasProgramAttributes.IsMovie;
+            entity.IsSeries = hasProgramAttributes.IsSeries;
+            entity.EpisodeTitle = hasProgramAttributes.EpisodeTitle;
+            entity.IsRepeat = hasProgramAttributes.IsRepeat;
+        }
+
+        if (dto is LiveTvChannel liveTvChannel)
+        {
+            entity.ExternalServiceId = liveTvChannel.ServiceName;
+        }
+
+        if (dto is Trailer trailer)
+        {
+            entity.LockedFields = trailer.LockedFields is not null ? string.Join('|', trailer.LockedFields) : null;
+        }
+
+        if (dto is Video video)
+        {
+            entity.PrimaryVersionId = video.PrimaryVersionId;
+        }
+
+        if (dto is IHasSeries hasSeriesName)
+        {
+            entity.SeriesName = hasSeriesName.SeriesName;
+            entity.SeriesId = hasSeriesName.SeriesId;
+            entity.SeriesPresentationUniqueKey = hasSeriesName.SeriesPresentationUniqueKey;
+        }
+
+        if (dto is Episode episode)
+        {
+            entity.SeasonName = episode.SeasonName;
+            entity.SeasonId = episode.SeasonId;
+        }
+
+        if (dto is IHasArtist hasArtists)
+        {
+            entity.Artists = hasArtists.Artists is not null ? string.Join('|', hasArtists.Artists) : null;
+        }
+
+        if (dto is IHasAlbumArtist hasAlbumArtists)
+        {
+            entity.AlbumArtists = hasAlbumArtists.AlbumArtists is not null ? string.Join('|', hasAlbumArtists.AlbumArtists) : null;
+        }
+
+        if (dto is LiveTvProgram program)
+        {
+            entity.ShowId = program.ShowId;
+        }
+
+        if (dto.ImageInfos is not null)
+        {
+            entity.Images = SerializeImages(dto.ImageInfos);
+        }
+
+        // dto.Type = entity.Type;
+        // dto.Data = entity.Data;
+        // dto.MediaType = entity.MediaType;
+        if (dto is IHasStartDate hasStartDate)
+        {
+            entity.StartDate = hasStartDate.StartDate;
+        }
+
+        // Fields that are present in the DB but are never actually used
+        // dto.UnratedType = entity.UnratedType;
+        // dto.TopParentId = entity.TopParentId;
+        // dto.CleanName = entity.CleanName;
+        // dto.UserDataKey = entity.UserDataKey;
+
+        if (dto is Folder folder)
+        {
+            entity.DateLastMediaAdded = folder.DateLastMediaAdded;
+            entity.IsFolder = folder.IsFolder;
+        }
+
+        return entity;
+    }
+
+    private string GetCleanValue(string value)
+    {
+        if (string.IsNullOrWhiteSpace(value))
+        {
+            return value;
+        }
+
+        return value.RemoveDiacritics().ToLowerInvariant();
+    }
+
+    private List<(int MagicNumber, string Value)> GetItemValuesToSave(BaseItem item, List<string> inheritedTags)
+    {
+        var list = new List<(int, string)>();
+
+        if (item is IHasArtist hasArtist)
+        {
+            list.AddRange(hasArtist.Artists.Select(i => (0, i)));
+        }
+
+        if (item is IHasAlbumArtist hasAlbumArtist)
+        {
+            list.AddRange(hasAlbumArtist.AlbumArtists.Select(i => (1, i)));
+        }
+
+        list.AddRange(item.Genres.Select(i => (2, i)));
+        list.AddRange(item.Studios.Select(i => (3, i)));
+        list.AddRange(item.Tags.Select(i => (4, i)));
+
+        // keywords was 5
+
+        list.AddRange(inheritedTags.Select(i => (6, i)));
+
+        // Remove all invalid values.
+        list.RemoveAll(i => string.IsNullOrWhiteSpace(i.Item2));
+
+        return list;
+    }
+
+    internal static string? SerializeProviderIds(Dictionary<string, string> providerIds)
+    {
+        StringBuilder str = new StringBuilder();
+        foreach (var i in providerIds)
+        {
+            // Ideally we shouldn't need this IsNullOrWhiteSpace check,
+            // but we're seeing some cases of bad data slip through
+            if (string.IsNullOrWhiteSpace(i.Value))
+            {
+                continue;
+            }
+
+            str.Append(i.Key)
+                .Append('=')
+                .Append(i.Value)
+                .Append('|');
+        }
+
+        if (str.Length == 0)
+        {
+            return null;
+        }
+
+        str.Length -= 1; // Remove last |
+        return str.ToString();
+    }
+
+    internal static void DeserializeProviderIds(string value, IHasProviderIds item)
+    {
+        if (string.IsNullOrWhiteSpace(value))
+        {
+            return;
+        }
+
+        foreach (var part in value.SpanSplit('|'))
+        {
+            var providerDelimiterIndex = part.IndexOf('=');
+            // Don't let empty values through
+            if (providerDelimiterIndex != -1 && part.Length != providerDelimiterIndex + 1)
+            {
+                item.SetProviderId(part[..providerDelimiterIndex].ToString(), part[(providerDelimiterIndex + 1)..].ToString());
+            }
+        }
+    }
+
+    internal string? SerializeImages(ItemImageInfo[] images)
+    {
+        if (images.Length == 0)
+        {
+            return null;
+        }
+
+        StringBuilder str = new StringBuilder();
+        foreach (var i in images)
+        {
+            if (string.IsNullOrWhiteSpace(i.Path))
+            {
+                continue;
+            }
+
+            AppendItemImageInfo(str, i);
+            str.Append('|');
+        }
+
+        str.Length -= 1; // Remove last |
+        return str.ToString();
+    }
+
+    internal ItemImageInfo[] DeserializeImages(string value)
+    {
+        if (string.IsNullOrWhiteSpace(value))
+        {
+            return Array.Empty<ItemImageInfo>();
+        }
+
+        // TODO The following is an ugly performance optimization, but it's extremely unlikely that the data in the database would be malformed
+        var valueSpan = value.AsSpan();
+        var count = valueSpan.Count('|') + 1;
+
+        var position = 0;
+        var result = new ItemImageInfo[count];
+        foreach (var part in valueSpan.Split('|'))
+        {
+            var image = ItemImageInfoFromValueString(part);
+
+            if (image is not null)
+            {
+                result[position++] = image;
+            }
+        }
+
+        if (position == count)
+        {
+            return result;
+        }
+
+        if (position == 0)
+        {
+            return Array.Empty<ItemImageInfo>();
+        }
+
+        // Extremely unlikely, but somehow one or more of the image strings were malformed. Cut the array.
+        return result[..position];
+    }
+
+    private void AppendItemImageInfo(StringBuilder bldr, ItemImageInfo image)
+    {
+        const char Delimiter = '*';
+
+        var path = image.Path ?? string.Empty;
+
+        bldr.Append(GetPathToSave(path))
+            .Append(Delimiter)
+            .Append(image.DateModified.Ticks)
+            .Append(Delimiter)
+            .Append(image.Type)
+            .Append(Delimiter)
+            .Append(image.Width)
+            .Append(Delimiter)
+            .Append(image.Height);
+
+        var hash = image.BlurHash;
+        if (!string.IsNullOrEmpty(hash))
+        {
+            bldr.Append(Delimiter)
+                // Replace delimiters with other characters.
+                // This can be removed when we migrate to a proper DB.
+                .Append(hash.Replace(Delimiter, '/').Replace('|', '\\'));
+        }
+    }
+
+    private string? GetPathToSave(string path)
+    {
+        if (path is null)
+        {
+            return null;
+        }
+
+        return _appHost.ReverseVirtualPath(path);
+    }
+
+    private string RestorePath(string path)
+    {
+        return _appHost.ExpandVirtualPath(path);
+    }
+
+    internal ItemImageInfo? ItemImageInfoFromValueString(ReadOnlySpan<char> value)
+    {
+        const char Delimiter = '*';
+
+        var nextSegment = value.IndexOf(Delimiter);
+        if (nextSegment == -1)
+        {
+            return null;
+        }
+
+        ReadOnlySpan<char> path = value[..nextSegment];
+        value = value[(nextSegment + 1)..];
+        nextSegment = value.IndexOf(Delimiter);
+        if (nextSegment == -1)
+        {
+            return null;
+        }
+
+        ReadOnlySpan<char> dateModified = value[..nextSegment];
+        value = value[(nextSegment + 1)..];
+        nextSegment = value.IndexOf(Delimiter);
+        if (nextSegment == -1)
+        {
+            nextSegment = value.Length;
+        }
+
+        ReadOnlySpan<char> imageType = value[..nextSegment];
+
+        var image = new ItemImageInfo
+        {
+            Path = RestorePath(path.ToString())
+        };
+
+        if (long.TryParse(dateModified, CultureInfo.InvariantCulture, out var ticks)
+            && ticks >= DateTime.MinValue.Ticks
+            && ticks <= DateTime.MaxValue.Ticks)
+        {
+            image.DateModified = new DateTime(ticks, DateTimeKind.Utc);
+        }
+        else
+        {
+            return null;
+        }
+
+        if (Enum.TryParse(imageType, true, out ImageType type))
+        {
+            image.Type = type;
+        }
+        else
+        {
+            return null;
+        }
+
+        // Optional parameters: width*height*blurhash
+        if (nextSegment + 1 < value.Length - 1)
+        {
+            value = value[(nextSegment + 1)..];
+            nextSegment = value.IndexOf(Delimiter);
+            if (nextSegment == -1 || nextSegment == value.Length)
+            {
+                return image;
+            }
+
+            ReadOnlySpan<char> widthSpan = value[..nextSegment];
+
+            value = value[(nextSegment + 1)..];
+            nextSegment = value.IndexOf(Delimiter);
+            if (nextSegment == -1)
+            {
+                nextSegment = value.Length;
+            }
+
+            ReadOnlySpan<char> heightSpan = value[..nextSegment];
+
+            if (int.TryParse(widthSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var width)
+                && int.TryParse(heightSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var height))
+            {
+                image.Width = width;
+                image.Height = height;
+            }
+
+            if (nextSegment < value.Length - 1)
+            {
+                value = value[(nextSegment + 1)..];
+                var length = value.Length;
+
+                Span<char> blurHashSpan = stackalloc char[length];
+                for (int i = 0; i < length; i++)
+                {
+                    var c = value[i];
+                    blurHashSpan[i] = c switch
+                    {
+                        '/' => Delimiter,
+                        '\\' => '|',
+                        _ => c
+                    };
+                }
+
+                image.BlurHash = new string(blurHashSpan);
+            }
+        }
+
+        return image;
+    }
+}

+ 51 - 0
Jellyfin.Server.Implementations/Item/ChapterManager.cs

@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Jellyfin.Data.Entities;
+using MediaBrowser.Model.Entities;
+using Microsoft.EntityFrameworkCore;
+using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem;
+
+namespace Jellyfin.Server.Implementations.Item;
+
+public class ChapterManager
+{
+    private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
+
+    public ChapterManager(IDbContextFactory<JellyfinDbContext> dbProvider)
+    {
+        _dbProvider = dbProvider;
+    }
+
+    public IReadOnlyList<ChapterInfo> GetChapters(BaseItemDto baseItemDto)
+    {
+        using var context = _dbProvider.CreateDbContext();
+        return context.Chapters.Where(e => e.ItemId.Equals(baseItemDto.Id)).Select(Map).ToList();
+    }
+
+    private Chapter Map(ChapterInfo chapterInfo, int index, Guid itemId)
+    {
+        return new Chapter()
+        {
+            ChapterIndex = index,
+            StartPositionTicks = chapterInfo.StartPositionTicks,
+            ImageDateModified = chapterInfo.ImageDateModified,
+            ImagePath = chapterInfo.ImagePath,
+            ItemId = itemId,
+            Name = chapterInfo.Name
+        };
+    }
+
+    private ChapterInfo Map(Chapter chapterInfo, BaseItemDto baseItem)
+    {
+        var info = new ChapterInfo()
+        {
+            StartPositionTicks = chapterInfo.StartPositionTicks,
+            ImageDateModified = chapterInfo.ImageDateModified.GetValueOrDefault(),
+            ImagePath = chapterInfo.ImagePath,
+            Name = chapterInfo.Name,
+        };
+        info.ImageTag = _imageProcessor.GetImageCacheTag(baseItem, info);
+        return info;
+    }
+}

+ 35 - 0
Jellyfin.Server.Implementations/JellyfinDbContext.cs

@@ -93,6 +93,41 @@ public class JellyfinDbContext : DbContext
     /// </summary>
     public DbSet<UserData> UserData => Set<UserData>();
 
+    /// <summary>
+    /// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
+    /// </summary>
+    public DbSet<AncestorId> AncestorIds => Set<AncestorId>();
+
+    /// <summary>
+    /// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
+    /// </summary>
+    public DbSet<AttachmentStreamInfo> AttachmentStreamInfos => Set<AttachmentStreamInfo>();
+
+    /// <summary>
+    /// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
+    /// </summary>
+    public DbSet<BaseItem> BaseItems => Set<BaseItem>();
+
+    /// <summary>
+    /// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
+    /// </summary>
+    public DbSet<Chapter> Chapters => Set<Chapter>();
+
+    /// <summary>
+    /// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
+    /// </summary>
+    public DbSet<ItemValue> ItemValues => Set<ItemValue>();
+
+    /// <summary>
+    /// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
+    /// </summary>
+    public DbSet<MediaStreamInfo> MediaStreamInfos => Set<MediaStreamInfo>();
+
+    /// <summary>
+    /// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
+    /// </summary>
+    public DbSet<People> Peoples => Set<People>();
+
     /*public DbSet<Artwork> Artwork => Set<Artwork>();
 
     public DbSet<Book> Books => Set<Book>();

+ 20 - 0
Jellyfin.Server.Implementations/ModelConfiguration/AncestorIdConfiguration.cs

@@ -0,0 +1,20 @@
+using System;
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration;
+
+/// <summary>
+/// AncestorId configuration.
+/// </summary>
+public class AncestorIdConfiguration : IEntityTypeConfiguration<AncestorId>
+{
+    /// <inheritdoc/>
+    public void Configure(EntityTypeBuilder<AncestorId> builder)
+    {
+        builder.HasKey(e => new { e.ItemId, e.Id });
+        builder.HasIndex(e => e.Id);
+        builder.HasIndex(e => new { e.ItemId, e.AncestorIdText });
+    }
+}

+ 42 - 0
Jellyfin.Server.Implementations/ModelConfiguration/BaseItemConfiguration.cs

@@ -0,0 +1,42 @@
+using System;
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration;
+
+/// <summary>
+/// Configuration for BaseItem.
+/// </summary>
+public class BaseItemConfiguration : IEntityTypeConfiguration<BaseItem>
+{
+    /// <inheritdoc/>
+    public void Configure(EntityTypeBuilder<BaseItem> builder)
+    {
+        builder.HasNoKey();
+        builder.HasIndex(e => e.Path);
+        builder.HasIndex(e => e.ParentId);
+        builder.HasIndex(e => e.PresentationUniqueKey);
+        builder.HasIndex(e => new { e.Id, e.Type, e.IsFolder, e.IsVirtualItem });
+        builder.HasIndex(e => new { e.UserDataKey, e.Type });
+
+        // covering index
+        builder.HasIndex(e => new { e.TopParentId, e.Id });
+        // series
+        builder.HasIndex(e => new { e.Type, e.SeriesPresentationUniqueKey, e.PresentationUniqueKey, e.SortName });
+        // series counts
+        // seriesdateplayed sort order
+        builder.HasIndex(e => new { e.Type, e.SeriesPresentationUniqueKey, e.IsFolder, e.IsVirtualItem });
+        // live tv programs
+        builder.HasIndex(e => new { e.Type, e.TopParentId, e.StartDate });
+        // covering index for getitemvalues
+        builder.HasIndex(e => new { e.Type, e.TopParentId, e.Id });
+        // used by movie suggestions
+        builder.HasIndex(e => new { e.Type, e.TopParentId, e.PresentationUniqueKey });
+        // latest items
+        builder.HasIndex(e => new { e.Type, e.TopParentId, e.IsVirtualItem, e.PresentationUniqueKey, e.DateCreated });
+        builder.HasIndex(e => new { e.IsFolder, e.TopParentId, e.IsVirtualItem, e.PresentationUniqueKey, e.DateCreated });
+        // resume
+        builder.HasIndex(e => new { e.MediaType, e.TopParentId, e.IsVirtualItem, e.PresentationUniqueKey });
+    }
+}

+ 20 - 0
Jellyfin.Server.Implementations/ModelConfiguration/ChapterConfiguration.cs

@@ -0,0 +1,20 @@
+using System;
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration;
+
+
+/// <summary>
+/// Chapter configuration.
+/// </summary>
+public class ChapterConfiguration : IEntityTypeConfiguration<Chapter>
+{
+    /// <inheritdoc/>
+    public void Configure(EntityTypeBuilder<Chapter> builder)
+    {
+        builder.HasNoKey();
+        builder.HasIndex(e => new { e.ItemId, e.ChapterIndex });
+    }
+}

+ 20 - 0
Jellyfin.Server.Implementations/ModelConfiguration/ItemValuesConfiguration.cs

@@ -0,0 +1,20 @@
+using System;
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration;
+
+/// <summary>
+/// itemvalues Configuration.
+/// </summary>
+public class ItemValuesConfiguration : IEntityTypeConfiguration<ItemValue>
+{
+    /// <inheritdoc/>
+    public void Configure(EntityTypeBuilder<ItemValue> builder)
+    {
+        builder.HasNoKey();
+        builder.HasIndex(e => new { e.ItemId, e.Type, e.CleanValue });
+        builder.HasIndex(e => new { e.ItemId, e.Type, e.Value });
+    }
+}

+ 20 - 0
Jellyfin.Server.Implementations/ModelConfiguration/PeopleConfiguration.cs

@@ -0,0 +1,20 @@
+using System;
+using Jellyfin.Data.Entities;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Metadata.Builders;
+
+namespace Jellyfin.Server.Implementations.ModelConfiguration;
+
+/// <summary>
+/// People configuration.
+/// </summary>
+public class PeopleConfiguration : IEntityTypeConfiguration<People>
+{
+    /// <inheritdoc/>
+    public void Configure(EntityTypeBuilder<People> builder)
+    {
+        builder.HasNoKey();
+        builder.HasIndex(e => new { e.ItemId, e.ListOrder });
+        builder.HasIndex(e => e.Name);
+    }
+}

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff