Files
Nexus.Reader/src/NexusReader.Web.Client/Services/WasmEpubService.cs
T
mjasin e1f1a4b3cb refactor: complete web project consolidation and stabilize identity flow
- Finalized move from NexusReader.Web.New to NexusReader.Web
- Implemented robust ServerIdentityService with UserManager and SignInManager
- Updated UI components to handle authentication state synchronization via force reload
- Refined BookIngestionModal styling following Nexus Neon design system
- Resolved namespace conflicts and updated CI/CD/VS Code configurations
- Fixes #33
2026-05-11 20:42:57 +02:00

59 lines
2.1 KiB
C#

using System.Net.Http.Json;
using FluentResults;
using NexusReader.Application.Abstractions.Services;
using NexusReader.Application.Queries.Reader;
namespace NexusReader.Web.Client.Services;
public class WasmEpubReader : IEpubReader
{
private readonly HttpClient _httpClient;
public WasmEpubReader(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<Result<ReaderPageViewModel>> GetEpubContentAsync(int chapterIndex, string? userId = null)
{
try
{
var response = await _httpClient.GetAsync($"/api/epub/{chapterIndex}");
if (response.IsSuccessStatusCode)
{
var viewModel = await response.Content.ReadFromJsonAsync<ReaderPageViewModel>();
return viewModel != null ? Result.Ok(viewModel) : Result.Fail("Failed to deserialize response.");
}
// Try to read the error message from the body
var errorBody = await response.Content.ReadAsStringAsync();
return Result.Fail($"Server error ({response.StatusCode}): {errorBody}");
}
catch (Exception ex)
{
// Fallback for network errors or parsing exceptions
return Result.Fail(new Error($"Network or parsing error: {ex.Message}").CausedBy(ex));
}
}
// Metadata extraction moved to WasmEpubMetadataExtractor
}
public class WasmEpubMetadataExtractor : IEpubMetadataExtractor
{
public async Task<Result<LocalEpubMetadata>> ExtractMetadataAsync(Stream epubStream)
{
try
{
using var bookRef = await VersOne.Epub.EpubReader.OpenBookAsync(epubStream);
var title = bookRef.Title ?? "Unknown Title";
var author = bookRef.Author ?? "Unknown Author";
byte[]? cover = await bookRef.ReadCoverAsync();
return Result.Ok(new LocalEpubMetadata(title, author, cover));
}
catch (Exception ex)
{
return Result.Fail(new Error($"Failed to extract EPUB metadata locally: {ex.Message}").CausedBy(ex));
}
}
}