CDN Integration for Admin
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
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 MinioConfig _config;
|
||||
private readonly ILogger<MinioCdnBrowserService> _logger;
|
||||
|
||||
public bool IsConfigured => !string.IsNullOrEmpty(_config.AccessKey) && !string.IsNullOrEmpty(_config.SecretKey);
|
||||
|
||||
public MinioCdnBrowserService(IConfiguration configuration, ILogger<MinioCdnBrowserService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = configuration.GetSection("MinIO").Get<MinioConfig>() ?? new MinioConfig();
|
||||
|
||||
if (IsConfigured)
|
||||
{
|
||||
try
|
||||
{
|
||||
_client = new MinioClient()
|
||||
.WithEndpoint(_config.Endpoint)
|
||||
.WithCredentials(_config.AccessKey, _config.SecretKey)
|
||||
.WithSSL(_config.UseSSL)
|
||||
.Build();
|
||||
|
||||
_logger.LogInformation("MinIO client initialized for endpoint: {Endpoint}", _config.Endpoint);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to initialize MinIO client");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("MinIO not configured - CDN browsing disabled. Set MinIO:AccessKey and MinIO:SecretKey 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(_config.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(_config.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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user