public async Task <IActionResult> UploadImage()
        {
            if (Request.Form.Files.Count < 1)
            {
                return(BadRequest("Request contains no file to upload"));
            }

            if (Request.Form.Files.Count > 1)
            {
                return(BadRequest("More than one file to upload at the same time is not supported"));
            }

            var firstFile = Request.Form.Files.First();

            using (var stream = firstFile.OpenReadStream())
            {
                var originalFileName = firstFile.FileName;
                var extension        = Path.GetExtension(originalFileName);
                var fileName         = string.Concat(DateTime.UtcNow.ToString("yyyyMMddHHmmss"),
                                                     "-",
                                                     Guid.NewGuid().ToString("D"),
                                                     "-",
                                                     originalFileName.Substring(0, originalFileName.Length - extension.Length),
                                                     extension);

                var result = await _imageStorage.SaveImage(stream, fileName);

                return(Json(new
                {
                    ImageUrl = result.Url
                }));
            }
        }
        public ActionResult AddProfileImage(FormCollection frmc, HttpPostedFileBase profileImage)
        {
            //getting directory to save images from the web.config file
            string imagesDirectory       = WebConfigurationManager.AppSettings["PublishersImagesDirectory"];
            string imagesDirectoryRooted = Server.MapPath("~" + imagesDirectory); //to save images the directory has to be passed rooted
            string id = frmc["PublisherId"];

            try
            {
                imageStorage.SaveImage(profileImage, imagesDirectoryRooted, id);
                var publisher = db.Publishers.Find(int.Parse(id));
                publisher.ProfileImageUrl = id + Path.GetExtension(profileImage.FileName);
                db.SaveChanges();
            }
            catch (Exception ex) {
                ModelState.AddModelError("ExceptionError", ex.Message);
                return(View());
            }
            return(RedirectToAction("Edit", "Publishers", new { id = id }));
        }
        public async Task <ActionResult <ImagePostResponse> > Create([FromForm] CreateImagePostRequest imagePostRequest)
        {
            _logger.LogInformation("Received image post create request.");

            // Get the user making the request.
            User user = _currentUserService.GetCurrentUser(HttpContext);

            _logger.LogInformation("Requesting user email: {0}", user.Email);

            IFormFile image = imagePostRequest.Image;

            // Optimize image.
            _logger.LogInformation("Optimizing image.");
            Stream imageStream = _imageOptimizationService.Optimize(image);

            // Store image file.
            _logger.LogInformation("Saving image file with filename '{0}'.", image.FileName);
            string imageUri = await _imageStorage.SaveImage(imageStream, Path.GetExtension(image.FileName));

            _logger.LogInformation("Successfully saved image file at location '{0}'.", imageUri);

            // Save image database record.
            ImagePost newImagePost = new ImagePost()
            {
                UserEmail   = user.Email,
                Description = imagePostRequest.Description,
                ImageUri    = imageUri,
                DateCreated = DateTime.Now,
                Tags        = imagePostRequest.Tags
                              .Select(content => new ImagePostTag()
                {
                    Tag = new Tag()
                    {
                        Content = content
                    }
                }).ToList()
            };

            _logger.LogInformation("Creating image post.");
            newImagePost = await _imagePostService.Create(newImagePost);

            newImagePost.User = user;

            // Map to response object.
            ImagePostResponse imagePostResponse = _mapper.Map <ImagePostResponse>(newImagePost);

            // Publish event.
            _logger.LogInformation("Publishing image post create notification.");
            await _notificationService.Publish(new ImagePostCreatedNotification(imagePostResponse));

            _logger.LogInformation("Successfully created image post with id {0}.", newImagePost.Id);

            return(Created($"/imageposts/{newImagePost.Id}", imagePostResponse));
        }
        public async Task <IActionResult> Upload(IFormFile image)
        {
            if (image != null)
            {
                using (var stream = image.OpenReadStream())
                {
                    var imageId = await _imageStore.SaveImage(stream);

                    return(RedirectToAction("Show", new { imageId }));
                }
            }
            return(View());
        }
Example #5
0
        public async Task <PixelartCreationResultModel> CreateAsync(PixelartCreateModel model)
        {
            var image = _storage.GetImage(model.SourcePath);

            var ids    = model.AvailibleColorsIds;
            var colors = await _repository.GetAsync <Color>(x => ids.Contains(x.Id));

            var options = new PixelizingOptions
            {
                AvailibleColors = colors.Select(x => System.Drawing.Color.FromArgb(x.A, x.R, x.G, x.B)),
                Size            = model.Size * (int)model.Unit
            };

            var result = _pixelartGenerator.CreatePixelart(image, options);

            var path = _storage.SaveImage(result);

            return(new PixelartCreationResultModel {
                ResultPath = path
            });
        }