2
0
mirror of synced 2025-02-22 13:08:08 +00:00

implementing

This commit is contained in:
benbierens 2024-11-01 09:24:46 +01:00
parent bb3f0fccd0
commit c785df3adf
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
5 changed files with 177 additions and 9 deletions

View File

@ -0,0 +1,51 @@
using CodexOpenApi;
using System.Net.Http;
using System.Windows;
using Utils;
namespace DevconBoothImages
{
public class CodexWrapper
{
public async Task<List<CodexApi>> GetCodexes()
{
var config = new Configuration();
var result = new List<CodexApi>();
foreach (var endpoint in config.CodexEndpoints)
{
var splitIndex = endpoint.LastIndexOf(':');
var host = endpoint.Substring(0, splitIndex);
var port = Convert.ToInt32(endpoint.Substring(splitIndex + 1));
var address = new Address(
host: host,
port: port
);
var client = new HttpClient();
var codex = new CodexApi(client);
codex.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1";
await CheckCodex(codex, endpoint);
}
return result;
}
private async Task CheckCodex(CodexApi codex, string endpoint)
{
try
{
var info = await codex.GetDebugInfoAsync();
if (string.IsNullOrEmpty(info.Id)) throw new Exception("Failed to fetch Codex node id");
}
catch (Exception ex)
{
MessageBox.Show($"Failed to connect to codex '{endpoint}': {ex}");
throw;
}
}
}
}

View File

@ -0,0 +1,16 @@
namespace DevconBoothImages
{
public class Configuration
{
public string[] CodexEndpoints { get; } =
[
"aaaa",
"bbbb"
];
public string AuthUser { get; } = "";
public string AuthPw { get; } = "";
public string LocalNodeBootstrapInfo { get; } = "";
public string WorkingDir { get; } = "D:\\DevconBoothApp";
}
}

View File

@ -8,4 +8,9 @@
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ProjectPlugins\CodexPlugin\CodexPlugin.csproj" />
<ProjectReference Include="..\Tools\AutoClient\AutoClient.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -14,8 +14,10 @@
<Image Grid.Row="0" Name="Img" /> <Image Grid.Row="0" Name="Img" />
<StackPanel Grid.Row="1"> <StackPanel Grid.Row="1">
<Button Content="Generate image -> Upload to Codex -> Put CID info in clipboard" Padding="10"/> <TextBlock Name="Txt" HorizontalAlignment="Center" />
<Button Content="Put last CID info in clipboard" Padding="10"/> <Button Content="Check Codex connections" Click="Button_Click_2" />
<Button Content="Generate image -> Upload to Codex -> Put CID info in clipboard" Padding="10" Click="Button_Click"/>
<Button Content="Put last CID info in clipboard" Padding="10" Click="Button_Click_1"/>
</StackPanel> </StackPanel>
</Grid> </Grid>
</Window> </Window>

View File

@ -1,27 +1,121 @@
using System.Windows; using AutoClient;
using Logging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
namespace DevconBoothImages namespace DevconBoothImages
{ {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private readonly Configuration config = new Configuration();
private readonly CodexWrapper codexWrapper = new CodexWrapper();
private readonly ImageGenerator imageGenerator = new ImageGenerator(new NullLog());
private string[] currentCids = Array.Empty<string>();
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
} }
private void Button_Click(object sender, RoutedEventArgs e) private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("Unhandled exception: " + e.Exception);
}
private async void Button_Click(object sender, RoutedEventArgs e)
{ {
// image // image
Log("Getting image...");
var file = await imageGenerator.Generate();
var filename = Path.Combine(config.WorkingDir, file);
File.Copy(file, filename);
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(filename);
bmp.EndInit();
Img.Source = bmp;
Log("Uploading...");
// upload // upload
await UploadToCodexes(filename, file);
// clipboard info // clipboard info
InfoToClipboard();
} }
private void Button_Click_1(object sender, RoutedEventArgs e) private async void Button_Click_1(object sender, RoutedEventArgs e)
{ {
// clipboard info InfoToClipboard();
}
private async void Button_Click_2(object sender, RoutedEventArgs e)
{
// check codexes
Log("Checking Codex connections...");
await codexWrapper.GetCodexes();
Log("Connections OK");
}
private async Task UploadToCodexes(string filename, string shortName)
{
var result = new List<string>();
var codexes = await codexWrapper.GetCodexes();
try
{
foreach (var codex in codexes)
{
using (var fileStream = File.OpenRead(filename))
{
var response = await codex.UploadAsync(
"application/image??",
"attachement filanem???",
fileStream);
if (string.IsNullOrEmpty(response) ||
response.ToLowerInvariant().Contains("unable to store block"))
{
MessageBox.Show("Unable to upload image. Response empty or error message.");
}
else
{
result.Add(response);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Upload failed: " + ex);
}
Log($"Generated {result.Count} CIDs");
currentCids = result.ToArray();
}
private void InfoToClipboard()
{
Clipboard.Clear();
if (!currentCids.Any())
{
Log("No CIDs were generated! Clipboard cleared.");
return;
}
var msg =
"";
Clipboard.SetText(msg);
Log("CID info copied to clipboard. Paste it in Discord plz!");
}
private void Log(string v)
{
Txt.Text = v;
} }
} }
} }