adding Code Content Extractor ...
This commit is contained in:
@@ -84,6 +84,7 @@ namespace xAiApi.DI
|
||||
source.AddSingleton<IXFileContentExtractorBase, XEpubFileContentExtractor>();
|
||||
source.AddSingleton<IXFileContentExtractorBase, XDocxFileContentExtractor>();
|
||||
source.AddSingleton<IXFileContentExtractorBase, XHtmlFileContentExtractor>();
|
||||
source.AddSingleton<IXFileContentExtractorBase, XCodeFileContentExtractor>();
|
||||
source.AddSingleton<IXFileContentExtractorBase, XImageFileContentExtractor>();
|
||||
source.AddSingleton<IXFileContentExtractorBase, XAudioFileContentExtractor>();
|
||||
source.AddSingleton<IXFileContentExtractorBase, XExcelFileContentExtractor>();
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace xAiApi.Interfaces.Extractors
|
||||
{
|
||||
/// <summary>
|
||||
/// Extractor for source code files ...
|
||||
/// (C#, Python, JavaScript, TypeScript, Java, Go, Rust, ...)
|
||||
/// </summary>
|
||||
public interface IXCodeFileContentExtractor : IXFileContentExtractorBase
|
||||
{ }
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xCommons.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using xAiApi.Interfaces.Extractors;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using xAiModels.Models;
|
||||
using System.Text;
|
||||
|
||||
namespace xAiApi.Providers.Extractors
|
||||
{
|
||||
/// <summary>
|
||||
/// Extractor for source code files ...
|
||||
/// (C#, Python, JavaScript, TypeScript, Java, Go, Rust, ...)
|
||||
/// </summary>
|
||||
public class XCodeFileContentExtractor : IXCodeFileContentExtractor
|
||||
{
|
||||
/// <summary>
|
||||
/// Supported MIME Types ...
|
||||
/// </summary>
|
||||
private static readonly string[] SupportedMimeTypes =
|
||||
[
|
||||
"text/x-c",
|
||||
"text/x-go",
|
||||
"text/yaml",
|
||||
"text/x-php",
|
||||
"text/x-c++",
|
||||
"text/x-sql",
|
||||
"text/x-java",
|
||||
"text/x-rust",
|
||||
"text/x-ruby",
|
||||
"text/x-csharp",
|
||||
"text/x-python",
|
||||
"text/javascript",
|
||||
"text/typescript",
|
||||
"text/x-java-source",
|
||||
"application/x-yaml",
|
||||
"application/x-csharp",
|
||||
"application/x-python",
|
||||
"application/javascript",
|
||||
"application/typescript",
|
||||
];
|
||||
|
||||
/// <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
|
||||
};
|
||||
|
||||
//
|
||||
// Read entire source code as UTF-8 (with BOM detection) ...
|
||||
using var reader = new StreamReader(
|
||||
fileStream,
|
||||
Encoding.UTF8,
|
||||
leaveOpen: false,
|
||||
detectEncodingFromByteOrderMarks: true
|
||||
);
|
||||
|
||||
//
|
||||
// Normalize line endings for consistent downstream processing ...
|
||||
result.Text = result.Text?
|
||||
.Replace("\r\n", "\n")
|
||||
.Replace("\r", "\n");
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user