2025-06-01 19:14:21 +00:00
|
|
|
using System.IO.Compression;
|
2025-06-02 18:08:25 +00:00
|
|
|
using System.Net.Http.Headers;
|
2025-06-01 19:14:21 +00:00
|
|
|
|
|
|
|
namespace AIStudio.Tools.PluginSystem;
|
|
|
|
|
|
|
|
public static partial class PluginFactory
|
|
|
|
{
|
2025-06-02 18:08:25 +00:00
|
|
|
public static async Task<EntityTagHeaderValue?> DetermineConfigPluginETagAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default)
|
|
|
|
{
|
2025-06-02 18:22:15 +00:00
|
|
|
if(configPlugId == Guid.Empty || string.IsNullOrWhiteSpace(configServerUrl))
|
|
|
|
return null;
|
|
|
|
|
2025-06-02 18:08:25 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var serverUrl = configServerUrl.EndsWith('/') ? configServerUrl[..^1] : configServerUrl;
|
|
|
|
var downloadUrl = $"{serverUrl}/{configPlugId}.zip";
|
|
|
|
|
|
|
|
using var http = new HttpClient();
|
|
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, downloadUrl);
|
|
|
|
var response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
|
|
|
return response.Headers.ETag;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
LOG.LogError(e, "An error occurred while determining the ETag for the configuration plugin.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-01 19:14:21 +00:00
|
|
|
public static async Task<bool> TryDownloadingConfigPluginAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default)
|
|
|
|
{
|
2025-06-02 18:08:25 +00:00
|
|
|
if(!IS_INITIALIZED)
|
|
|
|
{
|
|
|
|
LOG.LogWarning("Plugin factory is not yet initialized. Cannot download configuration plugin.");
|
2025-06-01 19:14:21 +00:00
|
|
|
return false;
|
2025-06-02 18:08:25 +00:00
|
|
|
}
|
2025-06-01 19:14:21 +00:00
|
|
|
|
2025-06-02 18:08:25 +00:00
|
|
|
var serverUrl = configServerUrl.EndsWith('/') ? configServerUrl[..^1] : configServerUrl;
|
|
|
|
var downloadUrl = $"{serverUrl}/{configPlugId}.zip";
|
|
|
|
|
|
|
|
LOG.LogInformation($"Try to download configuration plugin with ID='{configPlugId}' from server='{configServerUrl}' (GET {downloadUrl})");
|
2025-06-01 19:14:21 +00:00
|
|
|
var tempDownloadFile = Path.GetTempFileName();
|
|
|
|
try
|
|
|
|
{
|
2025-06-09 12:06:54 +00:00
|
|
|
await LockHotReloadAsync();
|
2025-06-01 19:14:21 +00:00
|
|
|
using var httpClient = new HttpClient();
|
2025-06-02 18:08:25 +00:00
|
|
|
var response = await httpClient.GetAsync(downloadUrl, cancellationToken);
|
2025-06-01 19:14:21 +00:00
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
{
|
2025-06-02 18:08:25 +00:00
|
|
|
await using(var tempFileStream = File.Create(tempDownloadFile))
|
|
|
|
{
|
|
|
|
await response.Content.CopyToAsync(tempFileStream, cancellationToken);
|
|
|
|
}
|
2025-06-01 19:14:21 +00:00
|
|
|
|
2025-06-02 18:08:25 +00:00
|
|
|
var configDirectory = Path.Join(CONFIGURATION_PLUGINS_ROOT, configPlugId.ToString());
|
|
|
|
if(Directory.Exists(configDirectory))
|
|
|
|
Directory.Delete(configDirectory, true);
|
2025-06-01 19:14:21 +00:00
|
|
|
|
2025-06-02 18:08:25 +00:00
|
|
|
Directory.CreateDirectory(configDirectory);
|
|
|
|
ZipFile.ExtractToDirectory(tempDownloadFile, configDirectory);
|
2025-06-01 19:14:21 +00:00
|
|
|
|
2025-06-02 18:08:25 +00:00
|
|
|
LOG.LogInformation($"Configuration plugin with ID='{configPlugId}' downloaded and extracted successfully to '{configDirectory}'.");
|
2025-06-01 19:14:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG.LogError($"Failed to download the enterprise configuration plugin. HTTP Status: {response.StatusCode}");
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
LOG.LogError(e, "An error occurred while downloading or extracting the enterprise configuration plugin.");
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (File.Exists(tempDownloadFile))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
File.Delete(tempDownloadFile);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
LOG.LogError(e, "Failed to delete the temporary download file.");
|
|
|
|
}
|
|
|
|
}
|
2025-06-09 12:06:54 +00:00
|
|
|
|
|
|
|
UnlockHotReload();
|
2025-06-01 19:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|