public async Task <IActionResult> AddPhoto(int id, [FromForm] PhotoForCreationDto photoForCreationDto) { if (checkUser(id)) { return(Unauthorized()); } var userFromRepo = await _usersRepository.GetUser(id); var uploadResult = UploadFileToCloudinary(photoForCreationDto.File, userFromRepo.Id); photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); photo.UserId = userFromRepo.Id; photo.IsApproved = false; if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } await _photosRepository.Add <Photo>(photo); if (await _photosRepository.SaveAll()) { return(CreatedAtRoute("GetPhoto", new { id, photoId = photo.Id }, _mapper.Map <PhotoForReturnDto>(photo))); } return(BadRequest("Could not add the photo")); }
public ActionResult AddPhoto(int announcementId, object qqfile) { bool res = false; string fileName; Photo photo = new Photo(); Stream stream = getFileStream(qqfile, out fileName); string message = string.Empty; User authenticatedUser = _userRepository.GetUserByPhone(User.Identity.Name); if (authenticatedUser != null) { Announcement announcement = _announcementRepository.GetById(announcementId); if (announcement != null) { if (announcement.Photos.Count < authenticatedUser.Profile.MaxPhotosPerAnnouncements) { photo.Url = savePhoto(stream, fileName); photo.Owner = authenticatedUser; photo.AnnouncementId = announcementId; _photoRepository.Add(photo); _photoRepository.SaveChanges(); res = true; } if (announcement.Photos.Count < authenticatedUser.Profile.MaxPhotosPerAnnouncements) { message = string.Format(Translation.Translation.PhotosLeft, authenticatedUser.Profile.MaxPhotosPerAnnouncements - announcement.Photos.Count); } else { message = string.Format(Translation.Translation.LimitForPhotosIsReachedLabel, Url.Action("UpdateLimit", "Account")); } } } return(Json(new { Done = res, PhotoId = photo.PhotoId, PhotoUrl = photo.Url, Message = message }, "text/json")); }
public Photo UpdateOrAdd(Photo photo, HttpPostedFileBase postedFile) { if (photo == null) { photo = new Photo(); } byte[] buffer = new byte[postedFile.InputStream.Length]; postedFile.InputStream.Read(buffer, 0, buffer.Length); photo.Data = buffer; photo.FileName = postedFile.FileName; photo.ContentContentType = _contentContentTypesBL.Get(postedFile.ContentType); if (photo.Id != 0) { _photosRepository.Update(photo); } else { photo = _photosRepository.Add(photo); } return(photo); }
public OperationStatus Add(Photo photo, HttpPostedFileBase file) { var status = new OperationStatus(); var path = HostingEnvironment.MapPath(ConfigurationManager.AppSettings["UploadPath"]); try { if (IsFileValidImage(file)) { CreateNewNames(photo, file); if (_handler.ReceivePhoto(file, photo, path)) { _repository.Add(photo); } else { status.ErrorMessage = "Quality of current picture is very low"; return(status); } } else { status.ErrorMessage = "Can’t upload current file like an image"; return(status); } } catch (Exception ex) { status.ErrorMessage = "Current file isn’t an image"; return(status); } status.IsSuccessful = true; return(status); }