Refactor: Web Consolidation and Identity Stabilization #40
@@ -1,142 +1,139 @@
|
|||||||
@using Microsoft.AspNetCore.Components.Forms
|
@using Microsoft.AspNetCore.Components.Forms
|
||||||
@using NexusReader.Application.Abstractions.Services
|
@using NexusReader.Application.Abstractions.Services
|
||||||
@using NexusReader.Application.Queries.Reader
|
@using NexusReader.Application.Queries.Reader
|
||||||
@inject IEpubService EpubService
|
@implements IAsyncDisposable
|
||||||
@inject ILogger<BookIngestionModal> Logger
|
@inject IEpubMetadataExtractor EpubMetadataExtractor
|
||||||
|
|
||||||
@if (IsOpen)
|
<div class="ingestion-modal @(IsVisible ? "visible" : "")" role="dialog" aria-labelledby="modal-title" aria-modal="true">
|
||||||
{
|
<div class="modal-overlay" @onclick="CloseModal"></div>
|
||||||
<div class="modal-backdrop" @onclick="CloseModal">
|
<div class="modal-container">
|
||||||
<div class="modal-content glass-panel" @onclick:stopPropagation>
|
<header class="modal-header">
|
||||||
<div class="modal-header">
|
<NexusTypography Type="h2" id="modal-title">Dodaj nową książkę</NexusTypography>
|
||||||
<h2>Add New Book</h2>
|
<NexusButton Variant="ghost" OnClick="CloseModal" aria-label="Zamknij">
|
||||||
<button class="close-btn" @onclick="CloseModal">
|
<NexusIcon Name="close" Size="20" />
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
|
</NexusButton>
|
||||||
</button>
|
</header>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="modal-body">
|
<section class="modal-content">
|
||||||
<div class="parsing-state shimmer" style="@(IsParsing ? "display:flex;" : "display:none;")">
|
@if (Metadata == null && !IsParsing)
|
||||||
<div class="shimmer-content">
|
{
|
||||||
<div class="spinner"></div>
|
<div class="upload-zone @(IsDragging ? "dragging" : "")"
|
||||||
<p>Scanning metadata...</p>
|
@ondragenter="HandleDragEnter"
|
||||||
</div>
|
@ondragleave="HandleDragLeave"
|
||||||
|
@ondragover:preventDefault
|
||||||
|
@ondrop:preventDefault
|
||||||
|
@ondrop="HandleDrop">
|
||||||
|
<NexusIcon Name="upload" Size="48" />
|
||||||
|
<NexusTypography Type="body-large">Przeciągnij plik EPUB tutaj lub</NexusTypography>
|
||||||
|
<label class="file-input-label">
|
||||||
|
Przeglądaj pliki
|
||||||
|
<InputFile OnChange="HandleFileSelected" accept=".epub" />
|
||||||
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
}
|
||||||
<div class="metadata-state" style="@(Metadata != null && !IsParsing ? "display:flex;" : "display:none;")">
|
else if (IsParsing)
|
||||||
@if (Metadata != null)
|
{
|
||||||
{
|
<div class="parsing-state">
|
||||||
<div class="metadata-info">
|
<div class="spinner"></div>
|
||||||
<h3>@Metadata.Title</h3>
|
<NexusTypography Type="body">Analizowanie metadanych pliku...</NexusTypography>
|
||||||
<p class="author">@Metadata.Author</p>
|
</div>
|
||||||
</div>
|
}
|
||||||
|
else if (Metadata != null)
|
||||||
|
{
|
||||||
|
<div class="metadata-preview">
|
||||||
|
<div class="cover-wrapper">
|
||||||
|
@if (Metadata.CoverImage != null)
|
||||||
|
{
|
||||||
|
<img src="data:image/jpeg;base64,@Convert.ToBase64String(Metadata.CoverImage)" alt="Okładka @Metadata.Title" />
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<div class="no-cover">
|
||||||
|
<NexusIcon Name="book" Size="40" />
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<div class="metadata-info">
|
||||||
|
<NexusTypography Type="h3">@Metadata.Title</NexusTypography>
|
||||||
|
<NexusTypography Type="body-muted">@Metadata.Author</NexusTypography>
|
||||||
|
|
||||||
<div class="actions">
|
<div class="actions">
|
||||||
<button class="btn btn-primary">Confirm & Upload</button>
|
<NexusButton Variant="primary" OnClick="ConfirmIngestion">Dodaj do biblioteki</NexusButton>
|
||||||
<button class="btn btn-secondary" @onclick="Reset">Cancel</button>
|
<NexusButton Variant="secondary" OnClick="Reset">Wybierz inny plik</NexusButton>
|
||||||
</div>
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="upload-state @(_isDragging ? "drag-over" : "")"
|
|
||||||
style="@(!IsParsing && Metadata == null ? "display:flex;" : "display:none;")"
|
|
||||||
@ondragenter="OnDragEnter"
|
|
||||||
@ondragleave="OnDragLeave">
|
|
||||||
<div class="drop-zone">
|
|
||||||
<InputFile id="epub-upload" OnChange="HandleFileSelected" accept=".epub" class="file-input-cover" />
|
|
||||||
<div class="drop-zone-content">
|
|
||||||
<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="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="17 8 12 3 7 8"></polyline><line x1="12" y1="3" x2="12" y2="15"></line></svg>
|
|
||||||
<p>Drag and drop your .epub file here</p>
|
|
||||||
<span>or click to browse</span>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
}
|
||||||
|
</section>
|
||||||
@if (!string.IsNullOrEmpty(ErrorMessage))
|
|
||||||
{
|
|
||||||
<div class="error-message">
|
|
||||||
@ErrorMessage
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
}
|
</div>
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
[Parameter]
|
/// <summary>
|
||||||
public bool IsOpen { get; set; }
|
/// Gets or sets a value indicating whether the modal is visible.
|
||||||
|
/// </summary>
|
||||||
|
[Parameter] public bool IsVisible { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Event callback triggered when the modal is closed.
|
||||||
|
/// </summary>
|
||||||
|
[Parameter] public EventCallback OnClose { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Event callback triggered when a book is successfully ingested.
|
||||||
|
/// Passes the extracted metadata to the parent component.
|
||||||
|
/// </summary>
|
||||||
|
[Parameter] public EventCallback<LocalEpubMetadata> OnBookAdded { get; set; }
|
||||||
|
|
||||||
[Parameter]
|
private bool IsDragging { get; set; }
|
||||||
public EventCallback<bool> IsOpenChanged { get; set; }
|
|
||||||
|
|
||||||
private bool _isDragging;
|
|
||||||
private bool IsParsing { get; set; }
|
private bool IsParsing { get; set; }
|
||||||
private LocalEpubMetadata? Metadata { get; set; }
|
private LocalEpubMetadata? Metadata { get; set; }
|
||||||
private string? ErrorMessage { get; set; }
|
|
||||||
|
|
||||||
// Allow up to 50 MB
|
|
||||||
private const long MaxFileSize = 50 * 1024 * 1024;
|
|
||||||
|
|
||||||
private async Task CloseModal()
|
private async Task CloseModal()
|
||||||
{
|
{
|
||||||
IsOpen = false;
|
|
||||||
Reset();
|
|
||||||
await IsOpenChanged.InvokeAsync(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Reset()
|
|
||||||
{
|
|
||||||
IsParsing = false;
|
|
||||||
Metadata = null;
|
Metadata = null;
|
||||||
ErrorMessage = null;
|
await OnClose.InvokeAsync();
|
||||||
_isDragging = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDragEnter() => _isDragging = true;
|
private void HandleDragEnter() => IsDragging = true;
|
||||||
private void OnDragLeave() => _isDragging = false;
|
private void HandleDragLeave() => IsDragging = false;
|
||||||
|
|
||||||
|
private async Task HandleDrop(DragEventArgs e)
|
||||||
|
{
|
||||||
|
IsDragging = false;
|
||||||
|
// JS interop would be needed for actual drag-drop file access in Blazor WASM
|
||||||
|
// This is a placeholder for the interaction pattern
|
||||||
|
}
|
||||||
|
|
||||||
private async Task HandleFileSelected(InputFileChangeEventArgs e)
|
private async Task HandleFileSelected(InputFileChangeEventArgs e)
|
||||||
{
|
{
|
||||||
_isDragging = false;
|
|
||||||
var file = e.File;
|
var file = e.File;
|
||||||
|
|
||||||
if (file == null) return;
|
if (file == null) return;
|
||||||
|
|
||||||
if (!file.Name.EndsWith(".epub", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
ErrorMessage = "Only .epub files are supported.";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorMessage = null;
|
|
||||||
IsParsing = true;
|
IsParsing = true;
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var stream = file.OpenReadStream(MaxFileSize);
|
// Requirement: Extract metadata locally before uploading
|
||||||
|
// We limit the size for extraction safety
|
||||||
|
using var stream = file.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024); // 10MB
|
||||||
|
|
||||||
// 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 MemoryStream to ensure the provider doesn't close the stream prematurely
|
||||||
using var memoryStream = new MemoryStream();
|
using var memoryStream = new MemoryStream();
|
||||||
await stream.CopyToAsync(memoryStream);
|
await stream.CopyToAsync(memoryStream);
|
||||||
memoryStream.Position = 0;
|
memoryStream.Position = 0;
|
||||||
|
|
||||||
var result = await EpubService.ExtractMetadataAsync(memoryStream);
|
var result = await EpubMetadataExtractor.ExtractMetadataAsync(memoryStream);
|
||||||
|
|
||||||
if (result.IsSuccess)
|
if (result.IsSuccess)
|
||||||
{
|
{
|
||||||
Metadata = result.Value;
|
Metadata = result.Value;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
ErrorMessage = result.Errors.FirstOrDefault()?.Message ?? "Failed to parse EPUB.";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.LogError(ex, "Error uploading EPUB");
|
// Log or handle error
|
||||||
ErrorMessage = $"An unexpected error occurred: {ex.Message} \n {ex.StackTrace}";
|
Console.WriteLine($"Metadata extraction failed: {ex.Message}");
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -144,4 +141,25 @@
|
|||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task ConfirmIngestion()
|
||||||
|
{
|
||||||
|
if (Metadata != null)
|
||||||
|
{
|
||||||
|
await OnBookAdded.InvokeAsync(Metadata);
|
||||||
|
await CloseModal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Reset()
|
||||||
|
{
|
||||||
|
Metadata = null;
|
||||||
|
IsParsing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
// Cleanup if necessary
|
||||||
|
return ValueTask.CompletedTask;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user