### Description This PR implements **Issue #34: [UI/UX] Implement Hybrid Metadata Verification Form in Ingestion Modal**. ### Key Changes - **Metadata Verification State**: Introduced a new state in `BookIngestionModal.razor` allowing users to edit `Title` and `Author` before final ingestion. - **Cover Image Preview**: Added a high-fidelity cover preview with a CSS-based glowing placeholder fallback for books without embedded covers. - **Ingestion Pipeline**: - Implemented `IngestEbookCommand` and `IngestEbookCommandHandler`. - Added `IBookStorageService` and its implementation for managing EPUB and cover file storage. - Exposed `POST /api/library/ingest` Minimal API endpoint with `.DisableAntiforgery()` to handle client-side JSON uploads. - **Stability Fixes**: - Resolved DI validation errors in the WASM client by providing a dummy `IBookStorageService` registration. - Adjusted Kestrel request limits to handle large EPUB payloads (up to 100MB). - Corrected middleware ordering to ensure Antiforgery works correctly with Authentication. ### Verification - Solution builds successfully. - Manual verification of modal state transitions and API ingestion logic. Closes #34. --------- Co-authored-by: Marek Jasiński <jasins.marek@gmail.com> Reviewed-on: #41 Reviewed-by: Marek Jaisński <jasins.marek@gmail.com> Co-authored-by: Antigravity <antigravity@google.com> Co-committed-by: Antigravity <antigravity@google.com>
This commit was merged in pull request #41.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
namespace NexusReader.Application.Abstractions.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service for managing ebook and cover file storage.
|
||||
/// </summary>
|
||||
public interface IBookStorageService
|
||||
{
|
||||
/// <summary>
|
||||
/// Saves an ebook file and returns its relative path/URL.
|
||||
/// </summary>
|
||||
Task<string> SaveEbookAsync(byte[] data, string fileName);
|
||||
|
||||
/// <summary>
|
||||
/// Saves an ebook file using a stream and returns its relative path/URL.
|
||||
/// </summary>
|
||||
Task<string> SaveEbookAsync(Stream data, string fileName);
|
||||
|
||||
/// <summary>
|
||||
/// Saves a cover image and returns its relative path/URL.
|
||||
/// Returns null if no cover data is provided.
|
||||
/// </summary>
|
||||
Task<string?> SaveCoverAsync(byte[] data, string fileName);
|
||||
|
||||
/// <summary>
|
||||
/// Saves a cover image using a stream and returns its relative path/URL.
|
||||
/// Returns null if no cover data is provided.
|
||||
/// </summary>
|
||||
Task<string?> SaveCoverAsync(Stream data, string fileName);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using NexusReader.Application.Abstractions.Messaging;
|
||||
|
||||
namespace NexusReader.Application.Commands.Library;
|
||||
|
||||
/// <summary>
|
||||
/// Command to ingest a new ebook into the library.
|
||||
/// </summary>
|
||||
/// <param name="Title">The title of the book.</param>
|
||||
/// <param name="AuthorName">The name of the author.</param>
|
||||
/// <param name="CoverImage">The raw bytes of the cover image (optional).</param>
|
||||
/// <param name="EpubData">The raw bytes of the EPUB file.</param>
|
||||
/// <param name="UserId">The ID of the user owning the book.</param>
|
||||
/// <param name="TenantId">The tenant ID for multi-tenant isolation. Defaults to "global" for single-tenant or default usage.</param>
|
||||
public record IngestEbookCommand(
|
||||
string Title,
|
||||
string AuthorName,
|
||||
byte[]? CoverImage,
|
||||
byte[] EpubData,
|
||||
string UserId,
|
||||
string TenantId = "global"
|
||||
) : ICommand<Guid>;
|
||||
@@ -0,0 +1,85 @@
|
||||
using FluentResults;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NexusReader.Application.Abstractions.Messaging;
|
||||
using NexusReader.Application.Abstractions.Services;
|
||||
using NexusReader.Data.Persistence;
|
||||
using NexusReader.Domain.Entities;
|
||||
|
||||
namespace NexusReader.Application.Commands.Library;
|
||||
|
||||
public class IngestEbookCommandHandler : IRequestHandler<IngestEbookCommand, Result<Guid>>
|
||||
{
|
||||
private readonly IDbContextFactory<AppDbContext> _dbContextFactory;
|
||||
private readonly IBookStorageService _storageService;
|
||||
|
||||
public IngestEbookCommandHandler(
|
||||
IDbContextFactory<AppDbContext> dbContextFactory,
|
||||
IBookStorageService storageService)
|
||||
{
|
||||
_dbContextFactory = dbContextFactory;
|
||||
_storageService = storageService;
|
||||
}
|
||||
|
||||
public async Task<Result<Guid>> Handle(IngestEbookCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
using var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
string epubPath;
|
||||
string? coverUrl;
|
||||
|
||||
try
|
||||
{
|
||||
// 1. Save Files
|
||||
epubPath = await _storageService.SaveEbookAsync(request.EpubData, $"{request.Title}.epub");
|
||||
coverUrl = request.CoverImage != null && request.CoverImage.Length > 0
|
||||
? await _storageService.SaveCoverAsync(request.CoverImage, $"{request.Title}_cover.jpg")
|
||||
: null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error($"Storage failure: {ex.Message}").CausedBy(ex));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 2. Resolve Author
|
||||
var authorName = string.IsNullOrWhiteSpace(request.AuthorName) ? "Unknown Author" : request.AuthorName.Trim();
|
||||
|
||||
// Use case-insensitive comparison
|
||||
var author = await context.Authors
|
||||
.FirstOrDefaultAsync(a => a.Name.ToLower() == authorName.ToLower(), cancellationToken);
|
||||
|
||||
if (author == null)
|
||||
{
|
||||
author = new Author { Name = authorName };
|
||||
context.Authors.Add(author);
|
||||
}
|
||||
|
||||
// 3. Create Ebook
|
||||
var ebook = new Ebook
|
||||
{
|
||||
Title = request.Title,
|
||||
Author = author,
|
||||
FilePath = epubPath, // Relative URL from wwwroot
|
||||
CoverUrl = coverUrl,
|
||||
UserId = request.UserId,
|
||||
TenantId = request.TenantId,
|
||||
AddedDate = DateTime.UtcNow
|
||||
};
|
||||
|
||||
context.Ebooks.Add(ebook);
|
||||
await context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Result.Ok(ebook.Id);
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
return Result.Fail(new Error($"Database error during ingestion: {ex.Message}").CausedBy(ex));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Fail(new Error($"Unexpected error during ingestion: {ex.Message}").CausedBy(ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace NexusReader.Application.Commands.Library;
|
||||
|
||||
public record IngestEbookRequest(
|
||||
string Title,
|
||||
string AuthorName,
|
||||
string? CoverImageBase64,
|
||||
string EpubDataBase64
|
||||
);
|
||||
@@ -1,7 +1,22 @@
|
||||
namespace NexusReader.Application.Queries.Reader;
|
||||
|
||||
public record LocalEpubMetadata(
|
||||
string Title,
|
||||
string Author,
|
||||
byte[]? CoverImage = null
|
||||
);
|
||||
/// <summary>
|
||||
/// Represents metadata extracted from a local EPUB file.
|
||||
/// </summary>
|
||||
public record LocalEpubMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// The title of the book.
|
||||
/// </summary>
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// The author(s) of the book.
|
||||
/// </summary>
|
||||
public string Author { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// The raw bytes of the cover image, if available.
|
||||
/// </summary>
|
||||
public byte[]? CoverImage { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user