Работа на новой архитектуре

This commit is contained in:
Халимов Рустам
2026-03-20 01:12:00 +03:00
parent a48a0b0977
commit 473e9bcef6
8 changed files with 105 additions and 80 deletions

View File

@@ -53,6 +53,8 @@ public sealed class MessagesController : ControllerBase
}
[HttpPost("upload")]
[DisableRequestSizeLimit]
[RequestFormLimits(MultipartBodyLengthLimit = 10L * 1024 * 1024 * 1024)] // 10 GB limit for form body
public async Task<IActionResult> UploadFile(IFormFile file, CancellationToken ct)
{
if (file == null || file.Length == 0)

View File

@@ -174,11 +174,11 @@ using (var scope = app.Services.CreateScope())
}
// Настройка конвейера запросов
app.UseCors();
app.UseMiddleware<Knot.Shared.Infrastructure.Middleware.ExceptionHandlingMiddleware>();
app.UseMiddleware<Knot.Shared.Infrastructure.Middleware.AdminAuthMiddleware>();
app.UseCors();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();

View File

@@ -10,7 +10,7 @@ public class MediaMessage : Message
public string? Caption { get => Content; private set => Content = value; }
public MediaType MediaType { get; private set; } // image, video, file, voice
private readonly List<Media> _media = new();
private List<Media> _media = new();
public override IReadOnlyCollection<Media> Media => _media.AsReadOnly();
private MediaMessage() : base()

View File

@@ -36,13 +36,13 @@ public abstract class Message : AggregateRoot<Guid>
public bool IsDeletedForUser(Guid userId) => _deletedFor.Exists(d => d.UserId == userId);
// ================== Связанные коллекции (общего назначения) ==================
protected readonly List<ReadReceipt> _readBy = new();
protected List<ReadReceipt> _readBy = new();
public IReadOnlyCollection<ReadReceipt> ReadBy => _readBy.AsReadOnly();
protected readonly List<DeletedMessage> _deletedFor = new();
protected List<DeletedMessage> _deletedFor = new();
public IReadOnlyCollection<DeletedMessage> DeletedFor => _deletedFor.AsReadOnly();
protected readonly List<Reaction> _reactions = new();
protected List<Reaction> _reactions = new();
public IReadOnlyCollection<Reaction> Reactions => _reactions.AsReadOnly();
// ================== Инфраструктурный конструктор EF ==================

View File

@@ -74,30 +74,30 @@ public sealed class MessageSentDomainEventHandler : INotificationHandler<Message
await _hubContext.Clients.Group(notification.ChatId.ToString())
.SendAsync("new_message", new
{
message.Id,
message.ChatId,
message.SenderId,
Content = message.Content,
Type = message.Type,
message.CreatedAt,
message.ForwardedFromId,
ForwardedFrom = forwardedFromObj,
message.ReplyToId,
ReplyTo = replyToObj,
Quote = message.Quote,
Media = message.Media.Select(m => new
id = message.Id,
chatId = message.ChatId,
senderId = message.SenderId,
content = message.Content,
type = message.Type,
createdAt = message.CreatedAt,
forwardedFromId = message.ForwardedFromId,
forwardedFrom = forwardedFromObj,
replyToId = message.ReplyToId,
replyTo = replyToObj,
quote = message.Quote,
media = message.Media.Select(m => new
{
m.Id,
m.Type,
m.Url,
Filename = m.Filename,
Size = m.Size
id = m.Id,
type = m.Type,
url = m.Url,
filename = m.Filename,
size = m.Size
}).ToList(),
Sender = senderObj,
ReadBy = new List<object>(),
StoryId = message.StoryId,
StoryMediaUrl = message.StoryMediaUrl,
StoryMediaType = message.StoryMediaType
sender = senderObj,
readBy = new List<object>(),
storyId = message.StoryId,
storyMediaUrl = message.StoryMediaUrl,
storyMediaType = message.StoryMediaType
}, cancellationToken);
}
}

View File

@@ -114,7 +114,9 @@ public sealed class MessageRepository : IMessageRepository
if (!msg.ReadBy.Any(r => r.UserId == userId))
{
var receipt = new ReadReceipt(msg.Id, userId);
var pushUpdate = Builders<Message>.Update.Push("ReadBy", receipt);
var updateModel = new UpdateOneModel<Message>(Builders<Message>.Filter.Eq(m => m.Id, msg.Id), pushUpdate);
writes.Add(updateModel);
}

View File

@@ -45,65 +45,69 @@ public class S3FileStorageService : IFileStorageService
{
await EnsureBucketExistsAsync();
// 1. Вычисляем SHA-256 для Content-Addressable Storage (CAS) - дедупликация
string fileHash;
using (var sha256 = SHA256.Create())
using (var msHash = new MemoryStream())
{
var startPos = fileStream.Position;
await fileStream.CopyToAsync(msHash);
var hashBytes = sha256.ComputeHash(msHash.ToArray());
fileHash = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
// Возвращаем указатель потока в начало для последующего чтения
fileStream.Position = startPos;
}
var ext = Path.GetExtension(fileName);
var fileId = $"{fileHash}{ext}";
// 2. Шифруем файл "на лету" во временный файл
// Для больших файлов мы используем временный файл, чтобы не перегружать оперативную память (RAM)
var tempFilePath = Path.GetTempFileName();
var tempUnencryptedPath = Path.GetTempFileName();
var tempEncryptedPath = Path.GetTempFileName();
byte[] ivParams;
try
{
using (var tempFs = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write))
using (var cryptoStream = _encryptionService.CreateEncryptionStream(tempFs, out ivParams))
// 1. Сохраняем входной поток (который может быть не seekable из-за HTTP/Kestrel)
// во временный файл
using (var unencryptedFs = new FileStream(tempUnencryptedPath, FileMode.Create, FileAccess.Write))
{
await fileStream.CopyToAsync(cryptoStream);
await fileStream.CopyToAsync(unencryptedFs);
}
// 3. Загружаем зашифрованный файл в MinIO
using var fileToUpload = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read);
var metadata = new System.Collections.Generic.Dictionary<string, string>
// 2. Вычисляем SHA-256 для дедупликации, читая из локального временного файла
string fileHash;
using (var unencryptedFs = new FileStream(tempUnencryptedPath, FileMode.Open, FileAccess.Read))
using (var sha256 = SHA256.Create())
{
{ "ContentType", contentType },
{ "OriginalFileName", fileName },
{ "IV", Convert.ToBase64String(ivParams) },
{ "KeyVersion", "1" },
{ "EncryptionAlgorithm", "AES-256-CBC" } // Как реализовано в потоке AES CBC
};
var hashBytes = await sha256.ComputeHashAsync(unencryptedFs);
fileHash = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
}
var putObjectArgs = new PutObjectArgs()
.WithBucket(_bucketName)
.WithObject(fileId)
.WithStreamData(fileToUpload)
.WithObjectSize(fileToUpload.Length)
.WithContentType("application/octet-stream")
.WithHeaders(metadata);
var ext = Path.GetExtension(fileName);
if (string.IsNullOrEmpty(ext)) ext = ""; // For files without extension
var fileId = $"{fileHash}{ext}";
await _minioClient.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
// 3. Шифруем файл "на лету" во второй временный файл
using (var unencryptedFs = new FileStream(tempUnencryptedPath, FileMode.Open, FileAccess.Read))
using (var encryptedFs = new FileStream(tempEncryptedPath, FileMode.Create, FileAccess.Write))
using (var cryptoStream = _encryptionService.CreateEncryptionStream(encryptedFs, out ivParams))
{
await unencryptedFs.CopyToAsync(cryptoStream);
}
// 4. Загружаем зашифрованный файл в MinIO
using (var fileToUpload = new FileStream(tempEncryptedPath, FileMode.Open, FileAccess.Read))
{
var metadata = new System.Collections.Generic.Dictionary<string, string>
{
{ "ContentType", contentType ?? "application/octet-stream" },
{ "OriginalFileName", Uri.EscapeDataString(fileName ?? "unknown") },
{ "IV", Convert.ToBase64String(ivParams) },
{ "KeyVersion", "1" },
{ "EncryptionAlgorithm", "AES-256-CBC" }
};
var putObjectArgs = new PutObjectArgs()
.WithBucket(_bucketName)
.WithObject(fileId)
.WithStreamData(fileToUpload)
.WithObjectSize(fileToUpload.Length)
.WithContentType("application/octet-stream")
.WithHeaders(metadata);
await _minioClient.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
}
return fileId;
}
finally
{
if (File.Exists(tempFilePath))
{
File.Delete(tempFilePath);
}
if (File.Exists(tempUnencryptedPath)) File.Delete(tempUnencryptedPath);
if (File.Exists(tempEncryptedPath)) File.Delete(tempEncryptedPath);
}
}
@@ -118,19 +122,36 @@ public class S3FileStorageService : IFileStorageService
var statArgs = new StatObjectArgs().WithBucket(_bucketName).WithObject(fileId);
var stat = await _minioClient.StatObjectAsync(statArgs).ConfigureAwait(false);
if (stat.MetaData.ContainsKey("Contenttype"))
var metaData = new Dictionary<string, string>(stat.MetaData, StringComparer.OrdinalIgnoreCase);
// Function to reliably get metadata by key with or without x-amz-meta-
string? GetMeta(string key)
{
contentType = stat.MetaData["Contenttype"];
if (metaData.TryGetValue(key, out var val)) return val;
if (metaData.TryGetValue($"X-Amz-Meta-{key}", out var val2)) return val2;
return null;
}
if (stat.MetaData.ContainsKey("Originalfilename"))
if (GetMeta("Contenttype") is string ct)
{
fileName = stat.MetaData["Originalfilename"];
contentType = ct;
}
if (stat.MetaData.ContainsKey("Iv"))
if (GetMeta("Originalfilename") is string ofn)
{
ivBase64 = stat.MetaData["Iv"];
try
{
fileName = Uri.UnescapeDataString(ofn);
}
catch
{
fileName = ofn; // fallback to unescaped if it was somehow valid
}
}
if (GetMeta("Iv") is string ivStr)
{
ivBase64 = ivStr;
}
// Загружаем во временный файл (так как CryptoStream требует правильного чтения/записи)

View File

@@ -570,7 +570,7 @@ function MessageBubble({
key={m.id}
src={m.url}
alt=""
className={`w-full h-full object-cover cursor-pointer hover:brightness-90 transition-all ${galleryMedia.length > 1 ? 'aspect-square' : isSingleGif ? 'max-h-[260px]' : 'max-h-[500px]'}`}
className={`w-full object-cover cursor-pointer hover:brightness-90 transition-all ${galleryMedia.length > 1 ? 'aspect-square h-full' : isSingleGif ? 'h-auto max-h-[260px]' : 'h-auto max-h-[500px]'}`}
onClick={() => setLightboxData({ index: idx })}
/>
)
@@ -791,7 +791,7 @@ function MessageBubble({
)}
{/* Реакции */}
{Object.keys(reactionGroups).length > 0 && (
<div className={`flex flex-wrap gap-1 mt-1.5 ${isMine ? 'justify-end' : 'justify-start'}`}>
<div className="flex flex-wrap gap-1 mt-1.5 justify-start">
{Object.entries(reactionGroups).map(([emoji, data]) => (
<button
key={emoji}