public async Task <IActionResult> UpdateImage(int id, [FromForm] IFormCollection formCollection)
        {
            if (formCollection == null || formCollection.Files.Count < 1)
            {
                return(BadRequest());
            }

            var file = await _mediator.Send(new GetFileQuery { Id = id });

            if (file == null)
            {
                return(BadRequest());
            }

            IFormFile formFile = formCollection.Files.First();

            await _imageFileService.DeleteAsync(file.Path);

            // throws exception if formFile is null
            string uploadedImagePath = await _imageFileService.UploadAsync(formFile);

            var result = await Mediator.Send(
                new UpdateFileCommand
            {
                Id       = file.Id,
                Name     = formFile.FileName,
                MimeType = formFile.ContentType,
            });

            return(Ok(result));
        }
示例#2
0
        public async Task <Unit> Handle(DeleteImageFileCommand request, CancellationToken cancellationToken)
        {
            await HandleDeleteFileCommand(request.Id, p => _imageFileService.DeleteAsync(p));

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }