Improved reading and writing large Office files without copying them into memory (#1243)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-28 23:03:30 +01:00
1 parent 918572c158
commit dbe609a9a7
37 files changed
+1161 -471

No files matched your search

@@ -4,6 +4,8 @@ using AIStudio.Tools.DocumentSegments;
using AIStudio.Tools.DocumentSegments.Html;
using AIStudio.Tools.DocumentSegments.Latex;
using Microsoft.Extensions.Logging.Abstractions;
namespace AIStudio.Tests.Tools.DocumentSegments;
/// <summary>
@@ -69,7 +71,7 @@ public sealed class TextDocumentFileTests
{
var source = await this.WriteAsync("page.html", Encoding.ASCII.GetBytes("<html><head><meta charset=\"iso-8859-1\"></head><body><p>M&uuml;ller</p></body></html>"));
var exception = await Assert.ThrowsAsync<UnsupportedTextEncodingException>(() => new HtmlSegmentFormat().ReadAsync(source, CancellationToken.None));
var exception = await Assert.ThrowsAsync<UnsupportedTextEncodingException>(() => new HtmlSegmentFormat().ReadAsync(source, NullLogger.Instance, CancellationToken.None));
Assert.That(exception!.DeclaredEncoding, Is.EqualTo("iso-8859-1"), "An ASCII page which says Latin-1 would show new words in UTF-8 garbled.");
}
@@ -80,7 +82,7 @@ public sealed class TextDocumentFileTests
var page = "<html><head><meta charset=\"iso-8859-1\"></head><body><p>Grüße</p></body></html>";
var source = await this.WriteAsync("page.html", new UTF8Encoding(true).GetPreamble().Concat(Encoding.UTF8.GetBytes(page)).ToArray());
var segments = await new HtmlSegmentFormat().ReadAsync(source, CancellationToken.None);
var segments = await new HtmlSegmentFormat().ReadAsync(source, NullLogger.Instance, CancellationToken.None);
Assert.That(segments.Select(segment => segment.Key), Does.Contain("Grüße"), "Browsers follow the byte order mark, too.");
}
@@ -90,7 +92,7 @@ public sealed class TextDocumentFileTests
{
var source = await this.WriteAsync("paper.tex", Encoding.ASCII.GetBytes("\\documentclass{article}\n\\usepackage[latin1]{inputenc}\n\\begin{document}\nHello.\n\\end{document}"));
var exception = await Assert.ThrowsAsync<UnsupportedTextEncodingException>(() => new LatexSegmentFormat().ReadAsync(source, CancellationToken.None));
var exception = await Assert.ThrowsAsync<UnsupportedTextEncodingException>(() => new LatexSegmentFormat().ReadAsync(source, NullLogger.Instance, CancellationToken.None));
Assert.That(exception!.DeclaredEncoding, Is.EqualTo("latin1"));
}
@@ -146,7 +148,7 @@ public sealed class TextDocumentFileTests
await using (var stream = new FileStream(source, FileMode.CreateNew))
stream.SetLength(new HtmlSegmentFormat().MaxTextSize + 1);
await Assert.ThrowsAsync<DocumentTooLargeException>(() => new HtmlSegmentFormat().ReadAsync(source, CancellationToken.None));
await Assert.ThrowsAsync<DocumentTooLargeException>(() => new HtmlSegmentFormat().ReadAsync(source, NullLogger.Instance, CancellationToken.None));
}
private async Task<string> WriteAsync(string name, byte[] bytes)