using System; using System.IO; using System.Linq; using System.Text; using System.Threading; using xAiModels.Models; using xCommons.Extensions; using System.Threading.Tasks; using OfficeIMO.OpenDocument; using System.Collections.Generic; using xAiApi.Interfaces.Extractors; namespace xAiApi.Providers.Extractors { /// /// Extractor for OpenDocument Format (ODF) documents /// (ODT: OpenDocument Text, ODS: OpenDocument Spreadsheet, ODP: OpenDocument Presentation) /// public class XOpenDocumentFileContentExtractor : IXOpenDocumentFileContentExtractor { /// /// Supported MIME Types ... /// private static readonly string[] SupportedMimeTypes = [ "application/vnd.oasis.opendocument.text", // .odt "application/vnd.oasis.opendocument.spreadsheet", // .ods "application/vnd.oasis.opendocument.presentation" // .odp ]; /// /// 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, "document.rtf", 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 }; // try { // if (IsOdt(mimeType)) { result.Text = await ExtractOdtAsync(fileStream, cancellationToken); } else if (IsOds(mimeType)) { result.Text = await ExtractOdsAsync(fileStream, cancellationToken); } else if (IsOdp(mimeType)) { result.Text = await ExtractOdpAsync(fileStream, cancellationToken); } else { result.ErrorMessage = $"Unsupported OpenDocument MIME type: {mimeType}"; } } catch (Exception ex) { // result.ErrorMessage = $"RTF extraction failed: {ex.Message}"; } // return result; } /// /// Check Mime Type ... /// /// /// private bool IsOdt(string mimeType) { // var result = mimeType? .ToLowerInvariant() == "application/vnd.oasis.opendocument.text"; // return result; } /// /// Check Mime Type ... /// /// /// private bool IsOds(string mimeType) { // var result = mimeType? .ToLowerInvariant() == "application/vnd.oasis.opendocument.spreadsheet"; // return result; } /// /// Check Mime Type ... /// /// /// private bool IsOdp(string mimeType) { // var result = mimeType? .ToLowerInvariant() == "application/vnd.oasis.opendocument.presentation"; // return result; } /// /// Extract ODT File Content ... /// /// /// /// private async Task ExtractOdtAsync( Stream fileStream, CancellationToken cancellationToken ) { // await using var ms = new MemoryStream(); await fileStream.CopyToAsync(ms, cancellationToken); ms.Position = 0; // var document = OdtDocument.Load(ms); // var sb = new StringBuilder(); foreach (var paragraph in document.Paragraphs) { // if (cancellationToken.IsCancellationRequested) { break; } // if (!paragraph.Text.IsNullOrEmpty()) { sb.AppendLine(paragraph.Text.Trim()); } } // // 4. استخراج جداول var tableIndex = 0; foreach (var table in document.Tables) { // if (cancellationToken.IsCancellationRequested) { break; } // tableIndex++; sb.AppendLine(); sb.AppendLine($"[Table {tableIndex}]"); // foreach (var row in table.Rows) { // var cellTexts = new List(); foreach (var cell in row.Cells) { // var cellText = string.Join( " ", cell.Paragraphs .Select(p => p.Text?.Trim()) .Where(t => !string.IsNullOrWhiteSpace(t)) ); cellTexts.Add(cellText); } // sb.AppendLine($" | {string.Join(" | ", cellTexts)} |"); } // sb.AppendLine(); } // var result = sb.ToString().TrimEnd(); // return result; } /// /// Extract ODS File Content ... /// /// /// /// private async Task ExtractOdsAsync( Stream fileStream, CancellationToken cancellationToken ) { // await using var ms = new MemoryStream(); await fileStream.CopyToAsync(ms, cancellationToken); ms.Position = 0; // var document = OdsDocument.Load(ms); var sb = new StringBuilder(); var sheetIndex = 0; foreach (var sheet in document.Sheets) { // if (cancellationToken.IsCancellationRequested) { break; } // sheetIndex++; sb.AppendLine($"--- Sheet: {sheet.Name} ---"); // for (long row = 0; row < sheet.RowCount; row++) { // if (cancellationToken.IsCancellationRequested) { break; } // var cellValues = new List(); var columnCount = sheet.ColumnRuns.Sum(r => r.RepeatCount); for (long col = 0; col < columnCount; col++) { // var value = sheet.GetValue(row, col); if (!value.IsNull()) { var text = value.LexicalValue?.Trim() ?? string.Empty; cellValues.Add(text); } } // if (cellValues.Any(v => !string.IsNullOrWhiteSpace(v))) { sb.AppendLine(string.Join(" | ", cellValues)); } } // sb.AppendLine(); } // var result = sb.ToString().TrimEnd(); // return result; } /// /// Extract ODS File Content ... /// /// /// /// private async Task ExtractOdpAsync( Stream fileStream, CancellationToken cancellationToken ) { // await using var ms = new MemoryStream(); await fileStream.CopyToAsync(ms, cancellationToken); ms.Position = 0; // var presentation = OdpPresentation.Load(ms); var sb = new StringBuilder(); var slideIndex = 0; foreach (var slide in presentation.Slides) { // if (cancellationToken.IsCancellationRequested) { break; } // slideIndex++; sb.AppendLine($"[Slide {slideIndex}]"); var textParts = new List(); // foreach (var shape in slide.Shapes) { // if (shape is OdpTextBox textBox) { // foreach (var paragraph in textBox.Paragraphs) { // if (!paragraph.Text.IsNullOrEmpty()) { textParts.Add(paragraph.Text.Trim()); } } } else if (shape is OdpShape odpShape) { // foreach (var paragraph in odpShape.TextParagraphs) { // if (!paragraph.Text.IsNullOrEmpty()) { textParts.Add(paragraph.Text.Trim()); } } } } // if (textParts.Count > 0) { // sb.AppendLine("Content:"); foreach (var text in textParts) { sb.AppendLine($" - {text}"); } } // sb.AppendLine(); } // var result = sb.ToString().TrimEnd(); // return result; } } }