Refactor media attachments to use MediaFile entity

This commit introduces the MediaFile entity and updates the media attachments model, repositories, and related logic to reference MediaFile instead of storing file metadata directly in MediaAttachments. Controller, service, and SignalR request/response contracts are updated to support the new structure. Database configurations and validators are also adjusted accordingly. This refactor improves media management and normalization in the data model.
This commit is contained in:
Artemy
2025-07-06 21:09:12 +07:00
parent 42348c863f
commit 66819a015a
23 changed files with 166 additions and 93 deletions
+9 -9
View File
@@ -1,4 +1,5 @@
using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Medias;
using Govor.Contracts.Requests;
using Govor.Core.Models;
using Govor.Core.Repositories.MediasAttachments;
@@ -13,12 +14,12 @@ namespace Govor.API.Controllers;
public class MediaController : Controller
{
private readonly ILogger<MediaController> _logger;
private readonly IStorageService _storageService;
public MediaController(ILogger<MediaController> logger, IStorageService storageService)
private readonly IMediaService _mediaService;
public MediaController(ILogger<MediaController> logger, IMediaService mediaService)
{
_logger = logger;
_storageService = storageService;
_mediaService = mediaService;
}
[HttpPost("upload")]
@@ -27,11 +28,10 @@ public class MediaController : Controller
{
try
{
var url = await _storageService.SaveAsync(request.Data,request.FileName);
var mediaId = Guid.NewGuid();
return Ok(mediaId);
var result = await _mediaService.UploadMediaAsync(new Media(request.Data, request.FileName, request.Type,
request.MimeType, request.EncryptedKey));
return Ok(result);
}
catch (Exception ex)
{
+13 -13
View File
@@ -92,14 +92,14 @@ public class ChatsHub : Hub
RecipientType: request.RecipientType,
SendAt: DateTime.UtcNow,
Media: request.MediaAttachments?.Select(f => new SendMedia(
f.MediaId, f.EncryptedKey, f.Type, f.MimeType)) ?? Array.Empty<SendMedia>()
f.MediaId, f.EncryptedKey)) ?? Array.Empty<SendMedia>()
);
try
{
var result = await _messageService.SendMessageAsync(sendMessageParams);
if (!result.IsSuccess || result.MessageId == Guid.Empty)
if (!result.IsSuccess || result.Message.Id == Guid.Empty)
{
_logger.LogError(result.Exception, "Failed to send message from {SenderId} to {RecipientId}. Error: {ErrorMessage}", senderId, request.RecipientId, result.Exception?.Message ?? "Unknown error");
if (result.Exception != null) throw result.Exception;
@@ -108,14 +108,15 @@ public class ChatsHub : Hub
var messageResponse = new UserMessageResponse // Assuming a response DTO
{
MessageId = result.MessageId,
SenderId = senderId,
RecipientId = request.RecipientId,
RecipientType = request.RecipientType,
EncryptedContent = request.EncryptedContent,
SentAt = sendMessageParams.SendAt,
MessageId = result.Message.Id,
SenderId = result.Message.SenderId,
RecipientId = result.Message.RecipientId,
RecipientType = result.Message.RecipientType,
EncryptedContent = result.Message.EncryptedContent,
SentAt = result.Message.SentAt,
IsEdited = false,
MediaAttachments = request.MediaAttachments,
MediaAttachments = result.Message.MediaAttachments
.Select(m => m.MediaFile).ToList(),
ReplyToMessageId = request.ReplyToMessageId
};
@@ -134,7 +135,7 @@ public class ChatsHub : Hub
// Notify sender (confirmation) on their connection
await Clients.Caller.SendAsync("MessageSent", messageResponse); // Or use "ReceiveMessage" if the sender should also just get it like anyone else
_logger.LogInformation("Message {MessageId} sent successfully from {SenderId} to {RecipientId} ({RecipientType})", result.MessageId, senderId, request.RecipientId, request.RecipientType);
_logger.LogInformation("Message {MessageId} sent successfully from {SenderId} to {RecipientId} ({RecipientType})", result.Message.Id, senderId, request.RecipientId, request.RecipientType);
}
catch (Exception ex)
{
@@ -145,18 +146,17 @@ public class ChatsHub : Hub
}
}
public async Task Remove(Guid recipientId, Guid messageId)
public async Task Remove(RemoveMessageRequest request)
{
}
public async Task Edit(string newMessage, Guid messageId)
public async Task Edit(EditMessageRequest request)
{
}
private Guid GetUserId(bool suppressException = false)
{
var userIdClaim = Context.User?.FindFirst("userId")?.Value;