mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-04 09:28:43 +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
@@ -44,15 +44,24 @@ public static partial class PluginFactory
|
||||
var downloadUrl = $"{serverUrl}/{configPlugId}.zip";
|
||||
|
||||
LOG.LogInformation($"Try to download configuration plugin with ID='{configPlugId}' from server='{configServerUrl}' (GET {downloadUrl})");
|
||||
var tempDownloadFile = Path.GetTempFileName();
|
||||
var stagedDirectory = Path.Join(ENTERPRISE_CONFIGURATION_PLUGINS_ROOT, $"{configPlugId}.staging-{Guid.NewGuid():N}");
|
||||
string? downloadDirectory = null;
|
||||
string? backupDirectory = null;
|
||||
var wasSuccessful = false;
|
||||
try
|
||||
{
|
||||
await LockHotReloadAsync();
|
||||
|
||||
// In the temporary folder of AI Studio, below the data directory: on the drive of the plugins, so the new configuration can move there:
|
||||
downloadDirectory = TemporaryFiles.CreateDirectory();
|
||||
var tempDownloadFile = Path.Join(downloadDirectory, $"{configPlugId}.zip");
|
||||
var stagedDirectory = Path.Join(downloadDirectory, configPlugId.ToString());
|
||||
|
||||
using var httpClient = ExternalHttpClientTimeout.CreateHttpClient(ExternalHttpTrustPolicy.ALLOW_CUSTOM_ROOTS_WHEN_HOST_WHITELISTED);
|
||||
var response = await httpClient.GetAsync(downloadUrl, cancellationToken);
|
||||
// Only the headers, so the archive goes to the disk instead of into memory. The time limit of the client
|
||||
// then covers the headers alone, so the token keeps it for the whole download:
|
||||
using var downloadTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
downloadTimeout.CancelAfter(httpClient.Timeout);
|
||||
using var response = await httpClient.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead, downloadTimeout.Token);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
LOG.LogError($"Failed to download the enterprise configuration plugin. HTTP Status: {response.StatusCode}");
|
||||
@@ -61,7 +70,7 @@ public static partial class PluginFactory
|
||||
|
||||
await using(var tempFileStream = File.Create(tempDownloadFile))
|
||||
{
|
||||
await response.Content.CopyToAsync(tempFileStream, cancellationToken);
|
||||
await response.Content.CopyToAsync(tempFileStream, downloadTimeout.Token);
|
||||
}
|
||||
|
||||
await ExtractConfigPluginArchiveAsync(tempDownloadFile, stagedDirectory, cancellationToken);
|
||||
@@ -73,7 +82,7 @@ public static partial class PluginFactory
|
||||
Directory.Move(configDirectory, backupDirectory);
|
||||
}
|
||||
|
||||
Directory.Move(stagedDirectory, configDirectory);
|
||||
MoveDeploymentIntoPlace(stagedDirectory, configDirectory);
|
||||
if (!string.IsNullOrWhiteSpace(backupDirectory) && Directory.Exists(backupDirectory))
|
||||
Directory.Delete(backupDirectory, true);
|
||||
|
||||
@@ -99,36 +108,39 @@ public static partial class PluginFactory
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (Directory.Exists(stagedDirectory))
|
||||
if (downloadDirectory is not null && Directory.Exists(downloadDirectory))
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(stagedDirectory, true);
|
||||
Directory.Delete(downloadDirectory, true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOG.LogError(e, "Failed to delete the staged configuration plugin directory.");
|
||||
LOG.LogError(e, "Failed to delete the downloaded configuration plugin. The next start of AI Studio deletes it.");
|
||||
}
|
||||
}
|
||||
|
||||
if (File.Exists(tempDownloadFile))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(tempDownloadFile);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOG.LogError(e, "Failed to delete the temporary download file.");
|
||||
}
|
||||
}
|
||||
|
||||
UnlockHotReload();
|
||||
}
|
||||
|
||||
return wasSuccessful;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves a downloaded deployment of an organization from the temporary folder of AI Studio to its place.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Unlike unpacking, Directory.Move creates no missing folders above its destination. The folder of the
|
||||
/// deployments is missing on a new installation until the first deployment arrives, and once somebody deleted it.
|
||||
/// </remarks>
|
||||
/// <param name="stagedDirectory">The unpacked deployment.</param>
|
||||
/// <param name="deploymentDirectory">The directory of the deployment, named after its configuration ID. It must not exist.</param>
|
||||
internal static void MoveDeploymentIntoPlace(string stagedDirectory, string deploymentDirectory)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(deploymentDirectory)!);
|
||||
Directory.Move(stagedDirectory, deploymentDirectory);
|
||||
}
|
||||
|
||||
private static async Task ExtractConfigPluginArchiveAsync(string sourceArchiveFileName, string destinationDirectory, CancellationToken token)
|
||||
{
|
||||
await PluginArchive.ExtractAsync(sourceArchiveFileName, destinationDirectory, token);
|
||||
|
||||
Reference in new issue
Block a user