public async Task <IActionResult> AddPhotoForUser(int userId, PhotosForCreationDto photoDto) { try { var user = await _userService.GetAsync(userId); if (user == null) { return(BadRequest("Could not find user")); } var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != user.Id) { return(Unauthorized()); } var file = photoDto.File; 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); } } photoDto.Url = uploadResult.Uri.ToString(); photoDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoDto); photo.User = user; if (!user.Photos.Any(m => m.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); await _userService.SaveAsync(); var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); // bu maplemeyi servisi kayıt ettikten sonra yapıyoruz. // ki yüklenmiş fotonun id si cloud a o id ile gitsin. Eğer öncesinde yaparsak cloud otomatik uniq id verir. // Ve fotoya geri bir istekte bulunursak o id delki fotoyu cloud da bulamaz. //return Ok(); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } catch (System.Exception) { return(BadRequest("Unexpected error has occured while uploading photos")); } }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotosForCreationDto photoForCreationDto) { // Checking if the user is the loged in user, so he is authorized to add photos if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = photoForCreationDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { // With "using" we will dispose the file once we finished. using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { // Transformation will crop the photo and will focus in the face. File = new FileDescription(file.Name, stream), Transformation = new Transformation() .Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(uploadParams); } } // Filling the properties of our DTO photoForCreationDto.Url = uploadResult.Url.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; // Map the photo using our DTO var photo = _mapper.Map <Photo>(photoForCreationDto); // If there is not main photo, we will asign this as the main if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { // If our save is successful, we will return the photo. var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> AddPhotosForUser([FromRoute] int userId, [FromForm] PhotosForCreationDto photosForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = photosForCreationDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParms = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _Cloudinary.Upload(uploadParms); } } photosForCreationDto.Url = uploadResult.Uri.ToString(); photosForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photosForCreationDto); if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotosFromReturnDto>(photo); return(CreatedAtRoute( "GetPhoto", routeValues: new { id = photo.Id }, value: photoToReturn)); } return(BadRequest("Could not add photo")); }
public async Task <IActionResult> AddPhotosForUser([FromBody] PhotosForCreationDto input) { if (input == null || input.Photos == null || input.Photos.Count == 0) { return(BadRequest("No images sent.")); } if (input.Photos.Count > 5) { return(BadRequest("Max 5 images allowed.")); } var userFromDb = await _userManager.GetUserAsync(this.User); foreach (var photoForCreationDto in input.Photos) { string imagestring = photoForCreationDto.PhotoBase64String.Substring( photoForCreationDto.PhotoBase64String.IndexOf(',') + 1); byte[] bytes = Convert.FromBase64String(imagestring); //System.IO.File.WriteAllBytes(string.Format($"c:\\temp\\{image.PhotoName}"), bytes); var coudinaryUploadResults = new ImageUploadResult(); if (bytes.Length > 0) { using (var stream = new MemoryStream(bytes)) { var uploadParams = new ImageUploadParams() { File = new FileDescription(photoForCreationDto.PhotoName, stream), Folder = "KlofBook/user_" + userFromDb.Id, //Transformation = new Transformation().Width(600).Height(600) // Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; coudinaryUploadResults = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = coudinaryUploadResults.Url.ToString(); photoForCreationDto.PublicId = coudinaryUploadResults.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); photo.IsMain = false; photo.IsApproved = true; photo.UserId = userFromDb.Id; _repository.Photo.CreatePhoto(photo); await _repository.SaveAsync(); } return(Ok()); }
public async Task <IActionResult> AddPhotosForUser(int userId, PhotosForCreationDto photodto) { var user = await _repo.getUser(userId); if (user == null) { return(BadRequest("Couldnot Find User")); } var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != user.id) { return(Unauthorized()); } var file = photodto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream) }; uploadResult = _cloudinary.Upload(uploadParams); } } photodto.Url = uploadResult.Uri.ToString(); photodto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photodto); photo.User = user; if (!user.Photos.Any(m => m.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could Not add the photo")); }
public async Task <IActionResult> AddPhotoForHouse(int houseId, [FromForm] PhotosForCreationDto photoDto) { var houseFromRepo = await _repo.GetHouse(houseId); var file = photoDto.File; photoDto.HouseId = houseId; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream) }; uploadResult = _cloudinary.Upload(uploadParams); }; } photoDto.Url = uploadResult.Uri.ToString(); photoDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoDto); if (!houseFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } houseFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add photo")); }
public async Task <IActionResult> AddPhotoForComponent(int componentId, [FromForm] PhotosForCreationDto photoForCreationDto) { var componentsFromRepo = await _repo.GetComponents(componentId); var file = photoForCreationDto.File; 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(300).Height(300).Crop("fill") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!componentsFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } componentsFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotosForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { componentId = componentId, id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> AddPhotosforUser(int userId, [FromForm] PhotosForCreationDto PhotoCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var UserFromRepo = await _dating.GetUser(userId); var file = PhotoCreationDto.File; var uploadResult = new ImageUploadResult(); if (file.Length >= 0) { using (var stream = file.OpenReadStream()) { var UploadParam = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; uploadResult = _cloudinary.Upload(UploadParam); } PhotoCreationDto.Url = uploadResult.Uri.ToString(); PhotoCreationDto.PublicId = uploadResult.PublicId; var Photo = _autoMapper.Map <Photo>(PhotoCreationDto); if (!UserFromRepo.Photos.Any(q => q.IsMain)) { Photo.IsMain = true; } UserFromRepo.Photos.Add(Photo); if (await _dating.SaveAll()) { var photoToReturn = _autoMapper.Map <PhotoForReturnDto>(Photo); return(CreatedAtRoute("GetPhoto", new { UserId = userId, id = Photo.Id }, photoToReturn)); } } return(BadRequest("could not add the photo")); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotosForCreationDto photosForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = photosForCreationDto.File; //azure blob upload if (file.Length > 0) { photosForCreationDto.Url = UploadPhotoToBlob(file, userId); photosForCreationDto.ThumbnailUrl = photosForCreationDto.Url.Replace("datinguploads", "datingthumbnail"); } //end var photo = _mapper.Map <Photo>(photosForCreationDto); if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could not ad the photo")); }