public static UploadPhotoDto UpdateImage(UploadPhotoDto uploadPhotoDto, Photo photo) { Account account = new Account(CloudinarySettings.CloudName, CloudinarySettings.ApiKey, CloudinarySettings.ApiSecret); Cloudinary cloudinary = new Cloudinary(account); var file = uploadPhotoDto.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); } } uploadPhotoDto.Url = uploadResult.Url.ToString(); uploadPhotoDto.PublicId = uploadResult.PublicId; DeleteImage(photo); return(uploadPhotoDto); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] UploadPhotoDto uploadPhotoDto) { var claim = User.FindFirst(ClaimTypes.NameIdentifier); if (userId != int.Parse(claim.Value)) { return(Unauthorized()); } var existingUser = await _repo.GetUser(userId); // Upload photo to cloud storage var file = uploadPhotoDto.File; var uploadResults = 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") }; uploadResults = _cloudinary.Upload(uploadParams); } } // Update photo by the response data from cloud storage uploadPhotoDto.Url = uploadResults.Uri.ToString(); uploadPhotoDto.PublicId = uploadResults.PublicId; // Map uploadPhotoDto into a photo itself based on property that we already have var photo = _mapper.Map <Photo>(uploadPhotoDto); // If the user have no photo, set uploaded photo as main if (!existingUser.Photos.Any(x => x.IsMain)) { photo.IsMain = true; } existingUser.Photos.Add(photo); if (!await _repo.SaveAll()) { return(BadRequest("Could not add the photo")); } var photoToReturn = _mapper.Map <UploadPhotoDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); }
public IActionResult UpdatePhotoForCar(int PhotoId, [FromForm] UploadPhotoDto photoDto) { var result = _photoManager.Update(PhotoId, photoDto); if (result.Success) { return(Ok(result)); } return(BadRequest(result)); }
public IActionResult AddPhotoForCar([FromForm] UploadPhotoDto photoDto) { var result = _photoManager.Add(photoDto); if (result.Success) { return(Ok(result)); } return(BadRequest(result)); }
public async Task <ActionResult <int> > UpdatePhoto(int id, [FromForm] UploadPhotoDto dto) { if (id != dto.EmployeeId) { return(BadRequest()); } var command = mapper.Map <UploadPhotoCommand>(dto); command.PhotoPath = path; await Mediator.Send(command); return(NoContent()); }
public void AddUploadPhotoDto(UploadPhotoDto uploadPhotoDto) { Photo photo = new Photo { CarId = uploadPhotoDto.carId, DateAdded = DateTime.Now, Description = uploadPhotoDto.Description, IsMain = uploadPhotoDto.IsMain, PublicId = uploadPhotoDto.PublicId, Url = uploadPhotoDto.Url }; this.Add(photo); }
public void UpdateUploadPhotoDto(UploadPhotoDto uploadPhotoDto, Photo photo) { Photo newPhoto = new Photo { Id = photo.Id, CarId = uploadPhotoDto.carId, DateAdded = DateTime.Now, Description = uploadPhotoDto.Description, IsMain = uploadPhotoDto.IsMain, PublicId = uploadPhotoDto.PublicId, Url = uploadPhotoDto.Url }; this.Update(newPhoto); }
public static UploadPhotoDto UpdateImage(UploadPhotoDto uploadPhotoDto, Photo photo) { if (uploadPhotoDto.file != null) { string uploadsFolder = Path.GetTempPath(); if (File.Exists(photo.Url)) { DeleteImage(photo); } return(UploadImage(uploadPhotoDto)); } else { return(uploadPhotoDto); } }
public async Task <IActionResult> UploadPhoto(int userId, [FromForm] UploadPhotoDto uploadPhotoDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var file = uploadPhotoDto.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); } } uploadPhotoDto.Url = uploadResult.Url.ToString(); uploadPhotoDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(uploadPhotoDto); 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 { userId = userId, id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
public static UploadPhotoDto UploadImage(UploadPhotoDto uploadPhotoDto) { string filePath = null; if (uploadPhotoDto.file != null) { string uploadsFolder = Path.GetTempPath(); string uniqueFileName = Guid.NewGuid().ToString() + "_" + uploadPhotoDto.file.FileName; filePath = Path.Combine(uploadsFolder, "CarRental", "Images", uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { uploadPhotoDto.file.CopyTo(fileStream); } uploadPhotoDto.Url = filePath; } return(uploadPhotoDto); }
public IResult Add(UploadPhotoDto uploadPhotoDto) { IResult result = BusinessRules.Run( CheckImageCount(uploadPhotoDto.carId) ); if (result != null) { return(result); } //string url = LocalImageProcesses.UploadImage(uploadPhotoDto.file); var uploadResult = CloudImageProcesses.UploadImage(uploadPhotoDto); _photoDal.AddUploadPhotoDto(uploadResult); return(new SuccessResult(Messages.PhotoAdded)); }
public IResult Update(int photoId, UploadPhotoDto uploadPhotoDto) { IResult result = BusinessRules.Run( PhotoExists(photoId), CheckImageCount(uploadPhotoDto.carId) ); if (result != null) { return(result); } var photo = _photoDal.Get(p => p.Id == photoId); var updateResult = CloudImageProcesses.UpdateImage(uploadPhotoDto, photo); // string url = LocalImageProcesses.UpdateImage(uploadPhotoDto,photo); _photoDal.UpdateUploadPhotoDto(updateResult, photo); return(new SuccessResult(Messages.PhotoUpdated)); }