مراحل زیر بهترتیب برای افزودن کامل Extractor OpenDocument به پروژه xAiApi انجام میشود:
گام ۱ — افزودن بسته NuGet
در فایل xAiApi.csproj، بسته OfficeIMO.OpenDocument را اضافه کنید. دستور نصب از طریق .NET CLI:
dotnet add package OfficeIMO.OpenDocument
یا مستقیماً در فایل پروژه:
<PackageReference Include="OfficeIMO.OpenDocument" Version="..." />
💡 نکته: بسته OfficeIMO.OpenDocument بهطور خودکار مدلهای بومی ODT، ODS و ODP را فراهم میکند. اگر نیاز به تبدیل به PDF یا فرمتهای دیگر داشتید، بستههای اضافی مانند OfficeIMO.OpenDocument.Odt.Pdf نیز موجود است[reference:11].
گام ۲ — ایجاد اینترفیس اختصاصی
فایل جدید Interfaces/Extractors/IXOpenDocumentFileContentExtractor.cs را با محتوای نشاندادهشده در بخش ۴ (بالا) ایجاد کنید.
گام ۳ — پیادهسازی کلاس اصلی
فایل Providers/Extractors/XOpenDocumentFileContentExtractor.cs را با ساختار زیر ایجاد کنید:
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 xAiApi.Interfaces.Extractors;
using OfficeIMO.OpenDocument;
namespace xAiApi.Providers.Extractors
{
/// <summary>
/// Extracts content from OpenDocument Format (ODF) files
/// (ODT: Text, ODS: Spreadsheet, ODP: Presentation)
/// using OfficeIMO.OpenDocument library.
/// </summary>
public class XOpenDocumentFileContentExtractor : IXOpenDocumentFileContentExtractor
{
private static readonly string[] SupportedMimeTypes =
[
"application/vnd.oasis.opendocument.text", // .odt
"application/vnd.oasis.opendocument.spreadsheet", // .ods
"application/vnd.oasis.opendocument.presentation" // .odp
];
public bool CanExtract(string mimeType)
{
return SupportedMimeTypes.Contains(
mimeType?.ToLowerInvariant() ?? string.Empty
);
}
public async Task<string> ExtractAsync(
Stream fileStream,
string mimeType,
CancellationToken cancellationToken = default
)
{
var result = await ExtractRichAsync(
fileStream,
"document.odf",
mimeType,
cancellationToken
);
return result.Text;
}
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 =
$"OpenDocument extraction failed: {ex.Message}";
}
return result;
}
private static bool IsOdt(string mimeType) =>
mimeType?.ToLowerInvariant() == "application/vnd.oasis.opendocument.text";
private static bool IsOds(string mimeType) =>
mimeType?.ToLowerInvariant() == "application/vnd.oasis.opendocument.spreadsheet";
private static bool IsOdp(string mimeType) =>
mimeType?.ToLowerInvariant() == "application/vnd.oasis.opendocument.presentation";
private async Task<string> ExtractOdtAsync(
Stream fileStream,
CancellationToken cancellationToken
)
{
await using var ms = new MemoryStream();
await fileStream.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
using var document = OdtDocument.Load(ms);
var sb = new StringBuilder();
// Extraction logic will be refined based on OfficeIMO.OpenDocument API
return sb.ToString().TrimEnd();
}
private async Task<string> ExtractOdsAsync(
Stream fileStream,
CancellationToken cancellationToken
)
{
await using var ms = new MemoryStream();
await fileStream.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
using var document = OdsDocument.Load(ms);
var sb = new StringBuilder();
// Extraction logic will be refined based on OfficeIMO.OpenDocument API
return sb.ToString().TrimEnd();
}
private async Task<string> ExtractOdpAsync(
Stream fileStream,
CancellationToken cancellationToken
)
{
await using var ms = new MemoryStream();
await fileStream.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
using var document = OdpDocument.Load(ms);
var sb = new StringBuilder();
// Extraction logic will be refined based on OfficeIMO.OpenDocument API
return sb.ToString().TrimEnd();
}
}
}
⚠ نکته مهم درباره API دقیق: ساختار ارائهشده بر اساس الگوی مستندات OfficeIMO (OdtDocument.Load، OdsDocument.Load، OdpDocument.Load) طراحی شده است[reference:12]. برای دسترسی به متن، جداول و محتوای هر سند، باید به مستندات رسمی API مراجعه و متدهای مناسب (مانند پیمایش پاراگرافها یا سلولهای جدول) را فراخوانی کنید. منطق کلی (بارگذاری با Load و استخراج محتوا) صحیح است.
گام ۴ — ثبت در Dependency Injection
در فایل DI/XDIHelperExtension.cs، در متد AddXAiFileContentExtractors، خط زیر را اضافه کنید:
source.AddSingleton<IXFileContentExtractorBase, XOpenDocumentFileContentExtractor>();
پس از افزودن، بخش ثبت Extractor ها بهصورت زیر خواهد بود:
// Register File Content Extractors ...
source.AddSingleton<IXFileContentExtractorBase, XPdfFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XDocxFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XImageFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XAudioFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XExcelFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XVisionFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XPlainTextFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XPowerPointFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XHtmlFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XRtfFileContentExtractor>();
source.AddSingleton<IXFileContentExtractorBase, XOpenDocumentFileContentExtractor>(); // ← جدید
source.AddSingleton<IXFileContentExtractor, XFileContentExtractor>();
✓ بدون نیاز به تغییرات دیگر: کلاس XFileContentExtractor (Composite Extractor) بهطور خودکار تمام پیادهسازیهای IXFileContentExtractorBase را دریافت و مدیریت میکند. بنابراین تنها ثبت در DI کافی است و نیازی به تغییر در منطق Composite Extractor نیست.