mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-05 05:04:35 +00:00
Improved reading and writing large Office files without copying them into memory (#1243)
This commit is contained in:
1 parent
918572c158
commit
dbe609a9a7
37 files changed
+1161
-471
No files matched your search
@@ -1,10 +1,9 @@
|
||||
using System.IO.Compression;
|
||||
using System.IO.Packaging;
|
||||
|
||||
using AIStudio.Tests.Tools.OpenXml;
|
||||
using AIStudio.Tests.Tools.PowerPoint;
|
||||
using AIStudio.Tools.DocumentSegments;
|
||||
using AIStudio.Tools.DocumentSegments.PowerPoint;
|
||||
using AIStudio.Tools.OpenXml;
|
||||
using AIStudio.Tools.PowerPoint;
|
||||
|
||||
using DocumentFormat.OpenXml;
|
||||
@@ -90,7 +89,7 @@ public sealed class PowerPointSegmentFormatTests
|
||||
[Test]
|
||||
public async Task TheTextsOfSlidesAndNotesBecomeSegmentsWithTags()
|
||||
{
|
||||
var keys = (await this.format.ReadAsync(this.source, CancellationToken.None)).Select(segment => segment.Key).ToList();
|
||||
var keys = (await this.format.ReadAsync(this.source, NullLogger.Instance, CancellationToken.None)).Select(segment => segment.Key).ToList();
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
@@ -248,7 +247,7 @@ public sealed class PowerPointSegmentFormatTests
|
||||
this.AddVideo();
|
||||
|
||||
var results = await this.ResultsAsync(key => key.ToUpperInvariant());
|
||||
var stagedBefore = StagedFiles();
|
||||
var stagedBefore = StagingFolder.Files();
|
||||
var allocatedBefore = GC.GetTotalAllocatedBytes(true);
|
||||
var result = await this.format.WriteAsync(this.source, this.target, results, "de-DE", NullLogger.Instance, CancellationToken.None);
|
||||
var allocated = GC.GetTotalAllocatedBytes(true) - allocatedBefore;
|
||||
@@ -267,7 +266,7 @@ public sealed class PowerPointSegmentFormatTests
|
||||
Assert.That(targetVideo.CompressedLength, Is.EqualTo(targetVideo.Length), "The video stays uncompressed.");
|
||||
Assert.That(allocated, Is.LessThan(VIDEO_SIZE / 2), "The video goes through the stream, not through memory.");
|
||||
Assert.That(SlideOf(document).Descendants<A.Paragraph>().Select(paragraph => paragraph.InnerText), Has.Member("MISSION TO MARS"), "The new words reach the new document.");
|
||||
Assert.That(StagedFiles().Except(stagedBefore), Is.Empty, "The new document leaves nothing behind in the staging folder.");
|
||||
Assert.That(StagingFolder.Files().Except(stagedBefore), Is.Empty, "The new document leaves nothing behind in the staging folder.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,6 +282,21 @@ public sealed class PowerPointSegmentFormatTests
|
||||
Assert.That(this.format.TextSizeOf(this.source), Is.EqualTo(xmlSize).And.LessThan(VIDEO_SIZE), "Only the XML comes into memory, unpacked, never the pictures and videos.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReadingThroughTheSkeletonLeavesNothingBehind()
|
||||
{
|
||||
this.AddVideo();
|
||||
var stagedBefore = StagingFolder.Files();
|
||||
|
||||
var keys = (await this.format.ReadAsync(this.source, NullLogger.Instance, CancellationToken.None)).Select(segment => segment.Key).ToList();
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(keys, Does.Contain("Mission to Mars"), "The skeleton holds every text of the presentation.");
|
||||
Assert.That(StagingFolder.Files().Except(stagedBefore), Is.Empty, "The skeleton is gone once the presentation is read.");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AChangedSourceWritesNothing()
|
||||
{
|
||||
@@ -297,7 +311,7 @@ public sealed class PowerPointSegmentFormatTests
|
||||
|
||||
private async Task<SegmentResults> ResultsAsync(Func<string, string> transform)
|
||||
{
|
||||
var segments = await this.format.ReadAsync(this.source, CancellationToken.None);
|
||||
var segments = await this.format.ReadAsync(this.source, NullLogger.Instance, CancellationToken.None);
|
||||
return new SegmentResults(segments
|
||||
.Where(segment => segment.HasWords)
|
||||
.DistinctBy(segment => segment.Key)
|
||||
@@ -309,19 +323,7 @@ public sealed class PowerPointSegmentFormatTests
|
||||
/// <summary>
|
||||
/// Adds a video as PowerPoint stores it: uncompressed, behind the slides, which the update mode of ZipArchive would load into memory.
|
||||
/// </summary>
|
||||
private void AddVideo()
|
||||
{
|
||||
var video = new byte[VIDEO_SIZE];
|
||||
new Random(1179).NextBytes(video);
|
||||
|
||||
using var package = Package.Open(this.source, FileMode.Open, FileAccess.ReadWrite);
|
||||
var slidePart = package.GetParts().Single(part => part.Uri.OriginalString.StartsWith("/ppt/slides/slide", StringComparison.Ordinal));
|
||||
var videoPart = package.CreatePart(new Uri("/ppt/media/video1.mp4", UriKind.Relative), "video/mp4", CompressionOption.NotCompressed);
|
||||
using (var stream = videoPart.GetStream())
|
||||
stream.Write(video);
|
||||
|
||||
slidePart.CreateRelationship(videoPart.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/video");
|
||||
}
|
||||
private void AddVideo() => TestTemplate.AddVideo(this.source, 1, TestTemplate.VideoOf(VIDEO_SIZE));
|
||||
|
||||
private static byte[] ContentOf(ZipArchiveEntry entry)
|
||||
{
|
||||
@@ -331,12 +333,6 @@ public sealed class PowerPointSegmentFormatTests
|
||||
return content.ToArray();
|
||||
}
|
||||
|
||||
private static HashSet<string> StagedFiles()
|
||||
{
|
||||
var directory = Path.Join(Path.GetTempPath(), StagedDocument.DIRECTORY);
|
||||
return Directory.Exists(directory) ? Directory.EnumerateFiles(directory).ToHashSet() : [];
|
||||
}
|
||||
|
||||
private static IEnumerable<OpenXmlElement> RootsOf(PresentationDocument document)
|
||||
{
|
||||
var slidePart = document.PresentationPart!.SlideParts.Single();
|
||||
|
||||
Reference in new issue
Block a user