231 lines
6.0 KiB
C#
231 lines
6.0 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Extractor for EPUB electronic publication format
|
|
/// (application/epub+zip)
|
|
/// </summary>
|
|
public class XEpubFileContentExtractor : IXEpubFileContentExtractor
|
|
{
|
|
/// <summary>
|
|
/// Supported MIME Types ...
|
|
/// </summary>
|
|
private static readonly string[] SupportedMimeTypes =
|
|
[
|
|
"application/epub+zip"
|
|
];
|
|
|
|
/// <summary>
|
|
/// Check if this extractor supports the specified MIME type ...
|
|
/// </summary>
|
|
/// <param name="mimeType"></param>
|
|
/// <returns></returns>
|
|
public bool CanExtract(string mimeType)
|
|
{
|
|
//
|
|
return SupportedMimeTypes.Contains(
|
|
mimeType?.ToLowerInvariant() ?? string.Empty
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract text content from file stream ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> ExtractAsync(
|
|
Stream fileStream,
|
|
string mimeType,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await ExtractRichAsync(
|
|
fileStream,
|
|
"book.epub",
|
|
mimeType,
|
|
cancellationToken
|
|
);
|
|
|
|
//
|
|
return result.Text;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract content from stream as Rich Result ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileExtractionResult> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Epub Content ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<string> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Text Content from HTML ...
|
|
/// </summary>
|
|
/// <param name="html"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
} |