mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-05 04:04:35 +00:00
Moved temporary files into a folder of their own below the data directory (#1245)
This commit is contained in:
1 parent
ddfc632b63
commit
12d3e1ec6c
52 files changed
+1219
-396
No files matched your search
@@ -1,5 +1,3 @@
|
||||
using AIStudio.Settings;
|
||||
|
||||
namespace AIStudio.Tools.Services.Indexing;
|
||||
|
||||
/// <summary>
|
||||
@@ -10,27 +8,18 @@ namespace AIStudio.Tools.Services.Indexing;
|
||||
/// for as long as its text is read, and is deleted right after. Their names are random: the
|
||||
/// runtime logs the path it reads, and the name of an attachment may name a person.
|
||||
///
|
||||
/// Whatever a crash left behind is deleted when AI Studio starts again, before the first mailbox
|
||||
/// is synced. That is why the files live in a directory of their own within the data directory of
|
||||
/// AI Studio, rather than in the temporary directory of the OS: deleting everything in it touches
|
||||
/// no file of another program.
|
||||
/// The files lie in the temporary folder of AI Studio, see TemporaryFiles. Whatever a crash left
|
||||
/// behind is deleted when AI Studio starts again.
|
||||
/// </remarks>
|
||||
internal static class MailAttachmentFiles
|
||||
{
|
||||
private const string DIRECTORY_NAME = "mailAttachments";
|
||||
|
||||
/// <summary>
|
||||
/// Makes up the path of a file for one attachment, and creates the directory it lies in.
|
||||
/// Makes up the path of a file for one attachment.
|
||||
/// </summary>
|
||||
/// <param name="extension">The extension of the attachment, checked against the known document types beforehand, without a dot.</param>
|
||||
/// <returns>The path, where no file exists yet.</returns>
|
||||
/// <exception cref="InvalidOperationException">The data directory is not known yet.</exception>
|
||||
public static string CreatePath(string extension)
|
||||
{
|
||||
var directory = GetDirectory() ?? throw new InvalidOperationException("The data directory is not known yet.");
|
||||
Directory.CreateDirectory(directory);
|
||||
return Path.Join(directory, $"{Guid.NewGuid():N}.{extension}");
|
||||
}
|
||||
/// <exception cref="InvalidOperationException">AI Studio has not set its temporary folder yet.</exception>
|
||||
public static string CreatePath(string extension) => TemporaryFiles.GetFilePath($".{extension}");
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the file of one attachment, should it exist.
|
||||
@@ -48,31 +37,4 @@ internal static class MailAttachmentFiles
|
||||
logger.LogWarning(exception, "The temporary file '{Path}' of a mail attachment could not be deleted. It is deleted on the next start.", path);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes every file an earlier session of AI Studio left behind.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Only to be called before any mailbox is synced, since it deletes the files of a running sync as well.
|
||||
/// </remarks>
|
||||
/// <param name="logger">The logger.</param>
|
||||
public static void DeleteLeftovers(ILogger logger)
|
||||
{
|
||||
if (GetDirectory() is not { } directory || !Directory.Exists(directory))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
Directory.Delete(directory, true);
|
||||
logger.LogInformation("Deleted the temporary files of mail attachments an earlier session left behind.");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogWarning(exception, "The temporary files of mail attachments in '{Directory}' could not be deleted.", directory);
|
||||
}
|
||||
}
|
||||
|
||||
private static string? GetDirectory() => string.IsNullOrWhiteSpace(SettingsManager.DataDirectory)
|
||||
? null
|
||||
: Path.Join(SettingsManager.DataDirectory, DIRECTORY_NAME);
|
||||
}
|
||||
Reference in new issue
Block a user