Files
AI-Studio/app/MindWork AI Studio/Tools/OpenXml/TemplateCopy.cs
T

88 lines
3.6 KiB
C#

namespace AIStudio.Tools.OpenXml;
/// <summary>
/// A copy of a template in the temporary folder, in which an Office document comes about before it
/// goes into the file the user chose.
/// </summary>
/// <remarks>
/// <para>
/// Only the complete document is streamed into the file the user chose, see TargetFile, so a failure
/// before the document is complete leaves the target untouched. Disposing the copy removes it.
/// </para>
/// <para>
/// The writer edits the copy with the Open XML SDK, which saves it in the update mode of ZipArchive.
/// That mode loads every entry behind the first changed one into memory, e.g., the pictures and videos
/// behind the first slide. The Slide Assistant needs it all the same, since it adds and removes parts.
/// A writer which only changes the XML of existing parts uses PackageRewrite instead, as the document
/// segments do.
/// </para>
/// </remarks>
internal sealed class TemplateCopy : IDisposable
{
private readonly ILogger logger;
private TemplateCopy(string filePath, ILogger logger)
{
this.FilePath = filePath;
this.logger = logger;
}
/// <summary>
/// The path of the copy.
/// </summary>
public string FilePath { get; }
/// <summary>
/// Copies the template into a folder below the temporary folder of the system.
/// </summary>
/// <param name="templatePath">The path of the template.</param>
/// <param name="temporaryDirectory">The name of the folder below the temporary folder, one per
/// kind of document.</param>
/// <param name="fileExtension">The extension of the document which comes about, including the
/// leading dot.</param>
/// <param name="logger">Where to report a temporary file which cannot be removed.</param>
/// <param name="token">The cancellation token.</param>
/// <returns>The copy.</returns>
public static async Task<TemplateCopy> CreateAsync(string templatePath, string temporaryDirectory, string fileExtension, ILogger logger, CancellationToken token)
{
var directory = Path.Join(Path.GetTempPath(), temporaryDirectory);
Directory.CreateDirectory(directory);
var copy = new TemplateCopy(Path.Join(directory, $"{Guid.NewGuid():N}{fileExtension}"), logger);
try
{
await using var templateFile = TemplateFile.OpenRead(templatePath);
await using var copyFile = new FileStream(copy.FilePath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
await templateFile.CopyToAsync(copyFile, token);
}
catch
{
copy.Dispose();
throw;
}
return copy;
}
/// <summary>
/// Opens the copy for the writer to edit it.
/// </summary>
/// <returns>The copy, opened for reading and writing.</returns>
public FileStream OpenForEditing() => new(this.FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
/// <summary>
/// Streams the complete document into the file the user chose, see TargetFile.WriteOverAsync.
/// </summary>
/// <param name="targetPath">The path of the file the user chose. It may exist; the save dialog
/// asked about overwriting it.</param>
/// <param name="token">The cancellation token.</param>
public Task WriteIntoAsync(string targetPath, CancellationToken token) => TargetFile.WriteOverAsync(this.FilePath, targetPath, this.logger, token);
/// <summary>
/// Removes the copy.
/// </summary>
/// <remarks>
/// A copy of the template left behind would pile up in the temporary folder with every attempt.
/// </remarks>
public void Dispose() => TargetFile.DeleteQuietly(this.FilePath, this.logger);
}