76 lines
2.5 KiB
C#
76 lines
2.5 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Deal.Storage.Options;
|
|
|
|
/// <summary>
|
|
/// Настройки объектного хранилища сервиса данных.
|
|
/// </summary>
|
|
public sealed class StorageOptions
|
|
{
|
|
/// <summary>
|
|
/// Значение бакета по умолчанию.
|
|
/// </summary>
|
|
public const string DefaultBucketName = "deal-attachments";
|
|
|
|
/// <summary>
|
|
/// Env-ключ адреса хранилища (host:port).
|
|
/// </summary>
|
|
public const string EndpointEnvKey = "DEAL_STORAGE_ENDPOINT";
|
|
|
|
/// <summary>
|
|
/// Env-ключ ключа доступа.
|
|
/// </summary>
|
|
public const string AccessKeyEnvKey = "DEAL_STORAGE_ACCESS_KEY";
|
|
|
|
/// <summary>
|
|
/// Env-ключ секрета доступа.
|
|
/// </summary>
|
|
public const string SecretKeyEnvKey = "DEAL_STORAGE_SECRET_KEY";
|
|
|
|
/// <summary>
|
|
/// Env-ключ имени бакета.
|
|
/// </summary>
|
|
public const string BucketEnvKey = "DEAL_STORAGE_BUCKET";
|
|
|
|
/// <summary>
|
|
/// Env-ключ флага TLS (1/true — https).
|
|
/// </summary>
|
|
public const string SecureEnvKey = "DEAL_STORAGE_SECURE";
|
|
|
|
private const string TrueLiteral = "true";
|
|
|
|
public string Endpoint { get; init; } = string.Empty;
|
|
|
|
public string AccessKey { get; init; } = string.Empty;
|
|
|
|
public string SecretKey { get; init; } = string.Empty;
|
|
|
|
public string Bucket { get; init; } = DefaultBucketName;
|
|
|
|
public bool Secure { get; init; }
|
|
|
|
/// <summary>
|
|
/// Читает настройки из конфигурации хоста.
|
|
/// </summary>
|
|
public static StorageOptions FromConfiguration(IConfiguration configuration)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
string bucket = (configuration[BucketEnvKey] ?? string.Empty).Trim();
|
|
return new StorageOptions
|
|
{
|
|
Endpoint = (configuration[EndpointEnvKey] ?? string.Empty).Trim(),
|
|
AccessKey = (configuration[AccessKeyEnvKey] ?? string.Empty).Trim(),
|
|
SecretKey = configuration[SecretKeyEnvKey] ?? string.Empty,
|
|
Bucket = bucket.Length == 0 ? DefaultBucketName : bucket,
|
|
Secure = IsEnabled(configuration[SecureEnvKey]),
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Разбирает значение флага TLS.
|
|
/// </summary>
|
|
public static bool IsEnabled(string? rawValue)
|
|
=> string.Equals(rawValue, "1", StringComparison.Ordinal)
|
|
|| string.Equals(rawValue, TrueLiteral, StringComparison.OrdinalIgnoreCase);
|
|
}
|