diff --git a/FileService.Application/UploadFile/FileResponse.cs b/FileService.Application/UploadFile/FileResponse.cs index ec4f198..bf8df21 100644 --- a/FileService.Application/UploadFile/FileResponse.cs +++ b/FileService.Application/UploadFile/FileResponse.cs @@ -19,6 +19,6 @@ namespace FileService.Application.UploadFile public StorageLocation StorageLocation { get; set; } public CheckSum CheckSum { get; set; } public DateTimeOffset Created { get; set; } - public DateTimeOffset Updated { get; set; + public DateTimeOffset Updated { get; set; } } } diff --git a/FileService.Application/UploadFileTask/UploadFileTaskService.cs b/FileService.Application/UploadFileTask/UploadFileTaskService.cs index 0f900e1..cba587d 100644 --- a/FileService.Application/UploadFileTask/UploadFileTaskService.cs +++ b/FileService.Application/UploadFileTask/UploadFileTaskService.cs @@ -44,6 +44,8 @@ namespace FileService.Application.UploadFileTask var res = mapper.Map(initRes); res.TaskId = task.Id; + + task.StartUpload(); reposity.Create(task); await redis.SetAsync(new StorageContracts.UploadRuntimeCache( @@ -83,9 +85,31 @@ namespace FileService.Application.UploadFileTask return Result.Success(presignUrl); } - //public async Task> CompleteTaskAsync() - //{ - // var taskCache = await redis.GetAsync() - //} + public async Task> CompleteTaskAsync(UploadTaskCompleteCommand command, CancellationToken cancellationToken = default) + { + var taskCache = await redis.GetAsync(command.UploadSessionId); + + if(taskCache is null) + { + return Result.Fail(ResultCode.CHUNK_NOT_FOUND); + } + + 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); + + task.CompleteUpload(new Domain.ValueObjects.StorageLocation( + taskCache.ProviderCode, taskCache.Bucket, + taskCache.ObjectKey, taskCache.Region + )); + + return Result.Success(mapper.Map(task)); + } } } diff --git a/FileService.Application/UploadFileTask/UploadTaskCompleteCommand.cs b/FileService.Application/UploadFileTask/UploadTaskCompleteCommand.cs index 08ad036..f69bac6 100644 --- a/FileService.Application/UploadFileTask/UploadTaskCompleteCommand.cs +++ b/FileService.Application/UploadFileTask/UploadTaskCompleteCommand.cs @@ -1,4 +1,5 @@ -using System; +using FileService.Application.StorageContracts; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,5 +7,5 @@ using System.Threading.Tasks; namespace FileService.Application.UploadFileTask { - public record UploadTaskCompleteCommand(string UploadSessionId, Guid userId,); + public record UploadTaskCompleteCommand(string UploadSessionId, Guid userId, IReadOnlyList Parts); } diff --git a/FileService.Infrastructure/Storage/LocalStorageAdapter.cs b/FileService.Infrastructure/Storage/LocalStorageAdapter.cs index 1e3386b..d0e7a44 100644 --- a/FileService.Infrastructure/Storage/LocalStorageAdapter.cs +++ b/FileService.Infrastructure/Storage/LocalStorageAdapter.cs @@ -2,7 +2,10 @@ using FileService.Application.StorageContracts; using FileService.Domain.ValueObjects; using IM.Commons; +using IM.InitCommon; using MassTransit; +using MassTransit.Configuration; +using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; @@ -11,14 +14,10 @@ using System.Threading.Tasks; namespace FileService.Infrastructure.Storage { - public class LocalStorageAdapter : IObjectStoragePort + public class LocalStorageAdapter(IRedisService redis, IOptions options) : IObjectStoragePort { - private readonly IRedisService redis; - - public LocalStorageAdapter(IRedisService redis) - { - this.redis = redis; - } + private readonly IRedisService redis = redis; + private readonly IOptions options = options; public string ProviderCode => "Local"; @@ -27,9 +26,14 @@ namespace FileService.Infrastructure.Storage throw new NotImplementedException(); } - public Task GenerateUploadUrlAsync(GenerateUploadUrlCommand command, CancellationToken token) + public async Task GenerateUploadUrlAsync(GenerateUploadUrlCommand command, CancellationToken token) { - throw new NotImplementedException(); + var baseUrl = options.Value.Providers[options.Value.DefaultProviderCode].LocalUploadApiBaseUrl; + return new PresignedUrl( + baseUrl + $"?partNumber={command.PartNumber}", + new Dictionary(), + ExpiresAt: DateTimeOffset.Now.Add(options.Value.Providers[options.Value.DefaultProviderCode].UploadUrlExpiresIn) + ); } public async Task InitUploadAsync(InitiateUploadCommand command, CancellationToken token) diff --git a/FileService.WebApi/Controllers/File/FileController.cs b/FileService.WebApi/Controllers/File/FileController.cs index 216de77..d3fc302 100644 --- a/FileService.WebApi/Controllers/File/FileController.cs +++ b/FileService.WebApi/Controllers/File/FileController.cs @@ -9,7 +9,7 @@ namespace FileService.WebApi.Controllers.File [ApiController] public class FileController : ControllerBase { - [HttpGet] - public async + //[HttpGet] + //public async } }