mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-03 15:42:02 +00:00
63 lines
3.6 KiB
C#
63 lines
3.6 KiB
C#
using AIStudio.Tools.OpenXml;
|
|
|
|
namespace AIStudio.Tools.PowerPoint;
|
|
|
|
/// <summary>
|
|
/// Creates a presentation file from a template and a slide deck.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 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>
|
|
/// 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
|
|
/// planned, and the writer has to see the layouts of the very file it edits.
|
|
/// </remarks>
|
|
/// <param name="templatePath">The path of the template, a .pptx or a .potx.</param>
|
|
/// <param name="targetPath">The path of the file the user chose. It may exist; the save dialog asked about overwriting it.</param>
|
|
/// <param name="deck">The slide deck, as checked against the template when it was planned.</param>
|
|
/// <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 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)
|
|
{
|
|
// 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(() =>
|
|
{
|
|
using var skeletonFile = skeleton.OpenRead();
|
|
return PresentationTemplate.Read(skeletonFile);
|
|
}, token);
|
|
|
|
if (!SlideDeckSpecValidator.StillFits(deck, currentTemplate, basis))
|
|
return PresentationFileResult.TEMPLATE_CHANGED;
|
|
|
|
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);
|
|
}
|
|
} |