Compare commits

...
2 Commits
Author SHA1 Message Date
saherelm 0e3d6249a2 refactor AddXCommons for Support File Type Detector ... 2026-10-23 00:13:20 +03:30
saherelm 0f92c54fb5 adding Code Content Extractor ... 2026-10-22 23:31:20 +03:30
4 changed files with 136 additions and 3 deletions
+1
View File
@@ -84,6 +84,7 @@ namespace xAiApi.DI
source.AddSingleton<IXFileContentExtractorBase, XEpubFileContentExtractor>(); source.AddSingleton<IXFileContentExtractorBase, XEpubFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XDocxFileContentExtractor>(); source.AddSingleton<IXFileContentExtractorBase, XDocxFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XHtmlFileContentExtractor>(); source.AddSingleton<IXFileContentExtractorBase, XHtmlFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XCodeFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XImageFileContentExtractor>(); source.AddSingleton<IXFileContentExtractorBase, XImageFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XAudioFileContentExtractor>(); source.AddSingleton<IXFileContentExtractorBase, XAudioFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XExcelFileContentExtractor>(); 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;
}
}
}
+1 -3
View File
@@ -23,11 +23,9 @@ using xCommons.Extensions;
using xPushService.Helpers; using xPushService.Helpers;
using xCommons.Configurations; using xCommons.Configurations;
using System.Collections.Generic; using System.Collections.Generic;
using xAiApi.Providers.Extractors;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using xAiApi.Interfaces.Extractors;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@@ -65,7 +63,7 @@ namespace xAiApi
{ {
// //
// Register Validation Provider and all XCommons Module Services ... // Register Validation Provider and all XCommons Module Services ...
services.AddXCommons(); services.AddXCommons(Configuration);
// //
// Register App Configuration ... // Register App Configuration ...