This commit is contained in:
Халимов Рустам
2026-03-30 23:57:34 +03:00
parent d3f1e3f361
commit f1f8f6e012
27 changed files with 88 additions and 142 deletions

View File

@@ -15,7 +15,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Carter" Version="10.0.0" />
<PackageReference Include="Minio" Version="7.0.0" />
<ProjectReference Include="..\..\Contracts\Settings\Knot.Contracts.Settings.csproj" />
</ItemGroup>

View File

@@ -1,21 +1,19 @@
using Carter;
using Microsoft.Extensions.Caching.Memory;
using System.Security.Cryptography;
using System.Text;
using Knot.Contracts.Settings.Application.Abstractions;
using Knot.Contracts.Settings.Application.DTOs;
using Knot.Shared.Kernel.Storage;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Mvc;
using Knot.Contracts.Settings.Application.Abstractions;
using Knot.Contracts.Settings.Application.DTOs;
using System.Text;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Caching.Memory;
namespace Knot.Host.Presentation.Endpoints;
public sealed class FilesEndpoints : ICarterModule
public static class FilesEndpoints
{
public void AddRoutes(IEndpointRouteBuilder app)
public static void MapFilesEndpoints(this WebApplication app)
{
var group = app.MapGroup("api/files");
@@ -31,12 +29,12 @@ public sealed class FilesEndpoints : ICarterModule
using var ms = new MemoryStream();
if (result.Stream.CanSeek) result.Stream.Position = 0;
await result.Stream.CopyToAsync(ms, ct);
cachedData = (ms.ToArray(), result.ContentType, result.FileName);
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(30));
cache.Set(cacheKey, cachedData, cacheEntryOptions);
result.Stream.Dispose();
}
@@ -47,7 +45,7 @@ public sealed class FilesEndpoints : ICarterModule
{
return Results.File(outStream, cachedData.ContentType, fileDownloadName: cachedData.FileName, enableRangeProcessing: true);
}
return Results.File(outStream, cachedData.ContentType, enableRangeProcessing: true);
}
catch (Exception ex)
@@ -64,12 +62,13 @@ public sealed class FilesEndpoints : ICarterModule
// Формируем подписанный запрос к удаленному серверу
var targetUrl = $"{domain.TrimEnd('/')}/api/federation/v1/proxy/{id}";
var request = new HttpRequestMessage(HttpMethod.Get, targetUrl);
request.Headers.Add("X-Knot-Domain", settings.System.DomainUrl);
// Генерируем подпись запроса своим приватным ключом
using(var rsa = RSA.Create()) {
using (var rsa = RSA.Create())
{
rsa.ImportPkcs8PrivateKey(Convert.FromBase64String(settings.Federation.PrivateKey!), out _);
var dataToSign = Encoding.UTF8.GetBytes(id + settings.System.DomainUrl);
var signature = Convert.ToBase64String(rsa.SignData(dataToSign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
@@ -81,7 +80,7 @@ public sealed class FilesEndpoints : ICarterModule
var remoteStream = await response.Content.ReadAsStreamAsync(ct);
var contentType = response.Content.Headers.ContentType?.ToString() ?? "application/octet-stream";
return Results.Stream(remoteStream, contentType, enableRangeProcessing: true);
});
}