using System; using System.IO; using System.Linq; using System.Text; using VersOne.Epub; using HtmlAgilityPack; using System.Threading; using xAiModels.Models; using xCommons.Extensions; using System.Threading.Tasks; using xAiApi.Interfaces.Extractors; namespace xAiApi.Providers.Extractors { /// /// Extractor for EPUB electronic publication format /// (application/epub+zip) /// public class XEpubFileContentExtractor : IXEpubFileContentExtractor { /// /// Supported MIME Types ... /// private static readonly string[] SupportedMimeTypes = [ "application/epub+zip" ]; /// /// Check if this extractor supports the specified MIME type ... /// /// /// public bool CanExtract(string mimeType) { // return SupportedMimeTypes.Contains( mimeType?.ToLowerInvariant() ?? string.Empty ); } /// /// Extract text content from file stream ... /// /// /// /// /// public async Task ExtractAsync( Stream fileStream, string mimeType, CancellationToken cancellationToken = default ) { // var result = await ExtractRichAsync( fileStream, "book.epub", mimeType, cancellationToken ); // return result.Text; } /// /// Extract content from stream as Rich Result ... /// /// /// /// /// /// public async Task ExtractRichAsync( Stream fileStream, string fileName, string mimeType, CancellationToken cancellationToken = default ) { // var result = new XFileExtractionResult { FileName = fileName, MimeType = mimeType }; // try { // result.Text = await ExtractEpubAsync( fileStream, cancellationToken ); } catch (Exception ex) { // result.ErrorMessage = $"EPUB extraction failed: {ex.Message}"; } // return result; } /// /// Extract Epub Content ... /// /// /// /// private async Task ExtractEpubAsync( Stream fileStream, CancellationToken cancellationToken ) { // // Copy to MemoryStream (VersOne.Epub requires seekable stream) ... await using var ms = new MemoryStream(); await fileStream.CopyToAsync(ms, cancellationToken); ms.Position = 0; // // Load EPUB book ... EpubBook book = await EpubReader.ReadBookAsync(ms); var sb = new StringBuilder(); // // Extract metadata ... // if (!book.Title.IsNullOrEmpty()) { sb.AppendLine($"Title: {book.Title}"); } // if (!book.Author.IsNullOrEmpty()) { sb.AppendLine($"Author: {book.Author}"); } // sb.AppendLine(); // // Extract chapters in reading order ... var chapterIndex = 0; foreach (var textContentFile in book.ReadingOrder) { // if (cancellationToken.IsCancellationRequested) { break; } // chapterIndex++; sb.AppendLine($"[Chapter {chapterIndex}]"); var plainText = ExtractTextFromHtml( textContentFile.Content ); if (!plainText.IsNullOrEmpty()) { sb.AppendLine(plainText); } // sb.AppendLine(); } // var result = sb.ToString().TrimEnd(); // return result; } /// /// Extract Text Content from HTML ... /// /// /// private string ExtractTextFromHtml(string html) { // var result = string.Empty; // if (html.IsNullOrEmpty()) { return result; } // var htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(html); // var sb = new StringBuilder(); var textNodes = htmlDocument.DocumentNode .SelectNodes("//text()"); // if (textNodes == null) { return result; } // foreach (var node in textNodes) { // var text = node.InnerText?.Trim(); if (!text.IsNullOrEmpty()) { sb.AppendLine(text); } } // result = sb.ToString().TrimEnd(); // return result; } } }