Adds book reading support with EPUB and PDF reader plugins

This commit is contained in:
BinaryAlley committed 2026-09-02 04:45:11 -07:00
1 parent 275fd83ff2
commit f7ccdf3d1c
271 files changed
+11501 -361

No files matched your search

@@ -0,0 +1,57 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
#endregion
namespace Lumina.DataAccess.Common.Configuration;
/// <summary>
/// Configures the entity mapping for the <see cref="LibraryBookReaderConfigurationEntity"/> entity.
/// </summary>
public class LibraryBookReaderConfigurationConfiguration : IEntityTypeConfiguration<LibraryBookReaderConfigurationEntity>
{
/// <summary>
/// Configures the <see cref="LibraryBookReaderConfigurationEntity"/> entity.
/// </summary>
/// <param name="builder">The builder to be used to configure the entity.</param>
public void Configure(EntityTypeBuilder<LibraryBookReaderConfigurationEntity> builder)
{
builder.ToTable("LibraryBookReaderConfigurations");
builder.HasKey(configuration => configuration.Id);
builder.Property(configuration => configuration.Id)
.ValueGeneratedNever() // because EF always tries to generate the value for the Id, and because we generate it as part of the aggregate root, we need to tell EF not to generate it
.HasColumnOrder(0);
builder.Property(configuration => configuration.LibraryId)
.IsRequired()
.HasColumnOrder(1);
builder.Property(configuration => configuration.PluginId)
.IsRequired()
.HasColumnOrder(2);
builder.Property(configuration => configuration.IsEnabled)
.IsRequired()
.HasColumnOrder(3);
// audit
builder.Property(configuration => configuration.CreatedOnUtc)
.IsRequired()
.HasColumnOrder(4);
builder.Property(configuration => configuration.CreatedBy)
.IsRequired()
.HasColumnOrder(5);
builder.Property(configuration => configuration.UpdatedOnUtc)
.HasDefaultValue(null)
.HasColumnOrder(6);
builder.Property(configuration => configuration.UpdatedBy)
.HasDefaultValue(null)
.HasColumnOrder(7);
// A media library has at most one book reader configuration per plugin, so the pair is unique; the repository upserts on it, and
// the constraint guarantees that two concurrent upserts cannot insert duplicate rows.
builder.HasIndex(configuration => new { configuration.LibraryId, configuration.PluginId })
.IsUnique();
}
}
@@ -50,6 +50,14 @@ public class UserSettingsConfiguration : IEntityTypeConfiguration<UserSettingsEn
.IsRequired()
.HasColumnOrder(10);
builder.Property(settings => settings.ShouldRenderPdfAsImages)
.IsRequired()
.HasColumnOrder(11);
builder.Property(settings => settings.ShouldPreserveBookStyles)
.IsRequired()
.HasColumnOrder(12);
builder.Property(settings => settings.IsThemeCachingEnabled)
.IsRequired()
.HasColumnOrder(9);
@@ -7,7 +7,6 @@ using Lumina.DataAccess.Core.Seed;
using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
@@ -16,13 +15,13 @@ using System.Reflection;
namespace Lumina.DataAccess.Common.DependencyInjection;
/// <summary>
/// Contains all services of the DataAccess layer.
/// Utility class for registering the services of the Data Access layer into the Dependency Injection container..
/// </summary>
[ExcludeFromCodeCoverage]
public static class DataAccessLayerServices
{
/// <summary>
/// Extension method for adding the DataAccess layer services to the DI container.
/// Registers the services of the Data Access layer into the Dependency Injection container.
/// </summary>
/// <param name="services">The service collection to add the services to.</param>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
@@ -0,0 +1,70 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Lumina.DataAccess.Common.Migrations
{
/// <inheritdoc />
public partial class AddsLibraryBookReaderConfigurationsAndShouldRenderPdfAsImagesToUserSettings : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "ShouldRenderPdfAsImages",
table: "UserSettings",
type: "INTEGER",
nullable: false,
defaultValue: false)
.Annotation("Relational:ColumnOrder", 11);
migrationBuilder.AddColumn<bool>(
name: "ShouldPreserveBookStyles",
table: "UserSettings",
type: "INTEGER",
nullable: false,
defaultValue: true)
.Annotation("Relational:ColumnOrder", 12);
migrationBuilder.CreateTable(
name: "LibraryBookReaderConfigurations",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
LibraryId = table.Column<Guid>(type: "TEXT", nullable: false),
PluginId = table.Column<Guid>(type: "TEXT", nullable: false),
IsEnabled = table.Column<bool>(type: "INTEGER", nullable: false),
CreatedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
CreatedBy = table.Column<Guid>(type: "TEXT", nullable: false),
UpdatedOnUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
UpdatedBy = table.Column<Guid>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_LibraryBookReaderConfigurations", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_LibraryBookReaderConfigurations_LibraryId_PluginId",
table: "LibraryBookReaderConfigurations",
columns: new[] { "LibraryId", "PluginId" },
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "LibraryBookReaderConfigurations");
migrationBuilder.DropColumn(
name: "ShouldRenderPdfAsImages",
table: "UserSettings");
migrationBuilder.DropColumn(
name: "ShouldPreserveBookStyles",
table: "UserSettings");
}
}
}
@@ -929,6 +929,48 @@ namespace Lumina.DataAccess.Common.Migrations
b.ToTable("LibraryArtworkProviderConfigurations", (string)null);
});
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Plugins.LibraryBookReaderConfigurationEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
.HasColumnOrder(0);
b.Property<Guid>("CreatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(5);
b.Property<DateTime>("CreatedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(4);
b.Property<bool>("IsEnabled")
.HasColumnType("INTEGER")
.HasColumnOrder(3);
b.Property<Guid>("LibraryId")
.HasColumnType("TEXT")
.HasColumnOrder(1);
b.Property<Guid>("PluginId")
.HasColumnType("TEXT")
.HasColumnOrder(2);
b.Property<Guid?>("UpdatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(7);
b.Property<DateTime?>("UpdatedOnUtc")
.HasColumnType("TEXT")
.HasColumnOrder(6);
b.HasKey("Id");
b.HasIndex("LibraryId", "PluginId")
.IsUnique();
b.ToTable("LibraryBookReaderConfigurations", (string)null);
});
modelBuilder.Entity("Lumina.Application.Common.DataAccess.Entities.Plugins.LibraryMetadataProviderConfigurationEntity", b =>
{
b.Property<Guid>("Id")
@@ -1215,6 +1257,14 @@ namespace Lumina.DataAccess.Common.Migrations
.HasColumnType("INTEGER")
.HasColumnOrder(4);
b.Property<bool>("ShouldRenderPdfAsImages")
.HasColumnType("INTEGER")
.HasColumnOrder(11);
b.Property<bool>("ShouldPreserveBookStyles")
.HasColumnType("INTEGER")
.HasColumnOrder(12);
b.Property<Guid?>("UpdatedBy")
.HasColumnType("TEXT")
.HasColumnOrder(8);
@@ -0,0 +1,130 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Primitives;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Plugins;
/// <summary>
/// Repository for the book reader configurations of media libraries.
/// </summary>
internal sealed class LibraryBookReaderConfigurationRepository : ILibraryBookReaderConfigurationRepository
{
private readonly LuminaDbContext _luminaDbContext;
/// <summary>
/// Initializes a new instance of the <see cref="LibraryBookReaderConfigurationRepository"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public LibraryBookReaderConfigurationRepository(LuminaDbContext luminaDbContext)
{
_luminaDbContext = luminaDbContext;
}
/// <summary>
/// Gets all the book reader configurations of the media library identified by <paramref name="libraryId"/>.
/// </summary>
/// <param name="libraryId">The Id of the media library whose book reader configurations are retrieved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="LibraryBookReaderConfigurationEntity"/>, or an error.</returns>
public async Task<Result<IReadOnlyList<LibraryBookReaderConfigurationEntity>>> GetByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken)
{
return await _luminaDbContext.LibraryBookReaderConfigurations
.Where(configuration => configuration.LibraryId == libraryId)
.ToListAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the book reader configuration of the media library identified by <paramref name="libraryId"/> for the plugin identified by <paramref name="pluginId"/>.
/// </summary>
/// <param name="libraryId">The Id of the media library whose book reader configuration is retrieved.</param>
/// <param name="pluginId">The Id of the plugin whose book reader configuration is retrieved.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the <see cref="LibraryBookReaderConfigurationEntity"/>, or an error.</returns>
public async Task<Result<LibraryBookReaderConfigurationEntity?>> GetByLibraryAndPluginIdAsync(Guid libraryId, Guid pluginId, CancellationToken cancellationToken)
{
return await _luminaDbContext.LibraryBookReaderConfigurations
.FirstOrDefaultAsync(configuration => configuration.LibraryId == libraryId && configuration.PluginId == pluginId, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Inserts a book reader configuration into the storage medium, or updates it when it already exists.
/// </summary>
/// <param name="configuration">The book reader configuration to insert or update.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Updated>> UpsertAsync(LibraryBookReaderConfigurationEntity configuration, CancellationToken cancellationToken)
{
LibraryBookReaderConfigurationEntity? existingConfiguration = await _luminaDbContext.LibraryBookReaderConfigurations
.FirstOrDefaultAsync(repositoryConfiguration => repositoryConfiguration.LibraryId == configuration.LibraryId && repositoryConfiguration.PluginId == configuration.PluginId, cancellationToken).ConfigureAwait(false);
if (existingConfiguration is null)
_luminaDbContext.LibraryBookReaderConfigurations.Add(configuration);
else
{
// Update only the mutable fields, preserving the Id of the existing configuration.
existingConfiguration.LibraryId = configuration.LibraryId;
existingConfiguration.PluginId = configuration.PluginId;
existingConfiguration.IsEnabled = configuration.IsEnabled;
}
return Result.Updated;
}
/// <summary>
/// Deletes all the book reader configurations of the media library identified by <paramref name="libraryId"/>.
/// </summary>
/// <param name="libraryId">The Id of the media library whose book reader configurations are deleted.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Deleted>> DeleteByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken)
{
List<LibraryBookReaderConfigurationEntity> configurations = await _luminaDbContext.LibraryBookReaderConfigurations
.Where(configuration => configuration.LibraryId == libraryId)
.ToListAsync(cancellationToken).ConfigureAwait(false);
if (configurations.Count > 0)
_luminaDbContext.LibraryBookReaderConfigurations.RemoveRange(configurations);
return Result.Deleted;
}
/// <summary>
/// Deletes all the book reader configurations referencing the plugin identified by <paramref name="pluginId"/>.
/// </summary>
/// <param name="pluginId">The Id of the plugin whose book reader configurations are deleted.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Deleted>> DeleteByPluginIdAsync(Guid pluginId, CancellationToken cancellationToken)
{
List<LibraryBookReaderConfigurationEntity> configurations = await _luminaDbContext.LibraryBookReaderConfigurations
.Where(configuration => configuration.PluginId == pluginId)
.ToListAsync(cancellationToken).ConfigureAwait(false);
if (configurations.Count > 0)
_luminaDbContext.LibraryBookReaderConfigurations.RemoveRange(configurations);
return Result.Deleted;
}
/// <summary>
/// Deletes the book reader configurations of the media library identified by <paramref name="libraryId"/> that reference
/// one of the plugins identified by <paramref name="pluginIds"/>.
/// </summary>
/// <param name="libraryId">The Id of the media library whose book reader configurations are deleted.</param>
/// <param name="pluginIds">The Ids of the plugins whose configurations of the media library are deleted.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Deleted>> DeleteByLibraryIdAndPluginIdsAsync(Guid libraryId, IEnumerable<Guid> pluginIds, CancellationToken cancellationToken)
{
List<Guid> pluginIdList = [.. pluginIds];
List<LibraryBookReaderConfigurationEntity> configurations = await _luminaDbContext.LibraryBookReaderConfigurations
.Where(configuration => configuration.LibraryId == libraryId && pluginIdList.Contains(configuration.PluginId))
.ToListAsync(cancellationToken).ConfigureAwait(false);
if (configurations.Count > 0)
_luminaDbContext.LibraryBookReaderConfigurations.RemoveRange(configurations);
return Result.Deleted;
}
}
@@ -35,6 +35,7 @@ public class LuminaDbContext : DbContext
public virtual DbSet<PluginEntity> Plugins { get; set; } = null!;
public virtual DbSet<LibraryMetadataProviderConfigurationEntity> LibraryMetadataProviderConfigurations { get; set; } = null!;
public virtual DbSet<LibraryArtworkProviderConfigurationEntity> LibraryArtworkProviderConfigurations { get; set; } = null!;
public virtual DbSet<LibraryBookReaderConfigurationEntity> LibraryBookReaderConfigurations { get; set; } = null!;
public virtual DbSet<ThemeEntity> Themes { get; set; } = null!;
public virtual DbSet<MediaContributorEntity> MediaContributors { get; set; } = null!;
public virtual DbSet<BookContributorEntity> BookContributors { get; set; } = null!;
@@ -43,6 +43,7 @@ public class UnitOfWork : IUnitOfWork, IDisposable
private ILibraryScanStagingResultsRepository? _libraryScanStagingResultsRepository;
private ILibraryMetadataProviderConfigurationRepository? _libraryMetadataProviderConfigurationRepository;
private IArtworkProviderConfigurationRepository? _artworkProviderConfigurationRepository;
private ILibraryBookReaderConfigurationRepository? _libraryBookReaderConfigurationRepository;
private IPluginRepository? _pluginRepository;
private IThemeRepository? _themeRepository;
private IUserRepository? _userRepository;
@@ -201,6 +202,18 @@ public class UnitOfWork : IUnitOfWork, IDisposable
}
}
/// <summary>
/// Gets the library book reader configuration repository.
/// </summary>
public ILibraryBookReaderConfigurationRepository LibraryBookReaderConfigurationRepository
{
get
{
_libraryBookReaderConfigurationRepository ??= new LibraryBookReaderConfigurationRepository(_luminaDbContext);
return _libraryBookReaderConfigurationRepository;
}
}
/// <summary>
/// Gets the plugin repository.
/// </summary>