Esempio n. 1
0
        public async Task <IActionResult> AddImage(AddImageViewModel addImageViewModel)
        {
            var httpClient = _clientFactory.CreateClient("Api");

            var addImageModel = new AddImageModel
            {
                Title    = addImageViewModel.Title,
                FileName = addImageViewModel.Files.First().FileName
            };

            var imageFile = addImageViewModel.Files.First();

            if (imageFile.Length > 0)
            {
                using var fileStream   = imageFile.OpenReadStream();
                using var memoryStream = new MemoryStream();
                fileStream.CopyTo(memoryStream);
                addImageModel.Bytes = memoryStream.ToArray();
            }

            var serializedImage = JsonSerializer.Serialize(addImageModel);

            var request = new HttpRequestMessage(HttpMethod.Post, "/images")
            {
                Content = new StringContent(serializedImage, Encoding.Unicode, MediaTypeNames.Application.Json)
            };

            var response = await httpClient.SendAsync(
                request, HttpCompletionOption.ResponseHeadersRead);

            response.EnsureSuccessStatusCode();

            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public void Add(AddImageModel input, string fileName)
        {
            Image imageDetails = new Image();

            if (input != null)
            {
                imageDetails.ImageId = input.ImageId;
            }
            imageDetails.Title       = input.Title;
            imageDetails.Description = input.Description;
            imageDetails.ImagePath   = fileName;

            _unitOfWork.Context.Set <Image>().Add(imageDetails);
            _unitOfWork.Commit();
        }
Esempio n. 3
0
        public ActionResult Post([FromBody] AddImageModel model)
        {
            _logger.LogInformation("Post image request...");

            string id = Guid.NewGuid().ToString();

            Images.Add(new ImageModel
            {
                Id          = id,
                Description = model.Description,
                ImageBase64 = model.ImageBase64,
                Latitude    = model.Latitude,
                Longitude   = model.Longitude
            });

            return(CreatedAtAction(nameof(Get), new { id = id }, new { id = id }));
        }
Esempio n. 4
0
        public ActionResult AddImage(AddImageModel model, int albumId)
        {
            byte[] array = null;

            if (model.Files.Length > 0)
            {
                foreach (var file in model.Files)
                {
                    if (file != null)
                    {
                        var tagsDto = new List <TagDTO>();

                        using (MemoryStream ms = new MemoryStream())
                        {
                            file.InputStream.CopyTo(ms);
                            array = ms.GetBuffer();
                        }

                        if (!string.IsNullOrWhiteSpace(model.Tags))
                        {
                            foreach (var el in model.Tags.Split(' '))
                            {
                                tagsDto.Add(new TagDTO()
                                {
                                    Name = el.Trim()
                                });
                            }
                        }

                        try
                        {
                            imageService.AddImage(new PictureDTO()
                            {
                                Img = array, Tags = tagsDto, AlbumId = albumId
                            });
                        }
                        catch (TargetNotFoundException)
                        {
                            return(new HttpNotFoundResult());
                        }
                    }
                }
            }

            return(Redirect($"/Images/Index?albumId={albumId}"));
        }
Esempio n. 5
0
        public void AddImage(AddImageModel input)
        {
            var file     = input.ImagePath;
            var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
            //Assigning Unique Filename
            var myUniqueFileName = Convert.ToString(Guid.NewGuid());
            //Getting file Extension
            var FileExtension = Path.GetExtension(fileName);
            // concating  FileName + FileExtension
            var newFileName = myUniqueFileName + FileExtension;

            // Combines two strings into a path.
            fileName = Path.Combine(_environment.ContentRootPath, imageFolder) + $@"\{newFileName}";
            using (FileStream fs = System.IO.File.Create(fileName))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            _repo.Add(input, newFileName);
        }
Esempio n. 6
0
        public async Task <IActionResult> AddImage(AddImageModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            using (BinaryReader reader = new BinaryReader(model.file.OpenReadStream( ))){
                Image image = new Image( )
                {
                    name = model.name,
                    data = reader.ReadBytes(Convert.ToInt32(reader.BaseStream.Length))
                };

                await this.context.images.AddAsync(image);

                await this.context.SaveChangesAsync( );
            }

            return(RedirectToAction(nameof(HomeController.Index)));
        }
Esempio n. 7
0
        public async Task <IActionResult> Post([FromBody] AddImageModel addImageModel)
        {
            //string fileName = $"{Guid.NewGuid()}.jpg";

            var filePath = $@"{_hostEnvironment}\images\{addImageModel.FileName}";

            await System.IO.File.WriteAllBytesAsync(filePath, addImageModel.Bytes);

            var image = new Image
            {
                Id       = Guid.NewGuid(),
                Title    = addImageModel.Title,
                FileName = addImageModel.FileName,
                OwnerId  = string.Empty // To use auth provider to retrieve value
            };

            _context.Images.Add(image);
            await _context.SaveChangesAsync();

            var imageModel = CreateImageModel(image);

            return(CreatedAtAction(nameof(Get), new { id = imageModel.Id }, imageModel));
        }
Esempio n. 8
0
        public IActionResult AddImage([FromForm] AddImageModel input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (input == null)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                _healthservice.AddImage(input);
                return(new JsonResult("Image inserted successfully"));
            }
            catch (Exception ex)
            {
                log.Info(ex);
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
Esempio n. 9
0
        public async Task <IActionResult> AddImage([FromBody] AddImageModel model)
        {
            await _imageService.AddImage(model.Name, model.ImageUrl);

            return(Ok());
        }