forked from MindWorkAI/AI-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginFactory.Download.cs
More file actions
87 lines (76 loc) · 3.5 KB
/
PluginFactory.Download.cs
File metadata and controls
87 lines (76 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.IO.Compression;
using System.Net.Http.Headers;
namespace AIStudio.Tools.PluginSystem;
public static partial class PluginFactory
{
public static async Task<EntityTagHeaderValue?> DetermineConfigPluginETagAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default)
{
if(configPlugId == Guid.Empty || string.IsNullOrWhiteSpace(configServerUrl))
return null;
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;
}
}
public static async Task<bool> TryDownloadingConfigPluginAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default)
{
if(!IS_INITIALIZED)
{
LOG.LogWarning("Plugin factory is not yet initialized. Cannot download configuration plugin.");
return false;
}
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})");
var tempDownloadFile = Path.GetTempFileName();
try
{
using var httpClient = new HttpClient();
var response = await httpClient.GetAsync(downloadUrl, cancellationToken);
if (response.IsSuccessStatusCode)
{
await using(var tempFileStream = File.Create(tempDownloadFile))
{
await response.Content.CopyToAsync(tempFileStream, cancellationToken);
}
var configDirectory = Path.Join(CONFIGURATION_PLUGINS_ROOT, configPlugId.ToString());
if(Directory.Exists(configDirectory))
Directory.Delete(configDirectory, true);
Directory.CreateDirectory(configDirectory);
ZipFile.ExtractToDirectory(tempDownloadFile, configDirectory);
LOG.LogInformation($"Configuration plugin with ID='{configPlugId}' downloaded and extracted successfully to '{configDirectory}'.");
}
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.");
}
}
}
return true;
}
}