Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromForm] ImagePostDto input)
        {
            if (input.File == null)
            {
                return(BadRequest("Missing file"));
            }

            await bus.SendAsync(new AddProductImageCommandModel(input.Id, input.File, input.IsMain));

            return(Accepted());
        }
        public async Task <Image> AddAsync(ImagePostDto entity)
        {
            ImagePostDtoValidator validator = new ImagePostDtoValidator();
            ValidationResult      results   = validator.Validate(entity);

            if (!results.IsValid)
            {
                throw new ValidationException("ImagePostDTO", string.Join(". ", results.Errors));
            }

            return(await _repository.AddAsync(mapper.Map <Image>(entity)));
        }
        public async Task <IActionResult> AddNewImage([FromBody] ImagePostDto imagePostDto)
        {
            var imageResp = await imageService.AddAsync(imagePostDto);

            return(CreatedAtAction("GetClient", new { id = imageResp.ID }, mapper.Map <ImageResponseDto>(imageResp)));
        }
Ejemplo n.º 4
0
        public IActionResult UploadProfileImage([FromForm] ImagePostDto model)
        {
            bool newRecord = true;

            if (model.TeamId == null && model.UserId == null)
            {
                return(BadRequest("Need to specify either User Id or Team Id."));
            }

            if (model.TeamId != null && model.UserId != null)
            {
                return(BadRequest("Do not specify both User Id and Team Id."));
            }

            if (model.Data.ContentType != JPEG && model.Data.ContentType != PNG)
            {
                return(BadRequest("Unsupported file format.  Only jpeg or png are supported."));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            TraImage traImage = new TraImage();

            if (model.UserId != null)
            {
                var image = _transActionRepo.GetUserProfileImage(model.UserId.Value);

                if (image != null)
                {
                    traImage  = image;
                    newRecord = false;
                }
            }

            if (model.TeamId != null)
            {
                var image = _transActionRepo.GetTeamProfileImage(model.TeamId.Value);

                if (image != null)
                {
                    traImage  = image;
                    newRecord = false;
                }
            }

            byte[] bytes = null;
            using (var memoryStream = new MemoryStream())
            {
                model.Data.CopyTo(memoryStream);
                bytes = memoryStream.ToArray();

                using (Image <Rgba32> image = Image.Load(bytes))
                {
                    int maxWidthOrLength = Math.Max(image.Width, image.Height);

                    if (maxWidthOrLength > MAX_SIZE)
                    {
                        double ratio = MAX_SIZE / maxWidthOrLength;
                        image.Mutate(x => x.Resize(Convert.ToInt32(image.Width * ratio), Convert.ToInt32(image.Height * ratio)));
                    }

                    memoryStream.SetLength(0);

                    if (model.Data.ContentType == JPEG)
                    {
                        image.SaveAsJpeg(memoryStream);
                    }
                    else
                    {
                        image.SaveAsPng(memoryStream);
                    }

                    bytes = memoryStream.ToArray();

                    traImage.Width  = image.Width;
                    traImage.Height = image.Height;
                }
            }

            traImage.UserId      = model.UserId;
            traImage.TeamId      = model.TeamId;
            traImage.Data        = bytes;
            traImage.Guid        = Guid.NewGuid().ToString();
            traImage.Filename    = model.Data.FileName;
            traImage.Filesize    = model.Data.Length;
            traImage.ContentType = model.Data.ContentType;

            if (newRecord)
            {
                _transActionRepo.AddProfileImage(traImage);
            }

            if (!_transActionRepo.Save())
            {
                return(StatusCode(500, "Unable to save image to database."));
            }

            return(Ok());
        }