public async Task <IActionResult> AddPhotoForUser(string userId, [FromForm] PhotoForCreationViewModel photoModel)
        {
            if (userId != User.FindFirst(ClaimTypes.NameIdentifier).Value)
            {
                return(Unauthorized());
            }


            var currentUser = await _userRepository.GetUserAsync(userId);

            if (currentUser.Photo != null && currentUser.Photo.Id != 1)
            {
                await DeletePhotoAsync(userId, currentUser.Photo.Id);
            }

            var photoDto = _mapper.Map <PhotoDto>(photoModel);

            var file = photoModel.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 result = await _photoReposity.AddPhotoAsync(userId, photoDto);

            if (result == null)
            {
                return(BadRequest("ERR_ADDING_PHOTO"));
            }

            var photoReturn = _mapper.Map <PhotoViewModel>(result);

            return(CreatedAtRoute("GetPhoto", new { photoId = photoReturn.Id, userId = userId }, photoReturn));
        }
        public async Task <IActionResult> Upload(IFormFile file)
        {
            var photos = photoRepository.GetPhotos();

            if (file == null)
            {
                return(BadRequest("No upload image file"));
            }

            if (file.Length == 0)
            {
                return(BadRequest("Empty file"));
            }

            if (file.Length > 10 * 1024 * 1024)
            {
                return(BadRequest("Max file size exceeded"));
            }

            var fileName = Path.GetFileNameWithoutExtension(file.FileName);

            if (photos != null)
            {
                // Check  repeated filename
                foreach (var p in photos)
                {
                    if (p.FileName.Contains(fileName))
                    {
                        return(BadRequest("Your photo name has repeated"));
                    }
                }
            }

            await photoRepository.AddPhotoAsync(file, host);

            await unitOfWork.CompleteAsync();


            var photo = photoRepository.GetPhoto(fileName);

            if (photo == null)
            {
                return(NotFound());
            }

            return(Ok(photo));
        }
Ejemplo n.º 3
0
 public async Task<ActionResult<DTOs.Photo>> PostAsync([FromBody] DTOs.Photo photo)
 {
     try
     {
         if (photo == null)
             return BadRequest($"Photo object from body is null.");
         // Create photo
         var (userId, userRole) = GetCurrentUserInfo();
         var photoModel = _mapper.Map<Models.Photo>(photo);
         photoModel.UserId = userId;
         var createdPhoto = await _photoRepository.AddPhotoAsync(photoModel);
         // Get created photo
         var photoDTO = _mapper.Map<DTOs.Photo>(await _photoRepository.GetPhotoAsync(createdPhoto.Id));
         return CreatedAtRoute("GetPhoto", new { id = photoDTO.Id }, photoDTO);
     }
     catch (Exception)
     {
         var msg = "Error occurred while creating new photo into database.";
         _logger.LogError(msg);
         return StatusCode(StatusCodes.Status500InternalServerError, msg);
     }
 }
Ejemplo n.º 4
0
 public async Task <ImageUploadResult> AddPhotoAsync(IFormFile formFile)
 {
     return(await _photorepository.AddPhotoAsync(formFile));
 }