407 lines
12 KiB
C#
407 lines
12 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// Extractor for OpenDocument Format (ODF) documents
|
|
/// (ODT: OpenDocument Text, ODS: OpenDocument Spreadsheet, ODP: OpenDocument Presentation)
|
|
/// </summary>
|
|
public class XOpenDocumentFileContentExtractor : IXOpenDocumentFileContentExtractor
|
|
{
|
|
/// <summary>
|
|
/// Supported MIME Types ...
|
|
/// </summary>
|
|
private static readonly string[] SupportedMimeTypes =
|
|
[
|
|
"application/vnd.oasis.opendocument.text", // .odt
|
|
"application/vnd.oasis.opendocument.spreadsheet", // .ods
|
|
"application/vnd.oasis.opendocument.presentation" // .odp
|
|
];
|
|
|
|
/// <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>
|
|
public async Task<string> ExtractAsync(
|
|
Stream fileStream,
|
|
string mimeType,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await ExtractRichAsync(
|
|
fileStream,
|
|
"document.rtf",
|
|
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
|
|
};
|
|
|
|
//
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Mime Type ...
|
|
/// </summary>
|
|
/// <param name="mimeType"></param>
|
|
/// <returns></returns>
|
|
private bool IsOdt(string mimeType)
|
|
{
|
|
//
|
|
var result = mimeType?
|
|
.ToLowerInvariant() == "application/vnd.oasis.opendocument.text";
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Mime Type ...
|
|
/// </summary>
|
|
/// <param name="mimeType"></param>
|
|
/// <returns></returns>
|
|
private bool IsOds(string mimeType)
|
|
{
|
|
//
|
|
var result = mimeType?
|
|
.ToLowerInvariant() == "application/vnd.oasis.opendocument.spreadsheet";
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Mime Type ...
|
|
/// </summary>
|
|
/// <param name="mimeType"></param>
|
|
/// <returns></returns>
|
|
private bool IsOdp(string mimeType)
|
|
{
|
|
//
|
|
var result = mimeType?
|
|
.ToLowerInvariant() == "application/vnd.oasis.opendocument.presentation";
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract ODT File Content ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<string> 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<string>();
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract ODS File Content ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<string> 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<string>();
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract ODS File Content ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<string> 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<string>();
|
|
|
|
//
|
|
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;
|
|
}
|
|
}
|
|
} |