feat(ingestion): implement hybrid metadata verification form #34 (#41)

### 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:
2026-05-12 18:19:07 +00:00
committed by Marek Jaisński
parent fe5ff81c98
commit d5c2952bec
15 changed files with 533 additions and 24 deletions
@@ -1,8 +1,13 @@
@using Microsoft.AspNetCore.Components.Forms
@using NexusReader.Application.Abstractions.Services
@using NexusReader.Application.Queries.Reader
@using NexusReader.Application.Commands.Library
@using System.Net.Http.Json
@inject IEpubMetadataExtractor MetadataExtractor
@inject ILogger<BookIngestionModal> Logger
@inject HttpClient Http
@inject IReaderNavigationService ReaderNavigation
@inject IJSRuntime JSRuntime
@implements IAsyncDisposable
@if (IsOpen)
@@ -24,22 +29,48 @@
</div>
</div>
<div class="metadata-state" style="@(Metadata != null && !IsParsing ? "display:flex;" : "display:none;")">
<div class="verification-state" style="@(IsVerifying && !IsParsing ? "display:flex;" : "display:none;")">
@if (Metadata != null)
{
<div class="metadata-info">
<h3>@Metadata.Title</h3>
<p class="author">@Metadata.Author</p>
<div class="verification-layout">
<div class="cover-preview">
@if (Metadata.CoverImage != null)
{
<img src="data:image/jpeg;base64,@Convert.ToBase64String(Metadata.CoverImage)" alt="Cover Preview" />
}
else
{
<div class="glowing-placeholder">
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"></path><path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"></path></svg>
</div>
}
</div>
<div class="verification-form">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-input" @bind="Metadata.Title" placeholder="Enter book title" />
</div>
<div class="form-group">
<label>Author</label>
<input type="text" class="form-input" @bind="Metadata.Author" placeholder="Enter author name" />
</div>
</div>
</div>
<div class="actions">
<button class="btn btn-primary">Confirm & Upload</button>
<button class="btn btn-secondary" @onclick="Reset">Cancel</button>
<NexusButton Class="btn-secondary" OnClick="Reset" Disabled="IsIngesting">Back</NexusButton>
<NexusButton Class="@($"btn-primary {(IsIngesting ? "btn-loading" : "")}")"
OnClick="SaveToLibrary"
Disabled="IsIngesting">
@(IsIngesting ? "" : "Save to Library")
</NexusButton>
</div>
}
</div>
<div class="upload-state @(_isDragging ? "drag-over" : "")"
style="@(!IsParsing && Metadata == null ? "display:flex;" : "display:none;")"
style="@(!IsParsing && !IsVerifying ? "display:flex;" : "display:none;")"
@ondragenter="OnDragEnter"
@ondragleave="OnDragLeave">
<div class="drop-zone">
@@ -64,6 +95,8 @@
</div>
}
@code {
/// <summary>
/// Gets or sets a value indicating whether the modal is open.
@@ -79,8 +112,11 @@
private bool _isDragging;
private bool IsParsing { get; set; }
private bool IsVerifying { get; set; }
private bool IsIngesting { get; set; }
private LocalEpubMetadata? Metadata { get; set; }
private string? ErrorMessage { get; set; }
private byte[]? _epubBytes;
// Allow up to 50 MB
private const long MaxFileSize = 50 * 1024 * 1024;
@@ -95,9 +131,12 @@
private void Reset()
{
IsParsing = false;
IsVerifying = false;
IsIngesting = false;
Metadata = null;
ErrorMessage = null;
_isDragging = false;
_epubBytes = null;
}
private void OnDragEnter() => _isDragging = true;
@@ -123,17 +162,17 @@
try
{
using var stream = file.OpenReadStream(MaxFileSize);
// In Blazor WASM, we might need to copy to memory stream first for synchronous parsing if the parser doesn't stream well over interop
using var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream);
_epubBytes = memoryStream.ToArray();
memoryStream.Position = 0;
var result = await MetadataExtractor.ExtractMetadataAsync(memoryStream);
if (result.IsSuccess)
{
Metadata = result.Value;
IsVerifying = true;
}
else
{
@@ -143,7 +182,7 @@
catch (Exception ex)
{
Logger.LogError(ex, "Error uploading EPUB");
ErrorMessage = $"An unexpected error occurred: {ex.Message} \n {ex.StackTrace}";
ErrorMessage = $"An unexpected error occurred: {ex.Message}";
}
finally
{
@@ -151,9 +190,56 @@
StateHasChanged();
}
}
private async Task SaveToLibrary()
{
if (Metadata == null || _epubBytes == null) return;
IsIngesting = true;
ErrorMessage = null;
StateHasChanged();
try
{
var request = new IngestEbookRequest(
Metadata.Title,
Metadata.Author,
Metadata.CoverImage != null ? Convert.ToBase64String(Metadata.CoverImage) : null,
Convert.ToBase64String(_epubBytes)
);
var response = await Http.PostAsJsonAsync("api/library/ingest", request);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadFromJsonAsync<IngestResult>();
if (result != null)
{
await CloseModal();
ReaderNavigation.NavigateToBook(result.Id);
}
}
else
{
ErrorMessage = await response.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
Logger.LogError(ex, "Error during ingestion");
ErrorMessage = "Failed to save book to library. Please try again.";
}
finally
{
IsIngesting = false;
StateHasChanged();
}
}
private record IngestResult(Guid Id);
public ValueTask DisposeAsync()
{
// Cleanup if necessary
return ValueTask.CompletedTask;
}
}