feat(storage): implement storage management API and enhance storage mode handling

This commit is contained in:
shiyu
2025-06-09 12:12:15 +08:00
parent 4ef4b2056b
commit 0a6fe70537
43 changed files with 2449 additions and 907 deletions

View File

@@ -2,18 +2,43 @@ using System.Net.Http.Headers;
using System.Text;
using Foxel.Services.Attributes;
using Foxel.Services.Configuration;
using Microsoft.Extensions.Logging;
namespace Foxel.Services.Storage.Providers;
[StorageProvider(StorageType.WebDAV)]
public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavStorageProvider> logger) : IStorageProvider
public class WebDavStorageConfig
{
public string ServerUrl { get; set; } = string.Empty;
public string BasePath { get; set; } = string.Empty;
public string? UserName { get; set; }
public string? Password { get; set; }
public string? PublicUrl { get; set; }
}
[StorageProvider(StorageType.WebDAV)]
public class WebDavStorageProvider : IStorageProvider
{
private readonly WebDavStorageConfig _webDavConfig;
private readonly IConfigService _configService;
private readonly ILogger<WebDavStorageProvider> _logger;
public WebDavStorageProvider(WebDavStorageConfig webDavConfig, IConfigService configService, ILogger<WebDavStorageProvider> logger)
{
_webDavConfig = webDavConfig;
_configService = configService;
_logger = logger;
if (string.IsNullOrEmpty(_webDavConfig.ServerUrl))
{
_logger.LogError("WebDAV Storage配置不完整 (ServerUrl 是必需的).");
throw new InvalidOperationException("WebDAV Storage配置不完整。");
}
}
private HttpClient CreateClient()
{
var httpClient = new HttpClient();
var userName = configService["Storage:WebDAVUserName"];
var password = configService["Storage:WebDAVPassword"];
var userName = _webDavConfig.UserName;
var password = _webDavConfig.Password;
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
{
@@ -28,8 +53,8 @@ public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavS
{
try
{
string webDavServerUrl = configService["Storage:WebDAVServerUrl"].TrimEnd('/');
string basePath = configService["Storage:WebDAVBasePath"].Trim('/');
string webDavServerUrl = _webDavConfig.ServerUrl.TrimEnd('/');
string basePath = _webDavConfig.BasePath?.Trim('/') ?? string.Empty;
// 创建唯一的文件存储路径
string currentDate = DateTime.Now.ToString("yyyy/MM");
@@ -58,7 +83,7 @@ public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavS
}
catch (Exception ex)
{
logger.LogError(ex, "上传文件到WebDAV时出错");
_logger.LogError(ex, "上传文件到WebDAV时出错");
throw;
}
}
@@ -70,7 +95,7 @@ public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavS
if (string.IsNullOrEmpty(storagePath))
return;
string webDavServerUrl = configService["Storage:WebDAVServerUrl"].TrimEnd('/');
string webDavServerUrl = _webDavConfig.ServerUrl.TrimEnd('/');
var requestUri = $"{webDavServerUrl}/{storagePath}";
using var client = CreateClient();
@@ -83,30 +108,30 @@ public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavS
}
catch (Exception ex)
{
logger.LogWarning(ex, "从WebDAV删除文件时出错");
_logger.LogWarning(ex, "从WebDAV删除文件时出错");
}
}
public string GetUrl(string storagePath)
public string GetUrl(int pictureId,string storagePath)
{
try
{
if (string.IsNullOrEmpty(storagePath))
return "/images/unavailable.gif";
string publicUrl = configService["Storage:WebDAVPublicUrl"].TrimEnd('/');
string serverUrl = configService["AppSettings:ServerUrl"];
string? publicUrl = _webDavConfig.PublicUrl?.TrimEnd('/');
string serverUrl = _configService["AppSettings:ServerUrl"];
if (!string.IsNullOrEmpty(publicUrl))
{
return $"{publicUrl}/{storagePath}";
}
return $"{serverUrl}/api/picture/proxy?path={Uri.EscapeDataString(storagePath)}";
return $"{serverUrl}/api/picture/file/{pictureId}";
}
catch (Exception ex)
{
logger.LogError(ex, "生成WebDAV文件URL时出错");
_logger.LogError(ex, "生成WebDAV文件URL时出错");
return "/images/unavailable.gif";
}
}
@@ -120,7 +145,7 @@ public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavS
throw new ArgumentException("存储路径不能为空");
}
string webDavServerUrl = configService["Storage:WebDAVServerUrl"].TrimEnd('/');
string webDavServerUrl = _webDavConfig.ServerUrl.TrimEnd('/');
// 创建临时目录
var tempDir = Path.Combine(Path.GetTempPath(), "FoxelWebDAVTemp");
@@ -147,7 +172,7 @@ public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavS
}
catch (Exception ex)
{
logger.LogError(ex, "从WebDAV下载文件时出错");
_logger.LogError(ex, "从WebDAV下载文件时出错");
throw;
}
}
@@ -159,7 +184,7 @@ public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavS
{
try
{
string webDavServerUrl = configService["Storage:WebDAVServerUrl"].TrimEnd('/');
string webDavServerUrl = _webDavConfig.ServerUrl.TrimEnd('/');
var requestUri = $"{webDavServerUrl}/{directoryPath}";
using var client = CreateClient();
@@ -213,7 +238,7 @@ public class WebDavStorageProvider(IConfigService configService, ILogger<WebDavS
}
catch (Exception ex)
{
logger.LogError(ex, "确保WebDAV目录存在时出错");
_logger.LogError(ex, "确保WebDAV目录存在时出错");
throw;
}
}