fix: #7 large EPUB parsing performance

This commit is contained in:
2026-05-07 20:30:38 +02:00
parent 775fb73fa9
commit 0dad1cbd2b
@@ -46,34 +46,29 @@ public class EpubService : IEpubService
return Result.Fail($"EPUB file at '{fullPath}' is not accessible or does not exist.");
}
EpubBook book;
try
{
book = await EpubReader.ReadBookAsync(fullPath);
}
catch (Exception ex)
{
return Result.Fail(new Error($"Failed to parse EPUB file. It might be corrupted or in use. Path: {fullPath}").CausedBy(ex));
}
var blocks = new List<ContentBlock>();
int totalWordCount = 0;
int blockCounter = 0;
using var bookRef = await EpubReader.OpenBookAsync(fullPath);
var readingOrder = bookRef.GetReadingOrder();
if (book.ReadingOrder == null || !book.ReadingOrder.Any())
if (readingOrder == null || !readingOrder.Any())
{
return Result.Fail("The EPUB has no readable content files in ReadingOrder.");
}
// Ensure index is within bounds
if (chapterIndex < 0 || chapterIndex >= book.ReadingOrder.Count)
if (chapterIndex < 0 || chapterIndex >= readingOrder.Count)
{
chapterIndex = 0; // Default to first chapter
}
var chapter = book.ReadingOrder[chapterIndex];
var chapterTitle = chapter.FilePath ?? $"Chapter {chapterIndex + 1}";
var chapterRef = readingOrder[chapterIndex];
var chapterTitle = chapterRef.FilePath ?? $"Chapter {chapterIndex + 1}";
var chapterContent = await chapterRef.ReadContentAsTextAsync();
var paragraphs = ExtractParagraphs(chapter.Content);
var blocks = new List<ContentBlock>();
int totalWordCount = 0;
int blockCounter = 0;
var paragraphs = ExtractParagraphs(chapterContent);
foreach (var p in paragraphs)
{
var sanitizedContent = SanitizeParagraph(p);
@@ -99,7 +94,7 @@ public class EpubService : IEpubService
blocks.Add(CreateAiTrigger($"trigger-{blockCounter++}"));
}
return Result.Ok(new ReaderPageViewModel(blocks, chapterIndex, book.ReadingOrder.Count, chapterTitle));
return Result.Ok(new ReaderPageViewModel(blocks, chapterIndex, readingOrder.Count, chapterTitle));
}
catch (Exception ex)
{