mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-05 05: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
@@ -17,13 +17,6 @@ public sealed class PluginShareService(NativeShareService nativeShareService, Ru
|
||||
|
||||
private const string PLUGIN_FILE_NAME = "plugin.lua";
|
||||
|
||||
/// <remarks>
|
||||
/// Keep in sync with SHARE_DIRECTORY_NAME in runtime/src/share_sheet.rs: the runtime only hands
|
||||
/// archives from a directory with this name to the native share sheet.
|
||||
/// </remarks>
|
||||
private const string TEMPORARY_ARCHIVE_DIRECTORY = "mindwork-ai-studio-plugin-shares";
|
||||
|
||||
private const int TEMPORARY_ARCHIVE_RETENTION_HOURS = 24;
|
||||
private const int FILE_NAME_PREFIX_MAX_LEN = 80;
|
||||
|
||||
/// <summary>
|
||||
@@ -37,8 +30,8 @@ public sealed class PluginShareService(NativeShareService nativeShareService, Ru
|
||||
/// The archive contains the plugin root contents, so <c>plugin.lua</c> is located at the archive root.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// On Windows and macOS, the archive is created in a temporary directory and handed over to the
|
||||
/// native share sheet. Linux has no such share sheet, since the XDG desktop portals do not provide
|
||||
/// On Windows and macOS, the archive is created in the temporary folder of AI Studio and handed over
|
||||
/// to the native share sheet. Linux has no such share sheet, since the XDG desktop portals do not provide
|
||||
/// a share interface. Thus, the archive is exported to a location of the user's choice there. The
|
||||
/// same happens on Windows and macOS once the share sheet failed or did not respond, until AI
|
||||
/// Studio restarts.
|
||||
@@ -162,22 +155,25 @@ public sealed class PluginShareService(NativeShareService nativeShareService, Ru
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the plugin archive in a temporary directory and opens the native share sheet for it.
|
||||
/// Creates the plugin archive in the temporary folder of AI Studio and opens the native share sheet for it.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The runtime hands only archives from the temporary folder of AI Studio to the share sheet, see
|
||||
/// TemporaryFiles. The archive gets a folder of its own there, so it can carry the name of the plugin,
|
||||
/// which the recipient sees, just like an exported archive.
|
||||
/// </remarks>
|
||||
/// <param name="plugin">The local plugin to archive and share.</param>
|
||||
/// <param name="pluginRoot">The validated plugin root directory.</param>
|
||||
/// <param name="token">Cancellation token for archive creation.</param>
|
||||
/// <returns>The share result, including the retained temporary archive path when successful.</returns>
|
||||
private async Task<PluginShareResult> ShareViaNativeSheetAsync(IAvailablePlugin plugin, string pluginRoot, CancellationToken token)
|
||||
{
|
||||
var archiveDirectory = Path.Join(Path.GetTempPath(), TEMPORARY_ARCHIVE_DIRECTORY);
|
||||
var archivePath = Path.Join(archiveDirectory, $"{CreateSafeFileNamePrefix(plugin.Name)}-{plugin.Id:N}-{Guid.NewGuid():N}{PluginArchive.PLUGIN_FILE_EXTENSION}");
|
||||
|
||||
string? archiveDirectory = null;
|
||||
try
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
Directory.CreateDirectory(archiveDirectory);
|
||||
this.CleanUpExpiredArchives(archiveDirectory);
|
||||
archiveDirectory = TemporaryFiles.CreateDirectory();
|
||||
var archivePath = Path.Join(archiveDirectory, $"{CreateSafeFileNamePrefix(plugin.Name)}{PluginArchive.PLUGIN_FILE_EXTENSION}");
|
||||
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
@@ -194,7 +190,7 @@ public sealed class PluginShareService(NativeShareService nativeShareService, Ru
|
||||
|
||||
//
|
||||
// A share sheet which did not respond may still appear later and ask for the archive.
|
||||
// We keep it for that case; the clean-up removes it once it expired:
|
||||
// We keep it for that case; AI Studio deletes it when it quits or starts again:
|
||||
//
|
||||
case ShareSheetOutcome.UNRESPONSIVE:
|
||||
return ShareSheetUnavailable(plugin, TA("The share dialog of your operating system did not respond."));
|
||||
@@ -202,7 +198,7 @@ public sealed class PluginShareService(NativeShareService nativeShareService, Ru
|
||||
case ShareSheetOutcome.FAILED:
|
||||
case ShareSheetOutcome.UNKNOWN:
|
||||
default:
|
||||
this.TryDeleteArchive(archivePath);
|
||||
this.TryDeleteArchiveDirectory(archiveDirectory);
|
||||
return ShareSheetUnavailable(plugin, TB("The native share dialog could not be opened."));
|
||||
}
|
||||
|
||||
@@ -211,12 +207,12 @@ public sealed class PluginShareService(NativeShareService nativeShareService, Ru
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
this.TryDeleteArchive(archivePath);
|
||||
this.TryDeleteArchiveDirectory(archiveDirectory);
|
||||
throw;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
this.TryDeleteArchive(archivePath);
|
||||
this.TryDeleteArchiveDirectory(archiveDirectory);
|
||||
logger.LogError(exception, "Failed to create a share archive for plugin '{PluginName}' ({PluginId}).", plugin.Name, plugin.Id);
|
||||
return ShareError(plugin, string.Format(TB("Unexpected error: {0}"), exception.Message));
|
||||
}
|
||||
@@ -259,23 +255,6 @@ public sealed class PluginShareService(NativeShareService nativeShareService, Ru
|
||||
return true;
|
||||
}
|
||||
|
||||
private void CleanUpExpiredArchives(string archiveDirectory)
|
||||
{
|
||||
var expiry = DateTime.UtcNow.AddHours(-TEMPORARY_ARCHIVE_RETENTION_HOURS);
|
||||
foreach (var archivePath in Directory.EnumerateFiles(archiveDirectory, $"*{PluginArchive.PLUGIN_FILE_EXTENSION}", SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.GetLastWriteTimeUtc(archivePath) < expiry)
|
||||
File.Delete(archivePath);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogWarning(exception, "Failed to delete expired plugin archive '{ArchivePath}'.", archivePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string CreateSafeFileNamePrefix(string pluginName)
|
||||
{
|
||||
var invalidCharacters = Path.GetInvalidFileNameChars().ToHashSet();
|
||||
@@ -300,6 +279,21 @@ public sealed class PluginShareService(NativeShareService nativeShareService, Ru
|
||||
return childPath.StartsWith(parentPath, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private void TryDeleteArchiveDirectory(string? archiveDirectory)
|
||||
{
|
||||
if (archiveDirectory is null || !Directory.Exists(archiveDirectory))
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
Directory.Delete(archiveDirectory, true);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
logger.LogWarning(exception, "Failed to delete the temporary plugin archive in '{ArchiveDirectory}'. AI Studio deletes it when it starts again.", archiveDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
private void TryDeleteArchive(string archivePath)
|
||||
{
|
||||
if (!File.Exists(archivePath))
|
||||
|
||||
Reference in new issue
Block a user