fix: #7 large EPUB parsing performance
This commit is contained in:
@@ -46,34 +46,29 @@ public class EpubService : IEpubService
|
|||||||
return Result.Fail($"EPUB file at '{fullPath}' is not accessible or does not exist.");
|
return Result.Fail($"EPUB file at '{fullPath}' is not accessible or does not exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
EpubBook book;
|
using var bookRef = await EpubReader.OpenBookAsync(fullPath);
|
||||||
try
|
var readingOrder = bookRef.GetReadingOrder();
|
||||||
{
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (book.ReadingOrder == null || !book.ReadingOrder.Any())
|
if (readingOrder == null || !readingOrder.Any())
|
||||||
{
|
{
|
||||||
return Result.Fail("The EPUB has no readable content files in ReadingOrder.");
|
return Result.Fail("The EPUB has no readable content files in ReadingOrder.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure index is within bounds
|
// Ensure index is within bounds
|
||||||
if (chapterIndex < 0 || chapterIndex >= book.ReadingOrder.Count)
|
if (chapterIndex < 0 || chapterIndex >= readingOrder.Count)
|
||||||
{
|
{
|
||||||
chapterIndex = 0; // Default to first chapter
|
chapterIndex = 0; // Default to first chapter
|
||||||
}
|
}
|
||||||
|
|
||||||
var chapter = book.ReadingOrder[chapterIndex];
|
var chapterRef = readingOrder[chapterIndex];
|
||||||
var chapterTitle = chapter.FilePath ?? $"Chapter {chapterIndex + 1}";
|
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)
|
foreach (var p in paragraphs)
|
||||||
{
|
{
|
||||||
var sanitizedContent = SanitizeParagraph(p);
|
var sanitizedContent = SanitizeParagraph(p);
|
||||||
@@ -99,7 +94,7 @@ public class EpubService : IEpubService
|
|||||||
blocks.Add(CreateAiTrigger($"trigger-{blockCounter++}"));
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user