This commit is contained in:
2026-05-09 21:17:56 +08:00
6 changed files with 160 additions and 28 deletions
@@ -0,0 +1,45 @@
using FileService.Application.Ports;
using IM.Commons.IntegrationEvents;
using MassTransit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileService.Application.EventHandler
{
public class UploadTaskCompleteEventHandler : IConsumer<UploadTaskCompleteEvent>
{
private readonly IObjectStorageRouter router;
private readonly IStorageRedisCache cache;
public UploadTaskCompleteEventHandler(IObjectStorageRouter router, IStorageRedisCache cache)
{
this.router = router;
this.cache = cache;
}
public async Task Consume(ConsumeContext<UploadTaskCompleteEvent> context)
{
var @event = context.Message;
var storage = router.Route(@event.ProviderCode);
if(@event.ProviderCode == "Local")
{
await storage.CompleteUploadAsync(new StorageContracts.CompleteUploadCommand(
ProviderCode: @event.ProviderCode,
Bucket: @event.Bucket,
Region: @event.Region,
ObjectKey: @event.ObjectKey,
UploadSessionId: @event.SessionId,
Parts: @event.Parts.Select(s => new StorageContracts.UploadPart(
PartNumber: s.PartNumber,
ETag: s.ETag,
Size: s.Size,
Checksum: s.Checksum
)).ToList()
), context.CancellationToken);
}
}
}
}
@@ -4,7 +4,9 @@ using FileService.Application.StorageContracts;
using FileService.Domain.Entities;
using FileService.Domain.IReposities;
using IM.Commons;
using IM.Commons.IntegrationEvents;
using IM.InitCommon;
using MassTransit;
using MassTransit.Internals;
using Microsoft.Extensions.Options;
using System;
@@ -17,13 +19,16 @@ namespace FileService.Application.UploadFileTask
{
public class UploadFileTaskService(IUploadTaskReposity reposity,
IMapper mapper, IObjectStorageRouter router,
IOptions<StorageOptions> options, IStorageRedisCache redis)
IOptions<StorageOptions> options, IStorageRedisCache redis,
IPublishEndpoint endpoint
)
{
private readonly IUploadTaskReposity reposity = reposity;
private readonly IMapper mapper = mapper;
private readonly IObjectStorageRouter router = router;
private readonly IOptions<StorageOptions> options = options;
private readonly IStorageRedisCache redis = redis;
private readonly IPublishEndpoint endpoint = endpoint;
private readonly IObjectStoragePort storage = router.Route(options.Value.DefaultProviderCode);
public async Task<Result<TaskInitResponse>> InitTaskAsync(UploadTaskInitCommand command)
@@ -34,9 +39,9 @@ namespace FileService.Application.UploadFileTask
var storageOption = options.Value.Providers[options.Value.DefaultProviderCode];
var storage = router.Route(storageOption.ProviderCode);
var initRes = await storage.InitUploadAsync(new StorageContracts.InitiateUploadCommand(
ProviderCode: options.Value.ProviderCode,
Bucket: options.Value.Bucket,
ObjectKey: options.Value.Endpoint,
ProviderCode: storageOption.ProviderCode,
Bucket: storageOption.Bucket,
ObjectKey: storageOption.Endpoint,
ContentType:task.ContentType.Value,
ContentLength: command.FileSize,
null
@@ -95,20 +100,35 @@ namespace FileService.Application.UploadFileTask
}
var task = await reposity.FindByIdAsync(Guid.Parse(taskCache.TaskId));
var res = await storage.CompleteUploadAsync(new CompleteUploadCommand(
ProviderCode: taskCache.ProviderCode,
Bucket: taskCache.Bucket,
Region: taskCache.Region,
ObjectKey: taskCache.ObjectKey,
UploadSessionId: taskCache.UploadSessionId,
Parts: command.Parts
), cancellationToken);
//var res = await storage.CompleteUploadAsync(new CompleteUploadCommand(
// ProviderCode: taskCache.ProviderCode,
// Bucket: taskCache.Bucket,
// Region: taskCache.Region,
// ObjectKey: taskCache.ObjectKey,
// UploadSessionId: taskCache.UploadSessionId,
// Parts: command.Parts
// ), cancellationToken);
task.CompleteUpload(new Domain.ValueObjects.StorageLocation(
taskCache.ProviderCode, taskCache.Bucket,
taskCache.ObjectKey, taskCache.Region
));
await endpoint.Publish(new UploadTaskCompleteEvent()
{
Bucket = taskCache.Bucket,
FileName = task.FileName.ToString(),
ObjectKey = taskCache.ObjectKey,
Parts = command.Parts.Select(s =>
new IM.Commons.IntegrationEvents.UploadPart(
s.PartNumber, s.ETag, s.Size, s.Checksum)
).ToList(),
ProviderCode = taskCache.ProviderCode,
Region = taskCache.Region,
SessionId = command.UploadSessionId,
TaskId = task.Id
});
return Result.Success(mapper.Map<UploadTaskResponse>(task));
}
}