[FA-misc] Mass transit overhaul, needs testing and review

This commit is contained in:
gamer147
2026-01-21 23:16:31 -05:00
parent 055ef33666
commit f88f340d0a
97 changed files with 1150 additions and 858 deletions

View File

@@ -1,10 +0,0 @@
using FictionArchive.Service.Shared.Services.EventBus;
namespace FictionArchive.Service.FileService.Models.IntegrationEvents;
public class FileUploadRequestCreatedEvent : IIntegrationEvent
{
public Guid RequestId { get; set; }
public string FilePath { get; set; }
public byte[] FileData { get; set; }
}

View File

@@ -1,22 +0,0 @@
using FictionArchive.Common.Enums;
using FictionArchive.Service.Shared.Services.EventBus;
namespace FictionArchive.Service.FileService.Models.IntegrationEvents;
public class FileUploadRequestStatusUpdateEvent : IIntegrationEvent
{
public Guid RequestId { get; set; }
public RequestStatus Status { get; set; }
#region Success
public string? FileAccessUrl { get; set; }
#endregion
#region Failure
public string? ErrorMessage { get; set; }
#endregion
}

View File

@@ -2,10 +2,9 @@ using Amazon.Runtime;
using Amazon.S3;
using FictionArchive.Common.Extensions;
using FictionArchive.Service.FileService.Models;
using FictionArchive.Service.FileService.Models.IntegrationEvents;
using FictionArchive.Service.FileService.Services.EventHandlers;
using FictionArchive.Service.Shared.Extensions;
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
using FictionArchive.Service.Shared.MassTransit;
using Microsoft.Extensions.Options;
namespace FictionArchive.Service.FileService;
@@ -24,14 +23,15 @@ public class Program
builder.Services.AddHealthChecks();
#region Event Bus
#region MassTransit
builder.Services.AddFictionArchiveMassTransit(
builder.Configuration,
x =>
{
x.AddConsumer<UploadFileCommandConsumer>();
});
builder.Services.AddRabbitMQ(opt =>
{
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
})
.Subscribe<FileUploadRequestCreatedEvent, FileUploadRequestCreatedEventHandler>();
#endregion
// Add authentication with cookie support

View File

@@ -2,57 +2,62 @@ using Amazon.S3;
using Amazon.S3.Model;
using FictionArchive.Common.Enums;
using FictionArchive.Service.FileService.Models;
using FictionArchive.Service.FileService.Models.IntegrationEvents;
using FictionArchive.Service.Shared.Services.EventBus;
using FictionArchive.Service.Shared.MassTransit.Contracts.Commands;
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using MassTransit;
using Microsoft.Extensions.Options;
namespace FictionArchive.Service.FileService.Services.EventHandlers;
public class FileUploadRequestCreatedEventHandler : IIntegrationEventHandler<FileUploadRequestCreatedEvent>
public class UploadFileCommandConsumer : IConsumer<UploadFileCommand>
{
private readonly ILogger<FileUploadRequestCreatedEventHandler> _logger;
private readonly ILogger<UploadFileCommandConsumer> _logger;
private readonly AmazonS3Client _amazonS3Client;
private readonly IEventBus _eventBus;
private readonly S3Configuration _s3Configuration;
private readonly ProxyConfiguration _proxyConfiguration;
public FileUploadRequestCreatedEventHandler(ILogger<FileUploadRequestCreatedEventHandler> logger, AmazonS3Client amazonS3Client, IEventBus eventBus, IOptions<S3Configuration> s3Configuration, IOptions<ProxyConfiguration> proxyConfiguration)
public UploadFileCommandConsumer(
ILogger<UploadFileCommandConsumer> logger,
AmazonS3Client amazonS3Client,
IOptions<S3Configuration> s3Configuration,
IOptions<ProxyConfiguration> proxyConfiguration)
{
_logger = logger;
_amazonS3Client = amazonS3Client;
_eventBus = eventBus;
_proxyConfiguration = proxyConfiguration.Value;
_s3Configuration = s3Configuration.Value;
}
public async Task Handle(FileUploadRequestCreatedEvent @event)
public async Task Consume(ConsumeContext<UploadFileCommand> context)
{
var command = context.Message;
var putObjectRequest = new PutObjectRequest();
putObjectRequest.BucketName = _s3Configuration.Bucket;
putObjectRequest.Key = @event.FilePath;
putObjectRequest.Key = command.FilePath;
putObjectRequest.UseChunkEncoding = false; // Needed to avoid an error with Garage
using MemoryStream memoryStream = new MemoryStream(@event.FileData);
using MemoryStream memoryStream = new MemoryStream(command.FileData);
putObjectRequest.InputStream = memoryStream;
var s3Response = await _amazonS3Client.PutObjectAsync(putObjectRequest);
if (s3Response.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
_logger.LogError("An error occurred while uploading file to S3. Response code: {responsecode}", s3Response.HttpStatusCode);
await _eventBus.Publish(new FileUploadRequestStatusUpdateEvent()
await context.Publish(new FileUploadCompletedEvent
{
RequestId = @event.RequestId,
RequestId = command.RequestId,
Status = RequestStatus.Failed,
ErrorMessage = "An error occurred while uploading file to S3."
});
return;
}
await _eventBus.Publish(new FileUploadRequestStatusUpdateEvent()
await context.Publish(new FileUploadCompletedEvent
{
Status = RequestStatus.Success,
RequestId = @event.RequestId,
FileAccessUrl = _proxyConfiguration.BaseUrl + "/" + @event.FilePath
RequestId = command.RequestId,
FileAccessUrl = _proxyConfiguration.BaseUrl + "/" + command.FilePath
});
}
}
}

View File

@@ -9,8 +9,10 @@
"BaseUrl": "https://localhost:7247/api"
},
"RabbitMQ": {
"ConnectionString": "amqp://localhost",
"ClientIdentifier": "FileService"
"Host": "localhost",
"VirtualHost": "/",
"Username": "guest",
"Password": "guest"
},
"S3": {
"Url": "https://s3.orfl.xyz",