Skip to content

Commit dd8f7bc

Browse files
authored
Merge pull request #408 from MangoInstantMessenger/file-upload-optimization
File upload optimization
2 parents 8e637c1 + 934af2b commit dd8f7bc

38 files changed

Lines changed: 3573 additions & 742 deletions

MangoAPI.Application/Services/MailgunSettings.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

MangoAPI.BusinessLogic/Pipelines/CommonFileValidator.cs renamed to MangoAPI.BusinessLogic/ApiCommands/CommonFileValidator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using FluentValidation;
1+
using FluentValidation;
42
using Microsoft.AspNetCore.Http;
3+
using System.Collections.Generic;
4+
using System.Linq;
55

6-
namespace MangoAPI.BusinessLogic.Pipelines;
6+
namespace MangoAPI.BusinessLogic.ApiCommands;
77

88
public class CommonFileValidator : AbstractValidator<IFormFile>
99
{

MangoAPI.BusinessLogic/Pipelines/CommonImageValidator.cs renamed to MangoAPI.BusinessLogic/ApiCommands/CommonImageValidator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using FluentValidation;
1+
using FluentValidation;
42
using Microsoft.AspNetCore.Http;
3+
using System.Collections.Generic;
4+
using System.Linq;
55

6-
namespace MangoAPI.BusinessLogic.Pipelines;
6+
namespace MangoAPI.BusinessLogic.ApiCommands;
77

88
public class CommonImageValidator : AbstractValidator<IFormFile>
99
{

MangoAPI.BusinessLogic/ApiCommands/Documents/UploadDocumentCommand.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

MangoAPI.BusinessLogic/ApiCommands/Documents/UploadDocumentCommandHandler.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.

MangoAPI.BusinessLogic/ApiCommands/Documents/UploadDocumentCommandValidator.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

MangoAPI.BusinessLogic/ApiCommands/Documents/UploadDocumentResponse.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using System;
22
using MangoAPI.BusinessLogic.Responses;
33
using MediatR;
4+
using Microsoft.AspNetCore.Http;
45

56
namespace MangoAPI.BusinessLogic.ApiCommands.Messages;
67

78
public record SendMessageCommand(
89
string MessageText,
910
Guid UserId,
1011
Guid ChatId,
11-
string AttachmentUrl,
1212
string InReplayToAuthor,
1313
string InReplayToText,
1414
DateTime? CreatedAt,
15-
Guid? MessageId)
16-
: IRequest<Result<SendMessageResponse>>;
15+
Guid? MessageId,
16+
IFormFile Attachment)
17+
: IRequest<Result<SendMessageResponse>>;

MangoAPI.BusinessLogic/ApiCommands/Messages/SendMessageCommandHandler.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System;
1+
using FluentValidation;
2+
using System;
23
using System.Linq;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using MangoAPI.Application.Interfaces;
7+
using MangoAPI.Application.Services;
68
using MangoAPI.BusinessLogic.HubConfig;
79
using MangoAPI.BusinessLogic.Models;
810
using MangoAPI.BusinessLogic.Responses;
@@ -22,17 +24,20 @@ public class SendMessageCommandHandler
2224
private readonly IHubContext<ChatHub, IHubClient> hubContext;
2325
private readonly ResponseFactory<SendMessageResponse> responseFactory;
2426
private readonly IBlobServiceSettings blobServiceSettings;
27+
private readonly IBlobService blobService;
2528

2629
public SendMessageCommandHandler(
2730
MangoDbContext dbContext,
2831
IHubContext<ChatHub, IHubClient> hubContext,
2932
ResponseFactory<SendMessageResponse> responseFactory,
30-
IBlobServiceSettings blobServiceSettings)
33+
IBlobServiceSettings blobServiceSettings,
34+
IBlobService blobService)
3135
{
3236
this.dbContext = dbContext;
3337
this.hubContext = hubContext;
3438
this.responseFactory = responseFactory;
3539
this.blobServiceSettings = blobServiceSettings;
40+
this.blobService = blobService;
3641
}
3742

3843
public async Task<Result<SendMessageResponse>> Handle(
@@ -44,8 +49,7 @@ public async Task<Result<SendMessageResponse>> Handle(
4449
{
4550
x.DisplayName, x.DisplayNameColour, x.Image, x.Id,
4651
}).FirstOrDefaultAsync(
47-
x => x.Id == request.UserId,
48-
cancellationToken);
52+
x => x.Id == request.UserId, cancellationToken);
4953

5054
if (user == null)
5155
{
@@ -68,14 +72,16 @@ public async Task<Result<SendMessageResponse>> Handle(
6872
return responseFactory.ConflictResponse(errorMessage, errorDescription);
6973
}
7074

75+
var attachmentUniqueFileName = await UploadAttachmentIfExistsAsync(request);
76+
7177
var messageEntity = new MessageEntity
7278
{
7379
Id = request.MessageId ?? Guid.NewGuid(),
7480
ChatId = request.ChatId,
7581
UserId = request.UserId,
7682
Content = request.MessageText,
7783
CreatedAt = request.CreatedAt ?? DateTime.UtcNow,
78-
Attachment = request.AttachmentUrl,
84+
AttachmentFileName = attachmentUniqueFileName,
7985
InReplayToAuthor = request.InReplayToAuthor,
8086
InReplayToText = request.InReplayToText,
8187
};
@@ -91,15 +97,38 @@ public async Task<Result<SendMessageResponse>> Handle(
9197

9298
await dbContext.SaveChangesAsync(cancellationToken);
9399

100+
var authorPictureUrl = $"{blobServiceSettings.MangoBlobAccess}/{user.Image}";
101+
102+
var attachmentUrl = attachmentUniqueFileName == null
103+
? null
104+
: $"{blobServiceSettings.MangoBlobAccess}/{attachmentUniqueFileName}";
105+
94106
var messageDto = messageEntity.ToMessage(
95107
user.DisplayName,
96108
user.Id,
97-
user.Image,
98-
blobServiceSettings.MangoBlobAccess,
99-
user.DisplayNameColour);
109+
user.DisplayNameColour,
110+
authorPictureUrl,
111+
attachmentUrl);
100112

101113
await hubContext.Clients.Group(request.ChatId.ToString()).BroadcastMessageAsync(messageDto);
102114

103-
return responseFactory.SuccessResponse(SendMessageResponse.FromSuccess(messageEntity.Id));
115+
return responseFactory.SuccessResponse(SendMessageResponse.FromSuccess(messageEntity.Id, attachmentUrl));
116+
}
117+
118+
private async Task<string> UploadAttachmentIfExistsAsync(SendMessageCommand request)
119+
{
120+
if (request.Attachment == null)
121+
{
122+
return null;
123+
}
124+
125+
await new CommonFileValidator().ValidateAndThrowAsync(request.Attachment);
126+
127+
var file = request.Attachment;
128+
var uniqueFileName = FileNameHelper.CreateUniqueFileName(file.FileName);
129+
130+
await blobService.UploadFileBlobAsync(file.OpenReadStream(), file.ContentType, uniqueFileName);
131+
132+
return uniqueFileName;
104133
}
105134
}

MangoAPI.BusinessLogic/ApiCommands/Messages/SendMessageCommandValidator.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ public SendMessageCommandValidator()
1919
.Cascade(CascadeMode.Stop)
2020
.Length(1, 300);
2121

22-
RuleFor(x => x.AttachmentUrl)
23-
.Cascade(CascadeMode.Stop);
24-
2522
RuleFor(x => x.ChatId).NotEmpty();
2623
RuleFor(x => x.UserId).NotEmpty();
2724
}

0 commit comments

Comments
 (0)