dramaling-vocab-learning/backend/DramaLing.Api/Models/Configuration/GoogleCloudStorageOptions.cs

69 lines
2.0 KiB
C#

using Microsoft.Extensions.Options;
namespace DramaLing.Api.Models.Configuration;
public class GoogleCloudStorageOptions
{
public const string SectionName = "GoogleCloudStorage";
/// <summary>
/// Google Cloud 專案 ID
/// </summary>
public string ProjectId { get; set; } = string.Empty;
/// <summary>
/// Storage Bucket 名稱
/// </summary>
public string BucketName { get; set; } = string.Empty;
/// <summary>
/// Service Account JSON 金鑰檔案路徑
/// </summary>
public string CredentialsPath { get; set; } = string.Empty;
/// <summary>
/// Service Account JSON 金鑰內容 (用於環境變數)
/// </summary>
public string CredentialsJson { get; set; } = string.Empty;
/// <summary>
/// 自訂域名 (用於 CDN)
/// </summary>
public string CustomDomain { get; set; } = string.Empty;
/// <summary>
/// 是否使用自訂域名
/// </summary>
public bool UseCustomDomain { get; set; } = false;
/// <summary>
/// 圖片路徑前綴
/// </summary>
public string PathPrefix { get; set; } = "examples";
/// <summary>
/// 傳統 API Key (如果使用)
/// </summary>
public string ApiKey { get; set; } = string.Empty;
}
public class GoogleCloudStorageOptionsValidator : IValidateOptions<GoogleCloudStorageOptions>
{
public ValidateOptionsResult Validate(string? name, GoogleCloudStorageOptions options)
{
var failures = new List<string>();
if (string.IsNullOrEmpty(options.ProjectId))
failures.Add("GoogleCloudStorage:ProjectId is required");
if (string.IsNullOrEmpty(options.BucketName))
failures.Add("GoogleCloudStorage:BucketName is required");
// 認證方式是可選的 - 可以使用 Application Default Credentials
// 不強制要求明確的認證設定
return failures.Count > 0
? ValidateOptionsResult.Fail(failures)
: ValidateOptionsResult.Success;
}
}