public async Task <Response <ItemDto> > Handle(CreateContentCommand request, CancellationToken cancellationToken) { Item item = _mapper.Map <Item>(request); ItemDto result; try { item.Name = request.Name ?? Path.GetFileNameWithoutExtension(request.File.FileName); item.ContentType = request.File.GetContentType(); item.CreatedBy = _httpContext.HttpContext.User.Identity.Name; item.Size = request.File.Length; item.VerifiedHash = await request.File .CalculateMD5FileHashAsync(cancellationToken); item.Url = await _storageFileSystemProvider .StoreAsync(request.File, cancellationToken).ConfigureAwait(false); _logger.LogInformation("storage item {Name} successfully stored in {Url}", item.Name, item.Url); result = _mapper.Map <ItemDto>(await _storageRepository.AddAsync(item)); // Raising new content created Event ... await _mediator.Publish(new ContentCreatedEvent(DateTime.Now, $"{item.CreatedBy}, {_httpContext.HttpContext.Connection.RemoteIpAddress}", item.VerifiedHash, item.Url), cancellationToken); } catch (Exception ex) { _logger.LogError("failed to store item {Name}, {ex}", item.Name, ex); throw new ApiException(ex.Message); } return(new Response <ItemDto>(result)); }
public async Task <Response <int> > Handle(UpdateContentCommand command, CancellationToken cancellationToken) { Item item = await _storageRepository.GetByIdAsync(command.Id); if (item == null) { throw new ApiException($"Content Not Found."); } else { item.Name = command.Name ?? Path.GetFileNameWithoutExtension(command.File.FileName); item.ContentType = command.File.GetContentType(); item.LastModifiedBy = _httpContext.HttpContext.User.Identity.Name; item.Description = command.Description; item.Size = command.File.Length; item.VerifiedHash = await command.File .CalculateMD5FileHashAsync(cancellationToken); item.Url = await _storageFileSystemProvider .StoreAsync(command.File, cancellationToken, overwrite : true).ConfigureAwait(false); if (!string.IsNullOrWhiteSpace(item.Url)) { await _storageRepository.UpdateAsync(item); } _logger.LogInformation("storage item {Name} successfully updated in {Url}", item.Name, item.Url); return(new Response <int> { Data = item.Id, Succeeded = true, Message = $"storage item {item.Name} successfully updated." }); } }