add Epub, Html, PowerPoint, RTF File Content Extractor and etc ...
This commit is contained in:
@@ -25,26 +25,6 @@ namespace xAiApi.Providers.Extractors
|
||||
"application/pdf"
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Minimum Length for Fallback Using Images ...
|
||||
/// </summary>
|
||||
private const int MinTextLengthThreshold = 50;
|
||||
|
||||
/// <summary>
|
||||
/// Max Pages for Falling Back Using Images ...
|
||||
/// </summary>
|
||||
private const int MaxPagesForImageFallback = 10;
|
||||
|
||||
/// <summary>
|
||||
/// Default DPI for Rendering Images ...
|
||||
/// </summary>
|
||||
private const int RenderDpi = 150;
|
||||
|
||||
/// <summary>
|
||||
/// Max Rendered Image Width ...
|
||||
/// </summary>
|
||||
private const int MaxImageWidth = 2000;
|
||||
|
||||
/// <summary>
|
||||
/// Api Configuration ...
|
||||
/// </summary>
|
||||
@@ -58,6 +38,8 @@ namespace xAiApi.Providers.Extractors
|
||||
/// <summary>
|
||||
/// Check if this extractor supports the specified MIME type ...
|
||||
/// </summary>
|
||||
/// <param name="mimeType"></param>
|
||||
/// <returns></returns>
|
||||
public bool CanExtract(string mimeType)
|
||||
{
|
||||
//
|
||||
@@ -69,6 +51,10 @@ namespace xAiApi.Providers.Extractors
|
||||
/// <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,
|
||||
@@ -182,6 +168,8 @@ namespace xAiApi.Providers.Extractors
|
||||
/// Check Extracted Content is Sufficient or not ...
|
||||
/// if not try to Falling Back using Images ...
|
||||
/// </summary>
|
||||
/// <param name="text"></param>
|
||||
/// <returns></returns>
|
||||
private bool IsTextSufficient(string text)
|
||||
{
|
||||
//
|
||||
@@ -195,12 +183,20 @@ namespace xAiApi.Providers.Extractors
|
||||
var cleanText = new string(text.Where(c => !char.IsWhiteSpace(c)).ToArray());
|
||||
|
||||
//
|
||||
return cleanText.Length >= MinTextLengthThreshold;
|
||||
var result = cleanText.Length >= configuration.PDFToImageDescriptor.MinTextLengthThreshold;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fallback Converts PDF Pages to Images ...
|
||||
/// </summary>
|
||||
/// <param name="pdfStream"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="mimeType"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XFileExtractionResult> FallbackToImageExtractionAsync(
|
||||
Stream pdfStream,
|
||||
string fileName,
|
||||
@@ -223,30 +219,48 @@ namespace xAiApi.Providers.Extractors
|
||||
try
|
||||
{
|
||||
//
|
||||
var images = Conversion.ToImages(
|
||||
pdfStream: pdfStream,
|
||||
options: new RenderOptions
|
||||
{
|
||||
Dpi = configuration.PDFToImageDescriptor.Dpi,
|
||||
Width = configuration.PDFToImageDescriptor.Width,
|
||||
Height = configuration.PDFToImageDescriptor.Height,
|
||||
UseTiling = configuration.PDFToImageDescriptor.UseTiling,
|
||||
WithFormFill = configuration.PDFToImageDescriptor.WithFormFill,
|
||||
WithAspectRatio = configuration.PDFToImageDescriptor.WithAspectRatio,
|
||||
WithAnnotations = configuration.PDFToImageDescriptor.WithAnnotations,
|
||||
DpiRelativeToBounds = configuration.PDFToImageDescriptor.DpiRelativeToBounds,
|
||||
}
|
||||
).ToList();
|
||||
// Calculating With ...
|
||||
var width = configuration.PDFToImageDescriptor.Width;
|
||||
if (width >= 0 && configuration.PDFToImageDescriptor.MaxImageWidth > 0)
|
||||
{
|
||||
//
|
||||
width = Math.Min(
|
||||
width,
|
||||
configuration.PDFToImageDescriptor.MaxImageWidth
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
var allPagesCount = Conversion.GetPageCount(pdfStream);
|
||||
var pageCount = Math.Min(
|
||||
allPagesCount,
|
||||
MaxPagesForImageFallback
|
||||
);
|
||||
//
|
||||
// Preparing Render Options ...
|
||||
var renderOptions = new RenderOptions
|
||||
{
|
||||
Width = width,
|
||||
Dpi = configuration.PDFToImageDescriptor.Dpi,
|
||||
Height = configuration.PDFToImageDescriptor.Height,
|
||||
UseTiling = configuration.PDFToImageDescriptor.UseTiling,
|
||||
WithFormFill = configuration.PDFToImageDescriptor.WithFormFill,
|
||||
WithAspectRatio = configuration.PDFToImageDescriptor.WithAspectRatio,
|
||||
WithAnnotations = configuration.PDFToImageDescriptor.WithAnnotations,
|
||||
DpiRelativeToBounds = configuration.PDFToImageDescriptor.DpiRelativeToBounds,
|
||||
};
|
||||
|
||||
//
|
||||
// Preparing Images to Fallback ...
|
||||
var pagesCount = Conversion.GetPageCount(pdfStream);
|
||||
pdfStream.Position = 0;
|
||||
|
||||
//
|
||||
if (configuration.PDFToImageDescriptor.MaxPagesForImageFallback > 0)
|
||||
{
|
||||
//
|
||||
pagesCount = Math.Min(
|
||||
pagesCount,
|
||||
configuration.PDFToImageDescriptor.MaxPagesForImageFallback
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
for (int pageIndex = 0; pageIndex < allPagesCount; pageIndex++)
|
||||
for (int pageIndex = 0; pageIndex < pagesCount; pageIndex++)
|
||||
{
|
||||
//
|
||||
// Check Cancellation Token ...
|
||||
@@ -259,7 +273,14 @@ namespace xAiApi.Providers.Extractors
|
||||
try
|
||||
{
|
||||
//
|
||||
var skBitmap = images[pageIndex];
|
||||
var skBitmap = Conversion.ToImage(
|
||||
page: pageIndex,
|
||||
pdfStream: pdfStream,
|
||||
options: renderOptions
|
||||
);
|
||||
pdfStream.Position = 0;
|
||||
|
||||
//
|
||||
using (skBitmap)
|
||||
using (var image = SKImage.FromBitmap(skBitmap))
|
||||
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
|
||||
@@ -271,7 +292,7 @@ namespace xAiApi.Providers.Extractors
|
||||
{
|
||||
Bytes = ms.ToArray(),
|
||||
MimeType = "image/png",
|
||||
Description = $"Page {pageIndex}, rendered from {fileName}"
|
||||
Description = $"Page {pageIndex + 1}, rendered from {fileName}"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user