private Picture UploadPictureToCloudinary(PictureForCreationDto pictureForCreationDto) { var file = pictureForCreationDto.File; ImageUploadResult uploadResult = new ImageUploadResult(); if (file?.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream) }; if (pictureForCreationDto.IsMain) { uploadParams.Transformation = new Transformation().Width(1200).Height(600).Crop("fill").Gravity("face"); } uploadResult = _cloudinary.Upload(uploadParams); } } pictureForCreationDto.Url = uploadResult.Uri.ToString(); pictureForCreationDto.PublicId = uploadResult.PublicId; var picture = _mapper.Map <Picture>(pictureForCreationDto); return(picture); }
public IResult InsertPictureForArticle(PictureForCreationDto pictureForCreationDto) { var article = _articleRepository.GetIncluding(x => x.Id == pictureForCreationDto.ArticleId, x => x.Pictures); var picture = UploadPictureToCloudinary(pictureForCreationDto); _pictureRepository.Insert(picture); return(new SuccessResult(string.Format(Messages.SuccessfulInsert, nameof(Picture)))); }
public async Task <IActionResult> AddPictureForUser(int userId, [FromForm] PictureForCreationDto pictureForCreationDto) { //check if the userId from token matches with the user Id in the root. if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = pictureForCreationDto.File; //use thee variable to store cloudinary data var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation() .Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } pictureForCreationDto.PictureUrl = uploadResult.Uri.ToString(); pictureForCreationDto.PublicId = uploadResult.PublicId; var picture = _mapper.Map <Pictures>(pictureForCreationDto); userFromRepo.Pictures.Add(picture); if (await _repo.SaveAllAsync()) { var pictureToReturn = _mapper.Map <PictureForReturnDto>(picture); return(CreatedAtRoute("GetPicture", new { id = picture.Id }, pictureToReturn )); } return(BadRequest("Couldnt add the photo!")); }