refactor: AdminApi 剔除租户侧能力
This commit is contained in:
@@ -8,13 +8,18 @@ namespace TakeoutSaaS.Application.Storage.Contracts;
|
||||
/// <remarks>
|
||||
/// 创建直传请求。
|
||||
/// </remarks>
|
||||
public sealed class DirectUploadRequest(UploadFileType fileType, string fileName, string contentType, long contentLength, string? requestOrigin)
|
||||
public sealed class DirectUploadRequest(UploadFileType fileType, long tenantId, string fileName, string contentType, long contentLength, string? requestOrigin)
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件类型。
|
||||
/// </summary>
|
||||
public UploadFileType FileType { get; } = fileType;
|
||||
|
||||
/// <summary>
|
||||
/// 租户 ID(0 表示平台)。
|
||||
/// </summary>
|
||||
public long TenantId { get; } = tenantId;
|
||||
|
||||
/// <summary>
|
||||
/// 文件名。
|
||||
/// </summary>
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace TakeoutSaaS.Application.Storage.Contracts;
|
||||
/// </remarks>
|
||||
public sealed class UploadFileRequest(
|
||||
UploadFileType fileType,
|
||||
long tenantId,
|
||||
Stream content,
|
||||
string fileName,
|
||||
string contentType,
|
||||
@@ -21,6 +22,11 @@ public sealed class UploadFileRequest(
|
||||
/// </summary>
|
||||
public UploadFileType FileType { get; } = fileType;
|
||||
|
||||
/// <summary>
|
||||
/// 租户 ID(0 表示平台)。
|
||||
/// </summary>
|
||||
public long TenantId { get; } = tenantId;
|
||||
|
||||
/// <summary>
|
||||
/// 文件流。
|
||||
/// </summary>
|
||||
|
||||
@@ -12,7 +12,6 @@ using TakeoutSaaS.Module.Storage.Options;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
using TakeoutSaaS.Shared.Abstractions.Security;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.Storage.Services;
|
||||
|
||||
@@ -22,7 +21,6 @@ namespace TakeoutSaaS.Application.Storage.Services;
|
||||
public sealed class FileStorageService(
|
||||
IStorageProviderResolver providerResolver,
|
||||
IOptionsMonitor<StorageOptions> optionsMonitor,
|
||||
ITenantProvider tenantProvider,
|
||||
ICurrentUserAccessor currentUserAccessor,
|
||||
ILogger<FileStorageService> logger) : IFileStorageService
|
||||
{
|
||||
@@ -47,8 +45,13 @@ public sealed class FileStorageService(
|
||||
ResetStream(request.Content);
|
||||
|
||||
// 3. 生成对象键与元数据
|
||||
var objectKey = BuildObjectKey(request.FileType, extension);
|
||||
var metadata = BuildMetadata(request.FileType);
|
||||
if (request.TenantId < 0)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.ValidationFailed, "TenantId 无效");
|
||||
}
|
||||
|
||||
var objectKey = BuildObjectKey(request.TenantId, request.FileType, extension);
|
||||
var metadata = BuildMetadata(request.TenantId, request.FileType);
|
||||
var expires = TimeSpan.FromMinutes(Math.Max(1, security.DefaultUrlExpirationMinutes));
|
||||
var provider = providerResolver.Resolve();
|
||||
|
||||
@@ -89,7 +92,12 @@ public sealed class FileStorageService(
|
||||
var contentType = NormalizeContentType(request.ContentType, extension);
|
||||
|
||||
// 3. 构建直传参数
|
||||
var objectKey = BuildObjectKey(request.FileType, extension);
|
||||
if (request.TenantId < 0)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.ValidationFailed, "TenantId 无效");
|
||||
}
|
||||
|
||||
var objectKey = BuildObjectKey(request.TenantId, request.FileType, extension);
|
||||
var provider = providerResolver.Resolve();
|
||||
var expires = TimeSpan.FromMinutes(Math.Max(1, security.DefaultUrlExpirationMinutes));
|
||||
|
||||
@@ -213,9 +221,8 @@ public sealed class FileStorageService(
|
||||
/// <summary>
|
||||
/// 生成对象存储的键路径。
|
||||
/// </summary>
|
||||
private string BuildObjectKey(UploadFileType type, string extension)
|
||||
private static string BuildObjectKey(long tenantId, UploadFileType type, string extension)
|
||||
{
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var tenantSegment = tenantId == 0 ? "platform" : tenantId.ToString();
|
||||
var folder = type.ToFolderName();
|
||||
var now = DateTime.UtcNow;
|
||||
@@ -227,12 +234,12 @@ public sealed class FileStorageService(
|
||||
/// <summary>
|
||||
/// 组装对象元数据,便于追踪租户与用户。
|
||||
/// </summary>
|
||||
private IDictionary<string, string> BuildMetadata(UploadFileType type)
|
||||
private IDictionary<string, string> BuildMetadata(long tenantId, UploadFileType type)
|
||||
{
|
||||
var metadata = new Dictionary<string, string>
|
||||
{
|
||||
["x-meta-upload-type"] = type.ToString(),
|
||||
["x-meta-tenant-id"] = tenantProvider.GetCurrentTenantId().ToString()
|
||||
["x-meta-tenant-id"] = tenantId.ToString()
|
||||
};
|
||||
|
||||
if (currentUserAccessor.IsAuthenticated)
|
||||
|
||||
Reference in New Issue
Block a user