Example #1
0
        public async Task <IEnumerable <Image> > AddImage(int listingId, ICollection <IFormFile> files)
        {
            try
            {
                //if (listingId < 100000 || listingId > 1000000) throw new ValidationException(nameof(listingId));
                var listing = await _listingService.GetListingDetail(listingId);

                if (listing == null)
                {
                    _logger.LogInformation("Can't find listing with id: {id}", listingId);
                    throw new ArgumentOutOfRangeException(nameof(listingId), "Can't find listing");
                }

                if (!(await _authorizationService.AuthorizeAsync(User, listing, Operations.Update)).Succeeded)
                {
                    throw new UnauthorizedAccessException();
                }

                var imageUrls = new List <Image>();
                foreach (var file in files)
                {
                    if (file.Length > 0)
                    {
                        // Bodom hack: deal with this later
                        var result = await AwsS3Extensions.SaveToS3Async(
                            AwsS3Extensions.GetS3Client(_appSettings.AwsS3SecretKey, _appSettings.AwsS3AccessKey,
                                                        _appSettings.AwsS3Region),
                            file.OpenReadStream(), "meetthelocal-development", $"images/listings/{listingId}/{file.FileName}");

                        var imageUrl = $"https://s3-ap-southeast-2.amazonaws.com/meetthelocal-development/images/listings/{listingId}/{file.FileName}";
                        await _imageService.InsertListingImage(listingId, imageUrl);

                        imageUrls.Add(new Image {
                            Url = imageUrl
                        });
                    }
                }

                return(imageUrls);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                throw;
            }
        }
Example #2
0
        public async Task <string> UploadHeroImage(int id, IList <IFormFile> files)
        {
            try
            {
                //if (listingId < 100000 || listingId > 1000000) throw new ValidationException(nameof(listingId));
                if (!(await _authorizationService.AuthorizeAsync(User, new User {
                    Id = id
                }, Operations.Update)).Succeeded)
                {
                    throw new UnauthorizedAccessException();
                }

                foreach (var file in files)
                {
                    if (file.Length <= 0)
                    {
                        throw new ArgumentNullException(nameof(files));
                    }

                    // Bodom hack: deal with this later
                    var result = await AwsS3Extensions.SaveToS3Async(
                        AwsS3Extensions.GetS3Client(_appSettings.AwsS3SecretKey, _appSettings.AwsS3AccessKey,
                                                    _appSettings.AwsS3Region),
                        file.OpenReadStream(), "meetthelocal-development", $"images/profiles/{id}/{file.FileName}");

                    var imageUrl     = $"https://s3-ap-southeast-2.amazonaws.com/meetthelocal-development/images/profiles/{id}/{file.FileName}";
                    var uploadResult = await _imageService.InsertHeroImage(id, imageUrl);

                    if (uploadResult == 1)
                    {
                        return(imageUrl);
                    }

                    throw new ArgumentNullException(nameof(files));
                }

                throw new ArgumentNullException(nameof(files));
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                throw;
            }
        }
Example #3
0
        public async Task <int> DeleteImage(int listingId, string url)
        {
            try
            {
                //if (listingId < 100000 || listingId > 1000000) throw new ValidationException(nameof(listingId));
                var listing = await _listingService.GetListingDetail(listingId);

                if (listing == null)
                {
                    _logger.LogInformation("Can't find listing with id: {id}", listingId);
                    throw new ArgumentOutOfRangeException(nameof(listingId), "Can't find listing");
                }


                if (!(await _authorizationService.AuthorizeAsync(User, listing, Operations.Update)).Succeeded)
                {
                    throw new UnauthorizedAccessException();
                }

                var image = await _listingService.FetchImageByUrl(listingId, url);

                if (image == null)
                {
                    _logger.LogInformation("Can't find image with listingId: {listingId}, url: {url}", listingId, url);
                    throw new ArgumentOutOfRangeException(nameof(listingId), "Can't find image");
                }

                var filename = image.Url.Split('/').LastOrDefault();

                // Bodom hack: deal with this later
                var result = await AwsS3Extensions.DeleteObjectS3Async(
                    AwsS3Extensions.GetS3Client(_appSettings.AwsS3SecretKey, _appSettings.AwsS3AccessKey,
                                                _appSettings.AwsS3Region), "meetthelocal-development", $"images/listings/{listingId}/{filename}");



                return(await _listingService.DeleteImage(image.ImageId));
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message, e);
                throw;
            }
        }