refactor: split WasmEpubService into reader and metadata extractor implementations

This commit is contained in:
2026-05-11 18:07:37 +00:00
parent bb7207b7ef
commit cb3d8726b1
@@ -2,14 +2,15 @@ using System.Net.Http.Json;
using FluentResults; using FluentResults;
using NexusReader.Application.Abstractions.Services; using NexusReader.Application.Abstractions.Services;
using NexusReader.Application.Queries.Reader; using NexusReader.Application.Queries.Reader;
using VersOne.Epub;
namespace NexusReader.Web.Client.Services; namespace NexusReader.Web.Client.Services;
public class WasmEpubService : IEpubService public class WasmEpubReader : IEpubReader
{ {
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
public WasmEpubService(HttpClient httpClient) public WasmEpubReader(HttpClient httpClient)
{ {
_httpClient = httpClient; _httpClient = httpClient;
} }
@@ -18,28 +19,28 @@ public class WasmEpubService : IEpubService
{ {
try try
{ {
var response = await _httpClient.GetAsync($"/api/epub/{chapterIndex}"); var response = await _httpClient.GetAsync($"api/epub/content?index={chapterIndex}&userId={userId}");
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {
var viewModel = await response.Content.ReadFromJsonAsync<ReaderPageViewModel>(); var result = await response.Content.ReadFromJsonAsync<ReaderPageViewModel>();
return viewModel != null ? Result.Ok(viewModel) : Result.Fail("Failed to deserialize response."); return result != null ? Result.Ok(result) : Result.Fail("Failed to deserialize reader page.");
} }
return Result.Fail("Failed to fetch EPUB content from server.");
// 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) catch (Exception ex)
{ {
// Fallback for network errors or parsing exceptions return Result.Fail(new Error("Network error while fetching EPUB content.").CausedBy(ex));
return Result.Fail(new Error($"Network or parsing error: {ex.Message}").CausedBy(ex));
} }
} }
}
public class WasmEpubMetadataExtractor : IEpubMetadataExtractor
{
public async Task<Result<LocalEpubMetadata>> ExtractMetadataAsync(Stream epubStream) public async Task<Result<LocalEpubMetadata>> ExtractMetadataAsync(Stream epubStream)
{ {
try try
{ {
using var bookRef = await VersOne.Epub.EpubReader.OpenBookAsync(epubStream); using var bookRef = await EpubReader.OpenBookAsync(epubStream);
var title = bookRef.Title ?? "Unknown Title"; var title = bookRef.Title ?? "Unknown Title";
var author = bookRef.Author ?? "Unknown Author"; var author = bookRef.Author ?? "Unknown Author";
byte[]? cover = await bookRef.ReadCoverAsync(); byte[]? cover = await bookRef.ReadCoverAsync();