Files
SqlDatabaseVectorSearch/SqlDatabaseVectorSearch/ContentDecoders/DocxContentDecoder.cs
T
Marco Minerva a0f1755c85 Add CancellationToken support to async methods #9
Introduce support for `CancellationToken` across various methods to allow for task cancellation and improve responsiveness.
- Update `DecodeAsync` method in `DocxContentDecoder.cs`, `PdfContentDecoder.cs`, `TextContentDecoder.cs`, and `IContentDecoder.cs` to include an optional `CancellationToken` parameter.
- Modify endpoint handlers in `Program.cs` to accept and pass `CancellationToken` parameters.
- Update methods in `ChatService.cs` to include `CancellationToken` parameters.
- Update methods in `DocumentService.cs` to include `CancellationToken` parameters.
- Update methods in `VectorSearchService.cs` to include `CancellationToken` parameters.
These changes ensure that long-running operations can be canceled if needed, improving the application's ability to handle cancellation requests gracefully.
2025-02-10 16:20:35 +01:00

26 lines
796 B
C#

using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace SqlDatabaseVectorSearch.ContentDecoders;
public class DocxContentDecoder : IContentDecoder
{
public Task<string> DecodeAsync(Stream stream, string contentType, CancellationToken cancellationToken = default)
{
// Open a Word document for read-only access.
using var document = WordprocessingDocument.Open(stream, false);
var body = document.MainDocumentPart?.Document.Body;
var content = new StringBuilder();
var paragraphs = body?.Descendants<Paragraph>() ?? [];
foreach (var p in paragraphs)
{
content.AppendLine(p.InnerText);
}
return Task.FromResult(content.ToString());
}
}