fix: align backend APIs and upload flow
This commit is contained in:
@@ -16,22 +16,33 @@ namespace FileService.Application.EventHandler
|
||||
private readonly IObjectStorageRouter router;
|
||||
private readonly IUploadFileReposity uploadFile;
|
||||
private readonly IUnitOfWork uwork;
|
||||
private readonly IStorageRedisCache storageCache;
|
||||
private readonly IUploadTaskReposity uploadTask;
|
||||
|
||||
public UploadTaskCompleteEventHandler(IObjectStorageRouter router, IUploadFileReposity uploadFile, IUnitOfWork uwork, IStorageRedisCache storageCache)
|
||||
public UploadTaskCompleteEventHandler(IObjectStorageRouter router, IUploadFileReposity uploadFile, IUploadTaskReposity uploadTask, IUnitOfWork uwork)
|
||||
{
|
||||
this.router = router;
|
||||
this.uploadFile = uploadFile;
|
||||
this.uwork = uwork;
|
||||
this.storageCache = storageCache;
|
||||
this.uploadTask = uploadTask;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<UploadTaskCompleteEvent> context)
|
||||
{
|
||||
var @event = context.Message;
|
||||
var existingFile = await uploadFile.FindBySourceTaskIdAsync(@event.TaskId);
|
||||
if (existingFile != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var task = await uploadTask.FindByIdAsync(@event.TaskId);
|
||||
if (task is null)
|
||||
{
|
||||
throw new InvalidOperationException($"Upload task {@event.TaskId} has not been committed yet.");
|
||||
}
|
||||
|
||||
var storage = router.Route(@event.ProviderCode);
|
||||
var taskCache = await storageCache.GetAsync(@event.SessionId);
|
||||
if(@event.ProviderCode == "Local")
|
||||
try
|
||||
{
|
||||
await storage.CompleteUploadAsync(new StorageContracts.CompleteUploadCommand(
|
||||
ProviderCode: @event.ProviderCode,
|
||||
@@ -46,14 +57,27 @@ namespace FileService.Application.EventHandler
|
||||
Checksum: s.Checksum
|
||||
)).ToList()
|
||||
), context.CancellationToken);
|
||||
uploadFile.Create(new Domain.Entities.UploadFile(
|
||||
var file = new Domain.Entities.UploadFile(
|
||||
ownerId: @event.OperatorId,
|
||||
fileName: @event.FileName,
|
||||
fileSize: taskCache.FileSize,
|
||||
fileSize: @event.FileSize,
|
||||
contentType: @event.ContentType,
|
||||
new Domain.ValueObjects.StorageLocation(taskCache.ProviderCode, taskCache.Bucket, taskCache.ObjectKey, taskCache.Region),
|
||||
checkSum: new Domain.ValueObjects.CheckSum("md5", @event.CheckSun)
|
||||
));
|
||||
new Domain.ValueObjects.StorageLocation(@event.ProviderCode, @event.Bucket, @event.ObjectKey, @event.Region),
|
||||
checkSum: new Domain.ValueObjects.CheckSum("md5", @event.CheckSun),
|
||||
sourceTaskId: @event.TaskId,
|
||||
chatType: @event.ChatType,
|
||||
targetId: @event.TargetId,
|
||||
isPublic: false
|
||||
);
|
||||
uploadFile.Create(file);
|
||||
task.CompleteUpload(file.Id);
|
||||
await uwork.SaveChangesAsync(context.CancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
task.Fail(ex.Message);
|
||||
await uwork.SaveChangesAsync(context.CancellationToken);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user