using AIStudio.Tools.OpenXml; namespace AIStudio.Tools.PowerPoint; /// /// Creates a presentation file from a template and a slide deck. /// /// /// 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. /// internal static class PresentationFile { /// /// Writes the slide deck into the skeleton of the template, and the presentation into the target. /// /// /// 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. /// /// The path of the template, a .pptx or a .potx. /// The path of the file the user chose. It may exist; the save dialog asked about overwriting it. /// The slide deck, as checked against the template when it was planned. /// What the deck was planned on, as PresentationTemplate.DescribeBasisOf told it then. /// What the user chose for the presentation as a whole. /// Where the pictures the deck refers to come from. /// Where to report a staged file which cannot be removed. /// The cancellation token. /// Whether the presentation was written, and which pictures it lacks. public static async Task CreateAsync(string templatePath, string targetPath, SlideDeckSpec deck, IReadOnlyDictionary 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); } }