public ActionResult Create(PhotoCreateDto photoCreateDto) { //check if the user has permission var user = HttpContext.User; int userId = int.Parse(user.Claims.FirstOrDefault(c => c.Type == "Id").Value); if (userId != photoCreateDto.UserId) { return(Forbid()); } var photo = _mapper.Map <Photo>(photoCreateDto); try { photo.DiskName = _photoSaver.SavePhoto(Convert.FromBase64String(photoCreateDto.ImgFile)); } catch (Exception) { return(Problem()); } _unitOfWork.Photos.Add(photo); _unitOfWork.SaveChanges(); return(NoContent()); //return CreatedAtRoute(); }
public async Task <bool> Create( PhotoCreateDto model, FileDto file ) { var result = false; try { model.Url = await UploadImage(file); _context.Photos.Add(new Photo { Url = model.Url, UserId = model.UserId }); await _context.SaveChangesAsync(); result = true; } catch (Exception e) { // Error logging } return(result); }
public async Task UploadPhoto(int userId, PhotoCreateDto model) { //var file = model.File; Microsoft.AspNetCore.Http.IFormFile file = model.File; var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/assets/images/", userId.ToString(), file.FileName); using (var db = base.NewDb()) { using (var stream = System.IO.File.Create(path)) { await file.CopyToAsync(stream); } bool isMain = true; if (await HasMainPhoto(userId)) { isMain = false; } var memberPhoto = new MemberPhoto() { UserId = userId, AddedDate = System.DateTime.Now, Descriptions = model.descriptions ?? file.FileName, IsMain = isMain, PhotoUrl = path }; db.Add(memberPhoto); await db.SaveChangesAsync(); return; } }
public async Task <IActionResult> AddPhoto(int userId, [FromForm] PhotoCreateDto photoCreateDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _unitOfWork.userRepository.GetEntityWithInclude(u => u.Photos, u => u.ID == userId); var file = photoCreateDto.File; if (file != null) { var blob = await _unitOfWork.storageService.UploadImage(file); photoCreateDto.Url = blob.Uri.ToString(); photoCreateDto.PublicId = blob.Name.ToString(); var photo = _mapper.Map <Photo>(photoCreateDto); _unitOfWork.photoRepository.AddPhoto(user, photo); if (await _unitOfWork.Commit()) { var photoToReturn = _mapper.Map <PhotoToReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } } return(BadRequest("Failed to add the photo")); }
public async Task <IActionResult> AddPhotoForUser(int userId, PhotoCreateDto photoDto) { var user = await _repo.GetUser(userId); if (user == null) { return(BadRequest("Could not find user")); } int 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.UrlString = uploadResult.Uri.ToString(); photoDto.PublicId = uploadResult.PublicId; Photo photo = _mapper.Map <Photo>(photoDto); photo.User = user; if (!user.Photos.Any(m => m.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); var photoReturn = _mapper.Map <PhotoReturnDto>(photo); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoReturn)); } return(BadRequest("Could not add photo")); }
public ActionResult <PhotoReadDto> CreatePhoto(PhotoCreateDto photoCreateDto) { var photoModel = _mapper.Map <Photo>(photoCreateDto); _repository.CreatePhoto(photoModel); _repository.SaveChanges(); var photoReadDto = _mapper.Map <PhotoReadDto>(photoModel); return(CreatedAtRoute(nameof(GetPhotosByFamilyMember), new { Id = photoReadDto.Id }, photoReadDto)); }
public async Task <IActionResult> AddPhoto(int propertyId, [FromForm] PhotoCreateDto photoCreateDto) { int userId = Convert.ToInt32(_userManager.GetUserId(User)); if (userId != _context.Properties.FindAsync(propertyId).Result.UserId) { return(Unauthorized()); } var propertyFromRepo = await _repo.GetProperty(propertyId); var file = photoCreateDto.PhotoFile; 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); } } photoCreateDto.PhotoUrl = uploadResult.Uri.ToString(); photoCreateDto.PhotoPublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoCreateDto); if (!propertyFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } propertyFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = propertyId }, photoToReturn)); } return(BadRequest("Failed to add photo(s)")); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoCreateDto photoDto) { var test = Request.Form.Files; var test1 = ModelState.Values; if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await this.repo.GetUser(userId); 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() }; uploadResult = this.cloudinary.Upload(uploadParams); } } photoDto.Url = uploadResult.Uri.ToString(); photoDto.PublicId = uploadResult.PublicId; var photo = this.mapper.Map <Photo>(photoDto); if (!user.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); if (await this.repo.SaveAll()) { var photoToReturn = this.mapper.Map <PhotoReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> AddPhotoForUser(int user_id, [FromForm] PhotoCreateDto photoCreateDto) { if (user_id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _dataRepository.GetUser(user_id); var file = photoCreateDto.file; var upload_result = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var file_upload_param = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; upload_result = _cloudinary.Upload(file_upload_param); } } photoCreateDto.url = upload_result.Uri.ToString(); photoCreateDto.public_photo_id = upload_result.PublicId; var photo = _mapper.Map <Photo>(photoCreateDto); if (!userFromRepo.Photos.Any(u => u.is_main)) { photo.is_main = true; } userFromRepo.Photos.Add(photo); if (await _dataRepository.SaveAll()) { var photo_to_return = _mapper.Map <PhotoCreateDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photo_to_return)); } return(BadRequest("Could not add the photo")); }
public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoCreateDto photoCreateDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _datingRepo.GetUser(userId); var uploadResult = new ImageUploadResult(); var file = photoCreateDto.File; 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); } } photoCreateDto.Url = uploadResult.Uri.ToString(); photoCreateDto.PublicId = uploadResult.PublicId.ToString(); var photo = _mapper.Map <Photo>(photoCreateDto); if (!userFromRepo.Photos.Any(a => a.IsActive)) { photo.IsActive = true; } userFromRepo.Photos.Add(photo); if (await _datingRepo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoToReturn)); } else { return(BadRequest("Could not upload a photo")); } }
public async Task <IActionResult> UploadPhotos(int userId, [FromForm] PhotoCreateDto model) { if (userId != int.Parse(User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var file = model.File; if (file == null || file.Length == 0) { return(BadRequest("未選取檔案,無法上傳")); } await _service.UploadPhoto(userId, model); return(Ok()); }
public IActionResult AddPhotoForFood(int foodid, [FromForm] PhotoCreateDto photoCreateDto) { try { var food = _wrapper.Food.GetFoodById(foodid); if (food == null) { return(BadRequest("Couldnt find food")); } //var currentUserId = 6;//int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); //var currentUserKitchenId = _wrapper.Kitchen.GetKitchenByUser(currentUserId).Id; //if (currentUserKitchenId != food.KitchenId) //{ // _logger.LogError($"Kullanıcının bu yemege erisimi yok"); // return Unauthorized(); //} var file = photoCreateDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams { File = new FileDescription(file.Name, stream), Folder = "food" }; uploadResult = _cloudinary.Upload(uploadParams); } } photoCreateDto.Url = uploadResult.Uri.ToString(); photoCreateDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoCreateDto); photo.Food = food; if (!food.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } food.Photos.Add(photo); _wrapper.Save(); var photoToReturn = _mapper.Map <PhotoDto>(photo); _logger.LogInfo($"Fotograf bilgileri veritabanına kaydedildi"); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } catch (Exception ex) { _logger.LogError($"Fotograf yüklenirken bir hata meydana geldi : {ex.Message}"); return(StatusCode(500, "Internal server error")); } }