178 lines
4.8 KiB
C#
178 lines
4.8 KiB
C#
using Microsoft.UI.Xaml;
|
|
using PhotoboothUploader.Models;
|
|
using PhotoboothUploader.Services;
|
|
using System.Linq;
|
|
using System.IO;
|
|
using Windows.Storage;
|
|
using Windows.Storage.Pickers;
|
|
using WinRT.Interop;
|
|
|
|
namespace PhotoboothUploader;
|
|
|
|
public sealed partial class MainWindow : Window
|
|
{
|
|
private const string DefaultBaseUrl = "https://fotospiel.app";
|
|
private PhotoboothConnectClient _client;
|
|
private readonly SettingsStore _settingsStore = new();
|
|
private readonly UploadService _uploadService = new();
|
|
private PhotoboothSettings _settings;
|
|
private FileSystemWatcher? _watcher;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
_settings = _settingsStore.Load();
|
|
_settings.BaseUrl ??= DefaultBaseUrl;
|
|
_client = new PhotoboothConnectClient(_settings.BaseUrl);
|
|
_settingsStore.Save(_settings);
|
|
ApplySettings();
|
|
}
|
|
|
|
private async void ConnectButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var code = (CodeBox.Text ?? string.Empty).Trim();
|
|
|
|
if (code.Length != 6 || code.Any(ch => ch is < '0' or > '9'))
|
|
{
|
|
StatusText.Text = "Bitte einen gültigen 6-stelligen Code eingeben.";
|
|
return;
|
|
}
|
|
|
|
ConnectButton.IsEnabled = false;
|
|
StatusText.Text = "Verbinde...";
|
|
|
|
var response = await _client.RedeemAsync(code);
|
|
|
|
if (response.Data is null)
|
|
{
|
|
StatusText.Text = response.Message ?? "Verbindung fehlgeschlagen.";
|
|
ConnectButton.IsEnabled = true;
|
|
return;
|
|
}
|
|
|
|
_settings.UploadUrl = ResolveUploadUrl(response.Data.UploadUrl);
|
|
_settings.Username = response.Data.Username;
|
|
_settings.Password = response.Data.Password;
|
|
_settings.ResponseFormat = response.Data.ResponseFormat;
|
|
_settingsStore.Save(_settings);
|
|
|
|
StatusText.Text = "Verbunden. Upload bereit.";
|
|
PickFolderButton.IsEnabled = true;
|
|
StartUploadPipelineIfReady();
|
|
ConnectButton.IsEnabled = true;
|
|
}
|
|
|
|
private async void PickFolderButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var picker = new FolderPicker
|
|
{
|
|
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
|
|
};
|
|
|
|
picker.FileTypeFilter.Add("*");
|
|
InitializeWithWindow.Initialize(picker, WindowNative.GetWindowHandle(this));
|
|
|
|
StorageFolder? folder = await picker.PickSingleFolderAsync();
|
|
|
|
if (folder is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_settings.WatchFolder = folder.Path;
|
|
_settingsStore.Save(_settings);
|
|
|
|
FolderText.Text = folder.Path;
|
|
StartUploadPipelineIfReady();
|
|
}
|
|
|
|
private void ApplySettings()
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(_settings.WatchFolder))
|
|
{
|
|
FolderText.Text = _settings.WatchFolder;
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(_settings.UploadUrl))
|
|
{
|
|
StatusText.Text = "Verbunden. Upload bereit.";
|
|
PickFolderButton.IsEnabled = true;
|
|
StartUploadPipelineIfReady();
|
|
}
|
|
}
|
|
|
|
private void StartUploadPipelineIfReady()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_settings.UploadUrl) || string.IsNullOrWhiteSpace(_settings.WatchFolder))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_uploadService.Start(_settings, UpdateStatus);
|
|
StartWatcher(_settings.WatchFolder);
|
|
}
|
|
|
|
private void StartWatcher(string folder)
|
|
{
|
|
_watcher?.Dispose();
|
|
|
|
_watcher = new FileSystemWatcher(folder)
|
|
{
|
|
IncludeSubdirectories = false,
|
|
EnableRaisingEvents = true,
|
|
};
|
|
|
|
_watcher.Created += OnFileChanged;
|
|
_watcher.Changed += OnFileChanged;
|
|
_watcher.Renamed += OnFileRenamed;
|
|
}
|
|
|
|
private void OnFileChanged(object sender, FileSystemEventArgs e)
|
|
{
|
|
if (!IsSupportedImage(e.FullPath))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_uploadService.Enqueue(e.FullPath);
|
|
}
|
|
|
|
private void OnFileRenamed(object sender, RenamedEventArgs e)
|
|
{
|
|
if (!IsSupportedImage(e.FullPath))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_uploadService.Enqueue(e.FullPath);
|
|
}
|
|
|
|
private bool IsSupportedImage(string path)
|
|
{
|
|
var extension = Path.GetExtension(path)?.ToLowerInvariant();
|
|
|
|
return extension is ".jpg" or ".jpeg" or ".png" or ".webp";
|
|
}
|
|
|
|
private void UpdateStatus(string message)
|
|
{
|
|
DispatcherQueue.TryEnqueue(() => StatusText.Text = message);
|
|
}
|
|
|
|
private string? ResolveUploadUrl(string? uploadUrl)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(uploadUrl))
|
|
{
|
|
return uploadUrl;
|
|
}
|
|
|
|
if (Uri.TryCreate(uploadUrl, UriKind.Absolute, out _))
|
|
{
|
|
return uploadUrl;
|
|
}
|
|
|
|
var baseUri = new Uri(_settings.BaseUrl ?? DefaultBaseUrl, UriKind.Absolute);
|
|
return new Uri(baseUri, uploadUrl).ToString();
|
|
}
|
|
}
|