public async Task <IActionResult> UploadPhoto(int userId, [FromForm] PhotoForUploadDTO photoForUploadDTO) { // Check ID On Route To ID on Token if (userId != Int32.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _datingRepository.GetUser(userId); Account credsAcc = new Account(_cloudinarySettings.Value.CloudName, _cloudinarySettings.Value.ApiKey, _cloudinarySettings.Value.ApiSecret); var filetoUpload = photoForUploadDTO.File; var cloudinaryClient = new Cloudinary(credsAcc); var cloudinaryResponse = new ImageUploadResult(); if (filetoUpload.Length > 0) { using (var photoStream = filetoUpload.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(filetoUpload.Name, photoStream), Transformation = new Transformation().Width(500).Height(500). Crop("fill").Gravity("face") }; cloudinaryResponse = cloudinaryClient.Upload(uploadParams); // Stores Response From Cloudinary photoForUploadDTO.Url = cloudinaryResponse.Url.ToString(); photoForUploadDTO.PublicId = cloudinaryResponse.PublicId; } var photoObj = _mapper.Map <Photos>(photoForUploadDTO); // Source - Destination if (userFromRepo.Photos.Any(p => p.Description == "DF_DELETE_IS_OK")) // IF That User Has Any Photo With That Description { var photo = userFromRepo.Photos.FirstOrDefault(p => p.Description == "DF_DELETE_IS_OK"); var photoFromRepo = await _datingRepository.GetPhoto(photo.Id); _datingRepository.Delete(photoFromRepo); photoObj.IsMain = true; } userFromRepo.Photos.Add(photoObj); if (await _datingRepository.SaveAll()) { var phototoReturn = _mapper.Map <PhotoToReturnDTO>(photoObj); // Doing It Here Will Ensure The Id return(CreatedAtRoute("GetPhoto", new { id = photoObj.Id }, phototoReturn)); // It Return The Endpoint To Get The Photo } return(BadRequest("No se pudo cargar foto/s")); } return(BadRequest("No hay foto")); }
public async Task <IActionResult> AddArtistPhoto(int artistId, [FromForm] PhotoForUploadDTO photoForUploadDTO) { var artist = await appRepository.GetArtist(artistId); var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (artist == null) { artist = await appRepository.GetArtist(artistId, userId); } if (artist == null) { return(BadRequest("Artist does not exist")); } if (photoForUploadDTO.File.Length > 10 * 1024 * 1024) { return(BadRequest("Max file size should be 10 MB.")); } using (var stream = photoForUploadDTO.File.OpenReadStream()) { if (!IsImage(stream)) { return(BadRequest("The file " + photoForUploadDTO.File.FileName + " is not an image!")); } } var photo = await UploadPhoto(photoForUploadDTO.File); if (photo == null) { return(BadRequest("Unable to upload photo")); } var artistPhoto = new ArtistPhoto(); if (artist.IsApproved) { artistPhoto.IsArtistApproved = true; } artistPhoto.Artist = artist; artistPhoto.ArtistId = artist.Id; artistPhoto.Photo = photo; artistPhoto.PhotoId = photo.Id; appRepository.Add(artistPhoto); if (await appRepository.SaveAll()) { return(CreatedAtRoute("GetArtistPhoto", new{ artistId = artist.Id, photoId = photo.Id }, artistPhoto)); } return(BadRequest("Unable to add photo")); }