From 0f92c54fb56da23662e13310fcee440e5eca611f Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Thu, 22 Oct 2026 23:31:20 +0330 Subject: [PATCH] adding Code Content Extractor ... --- DI/XDIHelperExtension.cs | 1 + .../Extractors/IXCodeFileContentExtractor.cs | 9 ++ .../Extractors/XCodeFileContentExtractor.cs | 125 ++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 Interfaces/Extractors/IXCodeFileContentExtractor.cs create mode 100644 Providers/Extractors/XCodeFileContentExtractor.cs diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index fcafc9d..1e8f063 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -84,6 +84,7 @@ namespace xAiApi.DI source.AddSingleton(); source.AddSingleton(); source.AddSingleton(); + source.AddSingleton(); source.AddSingleton(); source.AddSingleton(); source.AddSingleton(); diff --git a/Interfaces/Extractors/IXCodeFileContentExtractor.cs b/Interfaces/Extractors/IXCodeFileContentExtractor.cs new file mode 100644 index 0000000..6da4ed8 --- /dev/null +++ b/Interfaces/Extractors/IXCodeFileContentExtractor.cs @@ -0,0 +1,9 @@ +namespace xAiApi.Interfaces.Extractors +{ + /// + /// Extractor for source code files ... + /// (C#, Python, JavaScript, TypeScript, Java, Go, Rust, ...) + /// + public interface IXCodeFileContentExtractor : IXFileContentExtractorBase + { } +} \ No newline at end of file diff --git a/Providers/Extractors/XCodeFileContentExtractor.cs b/Providers/Extractors/XCodeFileContentExtractor.cs new file mode 100644 index 0000000..3b2c1a6 --- /dev/null +++ b/Providers/Extractors/XCodeFileContentExtractor.cs @@ -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 +{ + /// + /// Extractor for source code files ... + /// (C#, Python, JavaScript, TypeScript, Java, Go, Rust, ...) + /// + public class XCodeFileContentExtractor : IXCodeFileContentExtractor + { + /// + /// Supported MIME Types ... + /// + 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", + ]; + + /// + /// Check if this extractor supports the specified MIME type ... + /// + /// + /// + public bool CanExtract(string mimeType) + { + // + return SupportedMimeTypes.Contains( + mimeType?.ToLowerInvariant() ?? string.Empty + ); + } + + /// + /// Extract text content from file stream ... + /// + /// + /// + /// + /// + public async Task ExtractAsync( + Stream fileStream, + string mimeType, + CancellationToken cancellationToken = default + ) + { + // + var result = await ExtractRichAsync( + fileStream, + "book.epub", + mimeType, + cancellationToken + ); + + // + return result.Text; + } + + /// + /// Extract content from stream as Rich Result ... + /// + /// + /// + /// + /// + /// + public async Task 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; + } + } +} \ No newline at end of file