Exemplo n.º 1
0
        public async Task <IActionResult> UploadPhoto([FromForm] PhotoForUploadDto photoForUploadDto)
        {
            var uploadResult = new ImageUploadResult();

            var file = photoForUploadDto.File;

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new CloudinaryDotNet.FileDescription(file.FileName, stream),
                        Transformation = new CloudinaryDotNet.Transformation().Width(500)
                    };
                    uploadResult = await _cloudinary.UploadAsync(uploadParams);
                }
            }
            if (uploadResult.StatusCode == HttpStatusCode.OK)
            {
                string cloudinaryReturnUri = uploadResult.SecureUrl.ToString();
                return(Ok(new { Uri = cloudinaryReturnUri }));
            }
            return(BadRequest(uploadResult.Error));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> SetUserAvatar(int id, [FromForm] PhotoForUploadDto photoForUploadDto)
        {
            if (IsNotAuthorized(id))
            {
                return(Unauthorized());
            }

            var userFromRepo = await usersRepository.GetUser(id);

            if (userFromRepo.AvatarUrl != null && userFromRepo.AvatarPublicKey != null)
            {
                var deleteParams = new DeletionParams(userFromRepo.AvatarPublicKey);

                cloudinary.Destroy(deleteParams);
            }

            var file = photoForUploadDto.File;

            if (file == null)
            {
                return(BadRequest("File not found."));
            }

            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);
                }
            }

            userFromRepo.AvatarUrl       = uploadResult.Uri.ToString();
            userFromRepo.AvatarPublicKey = uploadResult.PublicId;

            if (await usersRepository.SaveAll())
            {
                var userToReturn = mapper.Map <UserForViewDto>(userFromRepo);
                return(CreatedAtRoute("GetUser", new { id = userFromRepo.Id }, userToReturn));
            }

            return(BadRequest("Error uploading the avatar."));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> UploadPhoto(int userId,
                                                      [FromForm] PhotoForUploadDto photoForUploadDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(userId);

            var file         = photoForUploadDto.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);
                }
            }

            photoForUploadDto.Url      = uploadResult.Uri.ToString();
            photoForUploadDto.PublicId = uploadResult.PublicId;

            var photo = _mapp.Map <Photo>(photoForUploadDto);

            if (!userFromRepo.Photos.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }

            userFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                var returnedPhoto = _mapp.Map <PhotoToReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, returnedPhoto));
            }

            return(BadRequest("Could not add photo "));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AddPhoto(int userId, [FromForm] PhotoForUploadDto photoForUploadDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(userId);

            var file         = photoForUploadDto.File;
            var uploadResult = new ImageUploadResult();    // przechowanie odpowiedzi od cloudinary

            if (file.Length > 0)                           // jezeli cos jest w tym pliku
            {
                using (var stream = file.OpenReadStream()) // aby pokazac co jest w tym pliku po zaladownaiu go
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") // zeby przerobic duze zdjecie na kwadrat
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            // pobranie reeszty danych z cloudinary do dto
            photoForUploadDto.Url      = uploadResult.Url.ToString();
            photoForUploadDto.PublicId = uploadResult.PublicId;
            //mapowanie z tych wartosci do instancji modelu photo z dto

            var photo = _mapper.Map <Photo>(photoForUploadDto);

            userFromRepo.Photo = photo;
            if (await _repo.Save())
            {
                var finalPhoto = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, finalPhoto));
            }

            return(BadRequest("Could not upload the photo"));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForUploadDto photoForUploadDto)
        {
            var unauthorization = this.CheckIfLoggedInUserIsAuthorized(userId,
                                                                       "You are not logged in as the user you are trying to upload the photo for.");

            if (unauthorization != null)
            {
                return(unauthorization);
            }

            var currentUser = await this.UserService.GetUser(userId).ConfigureAwait(false);

            var file         = photoForUploadDto.File;
            var uploadResult = new ImageUploadResult();

            bool doesPhotoExist = file == null;

            if (doesPhotoExist)
            {
                return(this.BadRequest(new StatusCodeResultReturnObject(this.BadRequest(),
                                                                        "The photo to be uploaded was not found.")));
            }
            else
            {
                bool doesPhotoHaveData = file.Length > 0;
                if (doesPhotoHaveData)
                {
                    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(CloudinaryDotNet.Gravity.Face)
                        };

                        uploadResult = this.Cloudinary.Upload(uploadParams);
                    }
                }
            }

            photoForUploadDto.Url      = uploadResult.Uri.ToString();
            photoForUploadDto.PublicId = uploadResult.PublicId;

            var  photo = this.Mapper.Map <Photo>(photoForUploadDto);
            bool AreNoneOfUserPhotosSetToMain = !currentUser.Photos.Any(u => u.IsMain);

            if (AreNoneOfUserPhotosSetToMain)
            {
                photo.IsMain = true;
            }

            currentUser.Photos.Add(photo);
            bool isDatabaseSaveSuccessful = await this.UserService.SaveAll().ConfigureAwait(false);

            if (isDatabaseSaveSuccessful)
            {
                var photoToReturn = this.Mapper.Map <PhotoForReturnDto>(photo);

                return(this.CreatedAtRoute("GetPhoto", new { userId, photo.Id }, photoToReturn));
            }

            return(BadRequest(new StatusCodeResultReturnObject(this.BadRequest(),
                                                               "Photo upload to server failed.")));
        }