forked from gongxuegit/tiku-backend.net
feat: add asset access signing endpoints
This commit is contained in:
97
Tiku.Api/Controllers/AssetsController.cs
Normal file
97
Tiku.Api/Controllers/AssetsController.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Tiku.Api.Contracts;
|
||||
using Tiku.Application.Assets;
|
||||
using Tiku.Application.Security;
|
||||
using Tiku.Domain.Tenancy;
|
||||
using Tiku.Infrastructure.Persistence;
|
||||
|
||||
namespace Tiku.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
[Produces("application/json")]
|
||||
[Route("api/assets")]
|
||||
public sealed class AssetsController(
|
||||
IAssetAccessService assetAccessService,
|
||||
ICurrentTenant currentTenant,
|
||||
ICurrentUser currentUser,
|
||||
TikuDbContext dbContext) : ControllerBase
|
||||
{
|
||||
[HttpGet("{assetId:guid}/download")]
|
||||
[EndpointSummary("获取资源下载地址")]
|
||||
[ProducesResponseType<AssetAccessResponseDto>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status503ServiceUnavailable)]
|
||||
public async Task<ActionResult<AssetAccessResponseDto>> Download(
|
||||
Guid assetId,
|
||||
[FromQuery] AssetAccessQueryDto query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await assetAccessService.DownloadAsync(
|
||||
ToRequest(assetId, query, await ResolveTenantIdAsync(query.TenantCode, cancellationToken)),
|
||||
cancellationToken);
|
||||
|
||||
return Ok(AssetAccessResponseDto.FromApplication(result));
|
||||
}
|
||||
|
||||
[HttpGet("{assetId:guid}/preview")]
|
||||
[EndpointSummary("获取资源预览地址")]
|
||||
[ProducesResponseType<AssetAccessResponseDto>(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status403Forbidden)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
|
||||
[ProducesResponseType<ProblemDetails>(StatusCodes.Status503ServiceUnavailable)]
|
||||
public async Task<ActionResult<AssetAccessResponseDto>> Preview(
|
||||
Guid assetId,
|
||||
[FromQuery] AssetAccessQueryDto query,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await assetAccessService.PreviewAsync(
|
||||
ToRequest(assetId, query, await ResolveTenantIdAsync(query.TenantCode, cancellationToken)),
|
||||
cancellationToken);
|
||||
|
||||
return Ok(AssetAccessResponseDto.FromApplication(result));
|
||||
}
|
||||
|
||||
private AssetAccessRequest ToRequest(Guid assetId, AssetAccessQueryDto query, Guid tenantId)
|
||||
{
|
||||
return new AssetAccessRequest(
|
||||
tenantId,
|
||||
assetId,
|
||||
currentUser.UserId,
|
||||
HttpContext.Connection.RemoteIpAddress?.ToString(),
|
||||
Request.Headers.UserAgent.ToString(),
|
||||
query.ExpiresInSeconds.HasValue
|
||||
? TimeSpan.FromSeconds(query.ExpiresInSeconds.Value)
|
||||
: null);
|
||||
}
|
||||
|
||||
private async Task<Guid> ResolveTenantIdAsync(string? tenantCode, CancellationToken cancellationToken)
|
||||
{
|
||||
if (currentTenant.TenantId.HasValue)
|
||||
{
|
||||
return currentTenant.TenantId.Value;
|
||||
}
|
||||
|
||||
var resolvedTenantCode = tenantCode ?? Request.Headers["x-tenant-code"].FirstOrDefault();
|
||||
if (string.IsNullOrWhiteSpace(resolvedTenantCode))
|
||||
{
|
||||
throw new TenantNotFoundException();
|
||||
}
|
||||
|
||||
var tenantId = await dbContext.Tenants
|
||||
.Where(tenant =>
|
||||
tenant.Slug == resolvedTenantCode.Trim() &&
|
||||
tenant.Status == TenantStatus.Active)
|
||||
.Select(tenant => (Guid?)tenant.Id)
|
||||
.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
return tenantId ?? throw new TenantNotFoundException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user