Exemplo n.º 1
0
        public async Task <IActionResult> UpdateProfileImage(IFormFile file)
        {
            try
            {
                var userInformation = await _manager.FindByEmailAsync(User.FindFirst(ClaimTypes.Email).Value);

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

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

                    if (file.Length > _photoAppSettings.MaxBytes)
                    {
                        return(BadRequest("Maximum file size exceeded"));
                    }

                    if (!_photoAppSettings.IsSupported(file.FileName))
                    {
                        return(BadRequest("Invalid file type"));
                    }

                    var awsServiceclientSettings = new AwsServiceClientSettings(file,
                                                                                _awsAppSettings.BucketName, _awsAppSettings.SubFolderProfile, _awsAppSettings.BucketLocation, _awsAppSettings.PublicDomain);

                    var photoUrl = await _awsServiceClient.UploadAsync(awsServiceclientSettings);

                    userInformation.ProfilePictureFileName = photoUrl;

                    if (!string.IsNullOrWhiteSpace(photoUrl))
                    {
                        var result = await _manager.UpdateAsync(userInformation);

                        if (result.Succeeded)
                        {
                            return(Ok(userInformation));
                        }
                        var sb = new StringBuilder();
                        foreach (var error in result.Errors)
                        {
                            sb.Append(error);
                            sb.Append("\n");
                        }
                        return(BadRequest(sb.ToString()));
                    }
                }
                return(NotFound("Please select an Image for update"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.ToString()));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Upload(int serviceId, List <IFormFile> files)
        {
            try
            {
                var servicePhoto = await _serviceRepository.GetServiceAsync(serviceId);

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

                if (files != null && files.Count == 0)
                {
                    return(BadRequest("Null file"));
                }

                var photos = await _repository.GetPhotos(serviceId);

                var numberOfphotos = 0;
                foreach (var ph in photos)
                {
                    numberOfphotos++;
                }

                if (numberOfphotos >= 8)
                {
                    return(BadRequest("Number of allowed photos limited to 8."));
                }

                var results = new List <ServicePhotoResource>();

                if (files != null)
                {
                    foreach (var file in files)
                    {
                        if (file.Length == 0)
                        {
                            return(BadRequest("Empty file"));
                        }

                        if (file.Length > _photoAppSettings.MaxBytes)
                        {
                            return(BadRequest("Maximum file size exceeded"));
                        }

                        if (!_photoAppSettings.IsSupported(file.FileName))
                        {
                            return(BadRequest("Invalid file type"));
                        }

                        var awsServiceSettings = new AwsServiceClientSettings(file, _awsAppSettings.BucketName,
                                                                              _awsAppSettings.SubFolderService, _awsAppSettings.BucketLocation,
                                                                              _awsAppSettings.PublicDomain);

                        var photoUrl = await _awsServiceClient.UploadAsync(awsServiceSettings);

                        var photo = new ServicePhoto {
                            Url = photoUrl
                        };
                        servicePhoto.Photos.Add(photo);

                        await _unitOfWork.CompleteAsync();

                        results.Add(_mapper.Map <ServicePhoto, ServicePhotoResource>(photo));
                    }
                }

                if (results.Count > 0)
                {
                    return(Ok(results));
                }
                else
                {
                    return(BadRequest("Unable to upload"));
                }
            }
            catch (Exception ex)
            {
                var message = $"Message: {ex.Message}\n Source:{ex.Source} \n Expection:{ex.InnerException}";
                return(BadRequest(message));
            }
        }