using System; using System.IO; using System.Linq; using System.Threading; using xAiModels.Models; using System.Threading.Tasks; using xAiApi.Interfaces.Extractors; namespace xAiApi.Providers.Extractors { public class XImageFileContentExtractor : IXImageFileContentExtractor { /// /// Supported MIME Types ... /// private static readonly string[] SupportedMimeTypes = [ "image/png", "image/jpeg", "image/jpg", "image/gif", "image/webp", "image/bmp" ]; /// /// آیا OCR فعال است (برای مدل‌های غیر Vision) ... /// private readonly bool _enableOcrFallback; public XImageFileContentExtractor() : this(enableOcrFallback: true) { } public XImageFileContentExtractor(bool enableOcrFallback = true) { _enableOcrFallback = enableOcrFallback; } /// /// 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, "image", 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 }; // using var memoryStream = new MemoryStream(); await fileStream.CopyToAsync(memoryStream, cancellationToken); var imageBytes = memoryStream.ToArray(); // result.Images.Add(new XExtractedImage { Bytes = imageBytes, MimeType = mimeType, Description = $"Attached image: {fileName}" }); // OCR Fallback برای مدل‌های متنی if (_enableOcrFallback) { // try { // var ocrText = await PerformOcrAsync(imageBytes, cancellationToken); if (!string.IsNullOrWhiteSpace(ocrText)) { result.Text = ocrText; } } catch { // OCR failed, but image is still available for Vision } } // return result; } /// /// Do OCR on Image for Text Extraction ... /// private async Task PerformOcrAsync( byte[] imageBytes, CancellationToken cancellationToken ) { // // TODO: Complete this ... return await Task.FromResult(string.Empty); } } }