167 lines
5.5 KiB
C#
167 lines
5.5 KiB
C#
using Minio;
|
|
using Minio.DataModel.Args;
|
|
|
|
namespace Server.Services;
|
|
|
|
public class MinioConfig
|
|
{
|
|
public string Endpoint { get; set; } = "";
|
|
public string AccessKey { get; set; } = "";
|
|
public string SecretKey { get; set; } = "";
|
|
public string BucketName { get; set; } = "downloads";
|
|
public bool UseSSL { get; set; } = true;
|
|
}
|
|
|
|
public class CdnFile
|
|
{
|
|
public string Key { get; set; } = "";
|
|
public string Name { get; set; } = "";
|
|
public long Size { get; set; }
|
|
public DateTime LastModified { get; set; }
|
|
public bool IsDirectory { get; set; }
|
|
}
|
|
|
|
public interface ICdnBrowserService
|
|
{
|
|
Task<List<CdnFile>> ListFilesAsync(string prefix, int maxKeys = 100, string? continuationToken = null);
|
|
Task<List<string>> ListPrefixesAsync(string prefix);
|
|
bool IsConfigured { get; }
|
|
}
|
|
|
|
public class MinioCdnBrowserService : ICdnBrowserService
|
|
{
|
|
private readonly IMinioClient? _client;
|
|
private readonly string _bucketName;
|
|
private readonly ILogger<MinioCdnBrowserService> _logger;
|
|
private readonly bool _isConfigured;
|
|
|
|
public bool IsConfigured => _isConfigured;
|
|
|
|
public MinioCdnBrowserService(IConfiguration configuration, ILogger<MinioCdnBrowserService> logger)
|
|
{
|
|
_logger = logger;
|
|
|
|
// Try environment variables first (for Kubernetes secrets), fall back to appsettings
|
|
var endpoint = Environment.GetEnvironmentVariable("MINIO_ENDPOINT")
|
|
?? configuration["MinIO:Endpoint"]
|
|
?? "";
|
|
var accessKey = Environment.GetEnvironmentVariable("MINIO_ACCESS_KEY")
|
|
?? configuration["MinIO:AccessKey"]
|
|
?? "";
|
|
var secretKey = Environment.GetEnvironmentVariable("MINIO_SECRET_KEY")
|
|
?? configuration["MinIO:SecretKey"]
|
|
?? "";
|
|
_bucketName = Environment.GetEnvironmentVariable("MINIO_BUCKET")
|
|
?? configuration["MinIO:BucketName"]
|
|
?? "downloads";
|
|
var useSSL = Environment.GetEnvironmentVariable("MINIO_USE_SSL")?.ToLower() != "false"
|
|
&& (configuration.GetValue<bool?>("MinIO:UseSSL") ?? true);
|
|
|
|
var hasCredentials = !string.IsNullOrEmpty(accessKey) && !string.IsNullOrEmpty(secretKey);
|
|
_isConfigured = hasCredentials && !string.IsNullOrEmpty(endpoint);
|
|
|
|
if (_isConfigured)
|
|
{
|
|
try
|
|
{
|
|
_client = new MinioClient()
|
|
.WithEndpoint(endpoint)
|
|
.WithCredentials(accessKey, secretKey)
|
|
.WithSSL(useSSL)
|
|
.Build();
|
|
|
|
_logger.LogInformation("MinIO client initialized for endpoint: {Endpoint}, bucket: {Bucket}", endpoint, _bucketName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to initialize MinIO client");
|
|
_isConfigured = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning("MinIO not configured - CDN browsing disabled. Set MINIO_ENDPOINT, MINIO_ACCESS_KEY, MINIO_SECRET_KEY environment variables or MinIO section in appsettings.");
|
|
}
|
|
}
|
|
|
|
public async Task<List<CdnFile>> ListFilesAsync(string prefix, int maxKeys = 100, string? continuationToken = null)
|
|
{
|
|
var files = new List<CdnFile>();
|
|
|
|
if (_client == null)
|
|
{
|
|
_logger.LogWarning("MinIO client not initialized");
|
|
return files;
|
|
}
|
|
|
|
try
|
|
{
|
|
// Normalize prefix
|
|
if (!string.IsNullOrEmpty(prefix) && !prefix.EndsWith("/"))
|
|
prefix += "/";
|
|
|
|
var listArgs = new ListObjectsArgs()
|
|
.WithBucket(_bucketName)
|
|
.WithPrefix(prefix)
|
|
.WithRecursive(false); // Only list current level
|
|
|
|
await foreach (var item in _client.ListObjectsEnumAsync(listArgs))
|
|
{
|
|
if (files.Count >= maxKeys) break;
|
|
|
|
var isDir = item.Key.EndsWith("/");
|
|
files.Add(new CdnFile
|
|
{
|
|
Key = item.Key,
|
|
Name = isDir
|
|
? item.Key.TrimEnd('/').Split('/').Last()
|
|
: Path.GetFileName(item.Key),
|
|
Size = (long)item.Size,
|
|
LastModified = item.LastModifiedDateTime ?? DateTime.MinValue,
|
|
IsDirectory = isDir
|
|
});
|
|
}
|
|
|
|
_logger.LogDebug("Listed {Count} files from prefix: {Prefix}", files.Count, prefix);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error listing files from MinIO prefix: {Prefix}", prefix);
|
|
}
|
|
|
|
return files;
|
|
}
|
|
|
|
public async Task<List<string>> ListPrefixesAsync(string prefix)
|
|
{
|
|
var prefixes = new List<string>();
|
|
|
|
if (_client == null) return prefixes;
|
|
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(prefix) && !prefix.EndsWith("/"))
|
|
prefix += "/";
|
|
|
|
var listArgs = new ListObjectsArgs()
|
|
.WithBucket(_bucketName)
|
|
.WithPrefix(prefix)
|
|
.WithRecursive(false);
|
|
|
|
await foreach (var item in _client.ListObjectsEnumAsync(listArgs))
|
|
{
|
|
if (item.Key.EndsWith("/"))
|
|
{
|
|
prefixes.Add(item.Key);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error listing prefixes from MinIO: {Prefix}", prefix);
|
|
}
|
|
|
|
return prefixes;
|
|
}
|
|
}
|