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 <ActionResult <File> > Add([FromBody] File file) { var body = await Request.Body.GetStringFromStreamAsync(Encoding.UTF8); if (file == null) { logger.LogError("POST/files: couldn't parse body " + body); return(BadRequest(new { error = "Couldn't parse the body to get the files to save" })); } List <string> errors = CheckFilesForErrors(new List <File>() { file }); if (errors != null) { return(BadRequest(errors)); } try { // Every item must be correctly added in the repo if (await storageRepository.AddAsync(file)) { return(Created($"/files/{file.ResourceName}", file)); } else { logger.LogError($"Error in POST/files : files couldn't be added with body {body} and items {Environment.NewLine}{JsonConvert.SerializeObject(file)}"); return(StatusCode(StatusCodes.Status500InternalServerError)); } } catch (Exception e) { logger.LogError($"Error in POST/files : Exception thrown with body {body} and error {e.Message}"); return(StatusCode(StatusCodes.Status500InternalServerError)); } }