From cb3d8726b1459f0bf18d40afbda3de4d58f88c08 Mon Sep 17 00:00:00 2001 From: Antigravity Date: Mon, 11 May 2026 18:07:37 +0000 Subject: [PATCH] refactor: split WasmEpubService into reader and metadata extractor implementations --- .../Services/WasmEpubService.cs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/NexusReader.Web.Client/Services/WasmEpubService.cs b/src/NexusReader.Web.Client/Services/WasmEpubService.cs index d9b24a3..a6a5421 100644 --- a/src/NexusReader.Web.Client/Services/WasmEpubService.cs +++ b/src/NexusReader.Web.Client/Services/WasmEpubService.cs @@ -2,14 +2,15 @@ using System.Net.Http.Json; using FluentResults; using NexusReader.Application.Abstractions.Services; using NexusReader.Application.Queries.Reader; +using VersOne.Epub; namespace NexusReader.Web.Client.Services; -public class WasmEpubService : IEpubService +public class WasmEpubReader : IEpubReader { private readonly HttpClient _httpClient; - public WasmEpubService(HttpClient httpClient) + public WasmEpubReader(HttpClient httpClient) { _httpClient = httpClient; } @@ -18,28 +19,28 @@ public class WasmEpubService : IEpubService { try { - var response = await _httpClient.GetAsync($"/api/epub/{chapterIndex}"); + var response = await _httpClient.GetAsync($"api/epub/content?index={chapterIndex}&userId={userId}"); if (response.IsSuccessStatusCode) { - var viewModel = await response.Content.ReadFromJsonAsync(); - return viewModel != null ? Result.Ok(viewModel) : Result.Fail("Failed to deserialize response."); + var result = await response.Content.ReadFromJsonAsync(); + return result != null ? Result.Ok(result) : Result.Fail("Failed to deserialize reader page."); } - - // Try to read the error message from the body - var errorBody = await response.Content.ReadAsStringAsync(); - return Result.Fail($"Server error ({response.StatusCode}): {errorBody}"); + return Result.Fail("Failed to fetch EPUB content from server."); } catch (Exception ex) { - // Fallback for network errors or parsing exceptions - return Result.Fail(new Error($"Network or parsing error: {ex.Message}").CausedBy(ex)); + return Result.Fail(new Error("Network error while fetching EPUB content.").CausedBy(ex)); } } +} + +public class WasmEpubMetadataExtractor : IEpubMetadataExtractor +{ public async Task> ExtractMetadataAsync(Stream epubStream) { try { - using var bookRef = await VersOne.Epub.EpubReader.OpenBookAsync(epubStream); + using var bookRef = await EpubReader.OpenBookAsync(epubStream); var title = bookRef.Title ?? "Unknown Title"; var author = bookRef.Author ?? "Unknown Author"; byte[]? cover = await bookRef.ReadCoverAsync();