154 lines
4.5 KiB
C#
154 lines
4.5 KiB
C#
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using xAiModels.Models;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using xAiApi.Interfaces.Extractors;
|
|
|
|
namespace xAiApi.Providers.Extractors
|
|
{
|
|
/// <summary>
|
|
/// Composite extractor that delegates to appropriate extractor
|
|
/// based on MIME type ...
|
|
/// </summary>
|
|
public class XFileContentExtractor : IXFileContentExtractor
|
|
{
|
|
//
|
|
private readonly IList<IXFileContentExtractorBase> _extractors = [];
|
|
|
|
public XFileContentExtractor(
|
|
IEnumerable<IXFileContentExtractorBase> availableExtractors
|
|
)
|
|
{
|
|
//
|
|
_extractors = [.. availableExtractors
|
|
.Where(e => e.GetType() != typeof(XFileContentExtractor))];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if this extractor supports the specified MIME type ...
|
|
/// </summary>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="extractors"></param>
|
|
/// <returns></returns>
|
|
public bool CanExtract(
|
|
string mimeType,
|
|
string[] extractors = null
|
|
)
|
|
{
|
|
//
|
|
return GetExtractors(extractors)
|
|
.Any(e => e.CanExtract(mimeType));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract text content from file stream ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="extractors"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> ExtractAsync(
|
|
Stream fileStream,
|
|
string mimeType,
|
|
string[] extractors = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var extractor = GetExtractors(extractors)
|
|
.FirstOrDefault(e => e.CanExtract(mimeType));
|
|
if (extractor.IsNull())
|
|
{
|
|
//
|
|
XException.NotAllowed.Throw(
|
|
$"Unsupported file type: {mimeType}"
|
|
);
|
|
}
|
|
|
|
//
|
|
var result = await extractor
|
|
.ExtractAsync(
|
|
mimeType: mimeType,
|
|
fileStream: fileStream,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract content from stream as Rich Result ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="extractors"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileExtractionResult> ExtractRichAsync(
|
|
Stream fileStream,
|
|
string fileName,
|
|
string mimeType,
|
|
string[] extractors = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var extractor = GetExtractors(extractors)
|
|
.FirstOrDefault(e => e.CanExtract(mimeType));
|
|
if (extractor.IsNull())
|
|
{
|
|
//
|
|
XException.NotAllowed.Throw(
|
|
$"Unsupported file type: {mimeType}"
|
|
);
|
|
}
|
|
|
|
//
|
|
var result = await extractor
|
|
.ExtractRichAsync(
|
|
mimeType: mimeType,
|
|
fileName: fileName,
|
|
fileStream: fileStream,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Provided Extractors ...
|
|
/// </summary>
|
|
/// <param name="extractors"></param>
|
|
/// <returns></returns>
|
|
private IList<IXFileContentExtractorBase> GetExtractors(string[] extractors = null)
|
|
{
|
|
//
|
|
var result = new List<IXFileContentExtractorBase>();
|
|
|
|
//
|
|
if (extractors.IsNull() || !extractors.HasChild())
|
|
{
|
|
//
|
|
result = [.. _extractors];
|
|
}
|
|
else
|
|
{
|
|
//
|
|
result = [.. _extractors
|
|
.Where(e => extractors
|
|
.Any(ex => e.GetType().FullName.Contains(ex)))];
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |