add Epub, Html, PowerPoint, RTF File Content Extractor and etc ...
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
using System;
|
||||
using RtfPipe;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using xAiModels.Models;
|
||||
using xCommons.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
using xAiApi.Interfaces.Extractors;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace xAiApi.Providers.Extractors
|
||||
{
|
||||
/// <summary>
|
||||
/// Extractor for Rich Text Format (RTF) documents
|
||||
/// (application/rtf, text/rtf)
|
||||
/// </summary>
|
||||
public class XRtfFileContentExtractor : IXRtfFileContentExtractor
|
||||
{
|
||||
/// <summary>
|
||||
/// Supported MIME Types ...
|
||||
/// </summary>
|
||||
private static readonly string[] SupportedMimeTypes =
|
||||
[
|
||||
"text/rtf",
|
||||
"application/rtf",
|
||||
];
|
||||
|
||||
/// <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,
|
||||
"document.rtf",
|
||||
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 ExtractRtfAsync(
|
||||
fileStream,
|
||||
cancellationToken
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
result.ErrorMessage =
|
||||
$"RTF extraction failed: {ex.Message}";
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract RTF Content ...
|
||||
/// </summary>
|
||||
/// <param name="fileStream"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<string> ExtractRtfAsync(
|
||||
Stream fileStream,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = string.Empty;
|
||||
|
||||
//
|
||||
// Read RTF content as string ...
|
||||
using var reader = new StreamReader(
|
||||
fileStream,
|
||||
Encoding.UTF8,
|
||||
detectEncodingFromByteOrderMarks: true,
|
||||
leaveOpen: true
|
||||
);
|
||||
var rtfContent = await reader.ReadToEndAsync();
|
||||
if (rtfContent.IsNullOrEmpty())
|
||||
{
|
||||
result = string.Empty;
|
||||
}
|
||||
|
||||
//
|
||||
// Convert RTF to HTML using RtfPipe ...
|
||||
var html = Rtf.ToHtml(rtfContent);
|
||||
|
||||
//
|
||||
// Convert HTML to plain text ...
|
||||
result = HtmlToPlainText(html);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts Html to Text ...
|
||||
/// </summary>
|
||||
/// <param name="html"></param>
|
||||
/// <returns></returns>
|
||||
private string HtmlToPlainText(string html)
|
||||
{
|
||||
//
|
||||
var result = string.Empty;
|
||||
|
||||
//
|
||||
if (html.IsNullOrEmpty())
|
||||
{
|
||||
result = string.Empty;
|
||||
}
|
||||
|
||||
//
|
||||
// Remove HTML tags ...
|
||||
result = Regex.Replace(
|
||||
html,
|
||||
"<.*?>",
|
||||
string.Empty,
|
||||
RegexOptions.Singleline
|
||||
);
|
||||
|
||||
//
|
||||
// Decode HTML entities ...
|
||||
result = System.Net.WebUtility.HtmlDecode(result);
|
||||
|
||||
//
|
||||
// Normalize whitespace ...
|
||||
result = Regex.Replace(result, @"\s+", " ").Trim();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user