UplodFile

This commit is contained in:
pns-hassanzadeh
2025-09-07 22:47:57 +03:30
parent ef7722cce4
commit 2b3e4d3263
4 changed files with 55 additions and 4 deletions
@@ -11,4 +11,5 @@ public interface IRepositoryBase<T, TId>
Task DeleteById(TId id, CancellationToken cancellationToken = default);
Task<T?> GetById(TId id, CancellationToken cancellationToken = default);
Task<bool> Exists(Expression<Func<T,bool>> predicate, CancellationToken cancellationToken = default);
Task<T?> Get(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default);
}
@@ -52,6 +52,9 @@ namespace VizhTalk.BuildingBlocks.Infrastructure
public async Task<bool> Exists(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)
=> await _dbContext.Set<T>().Where(predicate).AnyAsync(cancellationToken);
public async Task<T?> Get(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)
=> await _dbContext.Set<T>().Where(predicate).FirstOrDefaultAsync(predicate, cancellationToken);
public async Task<T?> GetById(TId id, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
@@ -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<UploadFileCommand, UploadFileDTO>
{
public Task<UploadFileDTO> Handle(UploadFileCommand request, CancellationToken cancellationToken)
public async Task<UploadFileDTO> 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();
}
}
}
@@ -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<MediaId>
{
private Media(ContentType contentType, int size , string hash , string path)
{
ContentType = contentType;
Size = size;
Hash = hash;
Path = path;
}
private List<MediaGroupMedia> _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<MediaGroupMedia> MediaGroupMedias => _mediaGroupMedia;
public static Media Create(ContentType contentType, int size, string hash, string path)
=> new (contentType, size, hash, path);
}