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

@@ -6,18 +6,16 @@ namespace AIStudio.Tools.PowerPoint;
/// Creates a presentation file from a template and a slide deck.
/// </summary>
/// <remarks>
/// The presentation comes about in a TemplateCopy, and only the complete presentation is streamed
/// into the file the user chose; TemplateCopy says why.
/// The writer edits a PackageSkeleton of the template, which holds only its XML. The skeleton then writes the
/// presentation into a StagedDocument, with the pictures and videos of the template streamed through and the new
/// pictures taken from their files, see StagedPictures. Only the complete presentation is streamed into the file the
/// user chose, see TargetFile. Neither the template nor the pictures come into memory as a whole on the way, nor into
/// the temporary folder, which lives in memory on many Linux systems.
/// </remarks>
internal static class PresentationFile
{
/// <summary>
/// The folder below the temporary folder of the system, where the presentations come about.
/// </summary>
private const string TEMPORARY_DIRECTORY = "mindwork-ai-studio-presentations";
/// <summary>
/// Writes the slide deck into a copy of the template, and the result into the target.
/// Writes the slide deck into the skeleton of the template, and the presentation into the target.
/// </summary>
/// <remarks>
/// The template is read once more rather than reused: it may have changed since the slides were
@@ -29,25 +27,37 @@ internal static class PresentationFile
/// <param name="basis">What the deck was planned on, as PresentationTemplate.DescribeBasisOf told it then.</param>
/// <param name="options">What the user chose for the presentation as a whole.</param>
/// <param name="pictures">Where the pictures the deck refers to come from.</param>
/// <param name="logger">Where to report a temporary file which cannot be removed.</param>
/// <param name="logger">Where to report a staged file which cannot be removed.</param>
/// <param name="token">The cancellation token.</param>
/// <returns>Whether the presentation was written, and which pictures it lacks.</returns>
public static async Task<PresentationFileResult> CreateAsync(string templatePath, string targetPath, SlideDeckSpec deck, IReadOnlyDictionary<string, string> basis, PresentationWriteOptions options, IPictureSource pictures, ILogger logger, CancellationToken token)
{
using var copy = await TemplateCopy.CreateAsync(templatePath, TEMPORARY_DIRECTORY, ".pptx", logger, token);
PresentationWriteResult written;
await using (var presentationFile = copy.OpenForEditing())
// The Open XML SDK works synchronously, so it runs off the thread of the UI, and so does every step through a whole file:
using var skeleton = await Task.Run(() => PackageSkeleton.Create(templatePath, ".pptx", logger, token), token);
var currentTemplate = await Task.Run(() =>
{
var currentTemplate = PresentationTemplate.Read(presentationFile);
presentationFile.Position = 0;
if (!SlideDeckSpecValidator.StillFits(deck, currentTemplate, basis))
return PresentationFileResult.TEMPLATE_CHANGED;
using var skeletonFile = skeleton.OpenRead();
return PresentationTemplate.Read(skeletonFile);
}, token);
written = await PresentationWriter.WriteAsync(presentationFile, currentTemplate, deck, options, pictures, token);
}
if (!SlideDeckSpecValidator.StillFits(deck, currentTemplate, basis))
return PresentationFileResult.TEMPLATE_CHANGED;
await copy.WriteIntoAsync(targetPath, token);
return new PresentationFileResult(true, written.SkippedPictures);
using var stagedPictures = await StagedPictures.LoadAsync(deck, pictures, logger, token);
using var presentation = StagedDocument.Create(".pptx", logger);
var isWritten = await Task.Run(() =>
{
using (var skeletonFile = skeleton.OpenForEditing())
PresentationWriter.Write(skeletonFile, currentTemplate, deck, options, stagedPictures);
return skeleton.TryWriteDocument([], stagedPictures.PartFiles, presentation.FilePath, token);
}, token);
// The template changed while the presentation was written, e.g., because PowerPoint saved it:
if (!isWritten)
return PresentationFileResult.TEMPLATE_CHANGED;
await presentation.WriteIntoAsync(targetPath, token);
return new PresentationFileResult(true, stagedPictures.Skipped);
}
}