diff --git a/VizhTalk.BuildingBlocks.Domain/IRepositoryBase.cs b/VizhTalk.BuildingBlocks.Domain/IRepositoryBase.cs index 7771d3d..9da2e2e 100644 --- a/VizhTalk.BuildingBlocks.Domain/IRepositoryBase.cs +++ b/VizhTalk.BuildingBlocks.Domain/IRepositoryBase.cs @@ -11,4 +11,5 @@ public interface IRepositoryBase Task DeleteById(TId id, CancellationToken cancellationToken = default); Task GetById(TId id, CancellationToken cancellationToken = default); Task Exists(Expression> predicate, CancellationToken cancellationToken = default); + Task Get(Expression> predicate, CancellationToken cancellationToken = default); } diff --git a/VizhTalk.BuildingBlocks.Infrastructure/RepositoryBase.cs b/VizhTalk.BuildingBlocks.Infrastructure/RepositoryBase.cs index 44071af..eeb434d 100644 --- a/VizhTalk.BuildingBlocks.Infrastructure/RepositoryBase.cs +++ b/VizhTalk.BuildingBlocks.Infrastructure/RepositoryBase.cs @@ -52,6 +52,9 @@ namespace VizhTalk.BuildingBlocks.Infrastructure public async Task Exists(Expression> predicate, CancellationToken cancellationToken = default) => await _dbContext.Set().Where(predicate).AnyAsync(cancellationToken); + public async Task Get(Expression> predicate, CancellationToken cancellationToken = default) + => await _dbContext.Set().Where(predicate).FirstOrDefaultAsync(predicate, cancellationToken); + public async Task GetById(TId id, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/Vizhtalk.CDN.Presentation/Application/CommandHandlers/MediaCommandHandler.cs b/Vizhtalk.CDN.Presentation/Application/CommandHandlers/MediaCommandHandler.cs index 9f29737..375d0e2 100644 --- a/Vizhtalk.CDN.Presentation/Application/CommandHandlers/MediaCommandHandler.cs +++ b/Vizhtalk.CDN.Presentation/Application/CommandHandlers/MediaCommandHandler.cs @@ -1,9 +1,13 @@ -using Grpc.Core; +using System.Security.Cryptography; +using Grpc.Core; using Mapster; +using Microsoft.AspNetCore.StaticFiles; using Vizhtalk.CDN.Presentation.Application.Exceptions; using Vizhtalk.CDN.Presentation.Domain.Entities; +using Vizhtalk.CDN.Presentation.Domain.ValueObjects; using VizhTalk.BuildingBlocks.Application.Configuration.Commnad; using VizhTalk.Grpc.CDN; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace Vizhtalk.CDN.Presentation.Application.CommandHandlers; @@ -27,8 +31,35 @@ public class CreateMediaGroupCommandHandler(IUnitOfWork unitOfWork, CDNGrpc.CDNG public class UploadFileCommandHandler(IUnitOfWork unitOfWork, CDNGrpc.CDNGrpcClient cdnClient) : ICommandHandler { - public Task Handle(UploadFileCommand request, CancellationToken cancellationToken) + public async Task Handle(UploadFileCommand request, CancellationToken cancellationToken) { + + var fileBytes = request.File.ToByteArray(); + + var hash = ToSHA512Hash(fileBytes); + + var media = await unitOfWork.Media.Get(a => a.Hash == hash); + + if (media is null) + { + + using MemoryStream stream = new MemoryStream(fileBytes); + + } + throw new NotImplementedException(); } + + public string ToSHA512Hash(byte[] data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + using (SHA512 sha512 = SHA512.Create()) + { + byte[] hashBytes = sha512.ComputeHash(data); + return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant(); + } + } + } \ No newline at end of file diff --git a/Vizhtalk.CDN.Presentation/Domain/Entities/Media.cs b/Vizhtalk.CDN.Presentation/Domain/Entities/Media.cs index 1f23e1a..5037b24 100644 --- a/Vizhtalk.CDN.Presentation/Domain/Entities/Media.cs +++ b/Vizhtalk.CDN.Presentation/Domain/Entities/Media.cs @@ -1,15 +1,31 @@ -using VizhTalk.BuildingBlocks.Domain; +using System.Text; using Vizhtalk.CDN.Presentation.Domain.TypeIds; using Vizhtalk.CDN.Presentation.Domain.ValueObjects; +using VizhTalk.BuildingBlocks.Domain; namespace Vizhtalk.CDN.Presentation.Domain.Entities; public class Media : Entity { + + private Media(ContentType contentType, int size , string hash , string path) + { + + ContentType = contentType; + Size = size; + Hash = hash; + Path = path; + + } + private List _mediaGroupMedia = new(); public ContentType ContentType { get; private set; } public int Size { get; set; } public string Path { get; private set; } public string Hash { get; set; } public IReadOnlyList MediaGroupMedias => _mediaGroupMedia; -} + + + public static Media Create(ContentType contentType, int size, string hash, string path) + => new (contentType, size, hash, path); +} \ No newline at end of file