56 lines
2.0 KiB
C#
56 lines
2.0 KiB
C#
using Inspectron.Epson.PrintServer.ConfigurationSources;
|
|
|
|
namespace EpsonPrintService;
|
|
|
|
public record ConfigLoadResult(bool Success, EpsonPrintServiceConfiguration? Config, string? Error);
|
|
|
|
public static class ConfigurationLoader
|
|
{
|
|
public static ConfigLoadResult TryLoadFromBase64(string base64Content)
|
|
{
|
|
try
|
|
{
|
|
var decoded = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(base64Content.Trim()));
|
|
var parts = decoded.Split(';');
|
|
if (parts.Length != 3)
|
|
return new ConfigLoadResult(false, null, $"Invalid configuration format. Expected 'apiUrl;restaurantId;apiKey', got {parts.Length} parts.");
|
|
|
|
var config = new EpsonPrintServiceConfiguration
|
|
{
|
|
ApiUrl = parts[0],
|
|
RestaurantId = parts[1],
|
|
ApiKey = parts[2]
|
|
};
|
|
|
|
if (string.IsNullOrWhiteSpace(config.ApiUrl) || string.IsNullOrWhiteSpace(config.RestaurantId) || string.IsNullOrWhiteSpace(config.ApiKey))
|
|
return new ConfigLoadResult(false, null, "Configuration contains empty values.");
|
|
|
|
return new ConfigLoadResult(true, config, null);
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
return new ConfigLoadResult(false, null, "Invalid base64 content.");
|
|
}
|
|
}
|
|
|
|
public static ConfigLoadResult TryLoadFromFile()
|
|
{
|
|
try
|
|
{
|
|
var configPath = ConfigurationPaths.GetConfigPath();
|
|
if (!File.Exists(configPath))
|
|
return new ConfigLoadResult(false, null, $"Configuration file not found: {configPath}");
|
|
|
|
var base64Line = File.ReadAllText(configPath).Trim();
|
|
if (string.IsNullOrWhiteSpace(base64Line))
|
|
return new ConfigLoadResult(false, null, "Configuration file is empty.");
|
|
|
|
return TryLoadFromBase64(base64Line);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ConfigLoadResult(false, null, $"Failed to read configuration: {ex.Message}");
|
|
}
|
|
}
|
|
}
|