Catalog/imagecatalog/Services/PickerPreferenceService.cs
Maddo e9142df97c
Some checks failed
Build Windows Avalonia / build (push) Failing after 1m48s
Release Windows Avalonia / build (push) Failing after 1m41s
Release Windows Avalonia / release (push) Has been skipped
feat: Enhance Face AI upload functionality and UI
- Updated MainWindow.axaml to increase height and add new UI elements for SSH upload configuration.
- Implemented commands for opening source and destination paths in file explorer.
- Added FaceUploadPath and SSH configuration properties to DataModel and AiSettingsViewModel.
- Introduced validation for FaceUploadPath format and commands for uploading face encoder output.
- Enhanced PickerPreferenceService to manage SSH credentials and upload preferences.
- Updated settings persistence to include FaceUploadPath and SSH preferences.
- Added tests for FaceUploadPath validation and upload command enabling logic.
2026-06-06 11:54:21 +02:00

142 lines
No EOL
4.8 KiB
C#

using Avalonia.Platform.Storage;
using ImageCatalog;
using System;
using System.IO;
using System.Threading.Tasks;
namespace ImageCatalog_2.Services;
public static class PickerPreferenceKeys
{
public const string SourceFolder = "Picker.SourceFolder.LastPath";
public const string DestinationFolder = "Picker.DestinationFolder.LastPath";
public const string LogoFile = "Picker.LogoFile.LastPath";
public const string ModelsFolder = "Picker.ModelsFolder.LastPath";
public const string CsvOutput = "Picker.CsvOutput.LastPath";
public const string SettingsFile = "Picker.SettingsFile.LastPath";
public const string LastSettingsFile = "Settings.LastFilePath";
public const string FaceExecutableFolder = "Picker.FaceExecutableFolder.LastPath";
public const string FaceOutputFolder = "Picker.FaceOutputFolder.LastPath";
public const string FaceMatcherExecutable = "Picker.FaceMatcherExecutable.LastPath";
public const string FaceMatcherImage = "Picker.FaceMatcherImage.LastPath";
public const string FaceMatcherEncodings = "Picker.FaceMatcherEncodings.LastPath";
public const string FaceMatcherOutput = "Picker.FaceMatcherOutput.LastPath";
public const string FaceMatcherLog = "Picker.FaceMatcherLog.LastPath";
public const string FaceSshUsername = "FaceAI.Ssh.Username";
public const string FaceSshPassword = "FaceAI.Ssh.Password";
public const string FaceSshAddress = "FaceAI.Ssh.Address";
public const string FaceSshPort = "FaceAI.Ssh.Port";
public const string FaceSshPathA = "FaceAI.Ssh.PathA";
public const string FaceSshPathB = "FaceAI.Ssh.PathB";
public const string FaceUploadDryRun = "FaceAI.Upload.DryRun";
}
public sealed class PickerPreferenceService
{
private readonly ParametriSetup _userPreferences;
public PickerPreferenceService(ParametriSetup userPreferences)
{
_userPreferences = userPreferences;
}
public async Task<IStorageFolder?> TryGetStartFolderAsync(IStorageProvider storageProvider, string preferenceKey, string? currentPath = null)
{
var startPath = GetPreferredStartDirectory(preferenceKey, currentPath);
if (string.IsNullOrWhiteSpace(startPath))
{
return null;
}
try
{
return await storageProvider.TryGetFolderFromPathAsync(new Uri(startPath)).ConfigureAwait(true);
}
catch
{
return null;
}
}
public void RememberPath(string preferenceKey, string? selectedPath)
{
var directory = TryGetExistingDirectory(selectedPath);
if (string.IsNullOrWhiteSpace(directory))
{
return;
}
_userPreferences.AggiornaParametro(preferenceKey, directory);
_userPreferences.SalvaParametriSetup();
}
public string? GetRememberedValue(string preferenceKey)
{
var value = _userPreferences.LeggiParametroString(preferenceKey);
return string.IsNullOrWhiteSpace(value)
? null
: value.Trim().Trim('"');
}
public string? GetRememberedRawValue(string preferenceKey)
{
return _userPreferences.ParametroExists(preferenceKey)
? _userPreferences.LeggiParametroString(preferenceKey)
: null;
}
public void RememberValue(string preferenceKey, string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
return;
}
_userPreferences.AggiornaParametro(preferenceKey, value.Trim().Trim('"'));
_userPreferences.SalvaParametriSetup();
}
public void RememberRawValue(string preferenceKey, string? value)
{
_userPreferences.AggiornaParametro(preferenceKey, value ?? string.Empty);
_userPreferences.SalvaParametriSetup();
}
public void ForgetValue(string preferenceKey)
{
if (_userPreferences.RimuoviParametro(preferenceKey))
{
_userPreferences.SalvaParametriSetup();
}
}
private string? GetPreferredStartDirectory(string preferenceKey, string? currentPath)
{
var storedPath = _userPreferences.LeggiParametroString(preferenceKey);
return TryGetExistingDirectory(storedPath) ?? TryGetExistingDirectory(currentPath);
}
private static string? TryGetExistingDirectory(string? path)
{
if (string.IsNullOrWhiteSpace(path))
{
return null;
}
var normalizedPath = path.Trim().Trim('"');
if (Directory.Exists(normalizedPath))
{
return normalizedPath;
}
if (File.Exists(normalizedPath))
{
return Path.GetDirectoryName(normalizedPath);
}
var containingDirectory = Path.GetDirectoryName(normalizedPath);
return !string.IsNullOrWhiteSpace(containingDirectory) && Directory.Exists(containingDirectory)
? containingDirectory
: null;
}
}