Moved temporary files into a folder of their own below the data directory (#1245)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-29 18:45:34 +01:00
1 parent ddfc632b63
commit 12d3e1ec6c
52 files changed
+1219 -396

No files matched your search

@@ -1,31 +1,22 @@
using AIStudio.Settings;
namespace AIStudio.Tools.OpenXml;
/// <summary>
/// A file below the data directory, in which a new Office document or a piece of it comes about, such as the skeleton
/// of a document or a picture for it, before the document goes into the file the user chose, see TargetFile.
/// A file in the temporary folder of AI Studio, in which a new Office document or a piece of it comes about, such as
/// the skeleton of a document or a picture for it, before the document goes into the file the user chose, see
/// TargetFile.
/// </summary>
/// <remarks>
/// <para>
/// The folder lies below the data directory rather than in the temporary folder of the system: on many Linux
/// systems, the temporary folder lives in memory (tmpfs), and a document with videos takes hundreds of megabytes.
/// A backup leaves the folder out, see DataBackupContents.
/// The temporary folder of AI Studio lies below the data directory rather than in the temporary folder of the system:
/// on many Linux systems, the temporary folder lives in memory (tmpfs), and a document with videos takes hundreds of
/// megabytes, see TemporaryFiles.
/// </para>
/// <para>
/// Disposing the file removes it. A file left behind, e.g., after a crash, is removed by the next document which
/// comes about here, once it is a day old. Tests, which have no data directory, use the temporary folder.
/// Disposing the file removes it. A file left behind, e.g., after a crash, is gone with the next start of AI Studio.
/// </para>
/// </remarks>
internal sealed class StagedDocument : IDisposable
{
/// <summary>
/// The folder below the data directory, see DataBackupContents.
/// </summary>
public const string DIRECTORY = ".document-staging/";
private static readonly TimeSpan AGE_OF_LEFTOVERS = TimeSpan.FromDays(1);
private readonly ILogger logger;
private StagedDocument(string filePath, ILogger logger)
@@ -40,19 +31,12 @@ internal sealed class StagedDocument : IDisposable
public string FilePath { get; }
/// <summary>
/// Reserves a file for a new document and removes what earlier documents left behind.
/// Reserves a file for a new document.
/// </summary>
/// <param name="fileExtension">The extension of the document, including the leading dot.</param>
/// <param name="logger">Where to report a file which cannot be removed.</param>
/// <returns>The file, which does not exist yet.</returns>
public static StagedDocument Create(string fileExtension, ILogger logger)
{
var directory = Path.Join(SettingsManager.DataDirectory ?? Path.GetTempPath(), DIRECTORY);
Directory.CreateDirectory(directory);
RemoveLeftovers(directory, logger);
return new StagedDocument(Path.Join(directory, $"{Guid.NewGuid():N}{fileExtension}"), logger);
}
public static StagedDocument Create(string fileExtension, ILogger logger) => new(TemporaryFiles.GetFilePath(fileExtension), logger);
/// <summary>
/// Streams the complete document into the file the user chose, see TargetFile.WriteOverAsync.
@@ -65,17 +49,4 @@ internal sealed class StagedDocument : IDisposable
/// Removes the file.
/// </summary>
public void Dispose() => TargetFile.DeleteQuietly(this.FilePath, this.logger);
/// <summary>
/// Removes the files which are older than a day, e.g., after a crash.
/// </summary>
private static void RemoveLeftovers(string directory, ILogger logger)
{
var oldest = DateTime.UtcNow - AGE_OF_LEFTOVERS;
foreach (var file in new DirectoryInfo(directory).EnumerateFiles())
{
if (file.LastWriteTimeUtc < oldest)
TargetFile.DeleteQuietly(file.FullName, logger);
}
}
}