public async Task <String> Update(Poster poster)
        {
            if (!_dbUpdater.CanUpdate(poster))
            {
                throw new Exception($"Can't update concert with ID: {poster.eventId}");
            }

            var image = _reader.GetImage(poster);

            _log.LogInformation($"Image compressed from {poster.imageUrl}");

            var guid      = _guidProvider.NewGuid();
            var imageName = $"{guid}.jpg";

            _log.LogInformation("Image name: {@imageName}", imageName);

            var imageUrl = "testImage";

            try
            {
                imageUrl = await _storage.PutObjectAsync(image, imageName);
            }
            catch (Exception e)
            {
                _log.LogError("Exception when updating poster: {@e}", e);
            }

            _log.LogInformation("Image uploaded to Yandex Object Storage");

            _log.LogInformation("Image URL: {@imageUrl}", imageUrl);
            _dbUpdater.Update(poster, imageUrl);
            return(imageUrl);
        }
        public byte[] GetImage(Poster poster)
        {
            var imageUrl = poster.imageUrl;

            using (var webClient = new WebClient())
            {
                using (var inputStream = webClient.OpenRead(imageUrl))
                {
                    using (MagickImage outputImage = new MagickImage(inputStream))
                    {
                        outputImage.Format = MagickFormat.Jpeg;
                        MagickGeometry size = new MagickGeometry(1000, 1000);
                        outputImage.Resize(size);

                        var            byteArray        = outputImage.ToByteArray();
                        var            compressedStream = new MemoryStream(byteArray);
                        ImageOptimizer optimizer        = new ImageOptimizer();
                        optimizer.Compress(compressedStream);

                        using (MagickImage compressedImage = new MagickImage(compressedStream))
                        {
                            return(compressedImage.ToByteArray());
                        }
                    }
                }
            }
        }
Example #3
0
        public void Update(Poster poster, String imageUri)
        {
            var concert = _db.Events.FirstOrDefault(e => e.Id == poster.eventId);

            if (concert == null)
            {
                throw new Exception($"No concert found by ID: {poster.eventId}");
            }
            concert.PosterUrl = imageUri;
            _db.Events.Update(concert);
            _db.SaveChanges();
            _log.LogInformation("Event updated: {@concert}", concert);
        }
        public async Task <String> Update(Poster poster)
        {
            if (!_dbUpdater.CanUpdate(poster))
            {
                throw new Exception($"Can't update concert with ID: {poster.eventId}");
            }

            var image = _reader.GetImage(poster);

            _log.LogInformation($"Image compressed from {poster.imageUrl}");

            var guid      = _guidProvider.NewGuid();
            var imageName = $"{guid}.jpg";

            _log.LogInformation("Image name: {@imageName}", imageName);

            var imageUrl = "testImage";

            try
            {
                var answer = await _storage.PutObjectAsync(image, imageName);

                if (answer.IsSuccessStatusCode)
                {
                    imageUrl = answer.Result;
                }
                else
                {
                    throw new BadHttpRequestException("Answer from Yandex was: {0}", (int)answer.StatusCode, new Exception(answer.Error));
                }
            }
            catch (Exception e)
            {
                _log.LogError("Exception when updating poster: {@e}", e);
            }

            _log.LogInformation("Image uploaded to Yandex Object Storage");

            _log.LogInformation("Image URL: {@imageUrl}", imageUrl);
            _dbUpdater.Update(poster, imageUrl);
            return(imageUrl);
        }
Example #5
0
        public bool CanUpdate(Poster poster)
        {
            var concert = _db.Events.FirstOrDefault(e => e.Id == poster.eventId);

            return(concert != null);
        }