예제 #1
0
        public async Task <ActionResult <Image> > PostImage([FromForm] ImageCreateDto imageDto)
        {
            if (imageDto.File == null)
            {
                return(BadRequest("File is empty"));
            }

            var image = new Image
            {
                ImageName = imageDto.File.FileName,
                Title     = imageDto.Title,
                Type      = imageDto.File.ContentType
            };

            byte[] imageData = null;

            using (var binaryReader = new BinaryReader(imageDto.File.OpenReadStream()))
            {
                imageData = binaryReader.ReadBytes((int)imageDto.File.Length);
            };

            image.Data = imageData;

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

            return(CreatedAtAction("GetImage", new { id = image.Id }, image));
        }
예제 #2
0
        public async Task <ActionResult <ImageDto> > PostImage(int articleId, ImageCreateDto imageCreateDto)
        {
            var article = await _context.Articles.FindAsync(articleId);

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

            var image = new Image {
                ArticleId = articleId
            };

            _mapper.Map(imageCreateDto, image);

            _context.Images.Add(image);

            if (image.IsMain)
            {
                var mainImage = _context.Images.FirstOrDefault(i => i.ArticleId == articleId && i.IsMain && i.Id != image.Id);

                if (mainImage != null)
                {
                    mainImage.IsMain = false;
                }
            }

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(CreatedAtAction("GetImage", new { articleId = image.ArticleId, id = image.Id }, _mapper.Map <ImageDto>(image)));
        }
예제 #3
0
        ImageGetDto IImageBusinessLogic.Create(ImageCreateDto imageCreateDto)
        {
            string imageType = string.Empty;

            byte[] imageBytes = Array.Empty <byte>();
            Base64Extractor.Extract(imageCreateDto.Image, ref imageType, ref imageBytes);

            string imageUrl = $"{imageCreateDto.Prefix}/{Guid.NewGuid().ToString()}";

            fileRepository.Create(imageUrl, imageBytes);

            ImageInfo imageInfo = new ImageInfo()
            {
                FilePath  = imageUrl,
                ImageType = imageType
            };

            imageInfoRepository.Create(imageInfo);

            return(new ImageGetDto()
            {
                Id = imageInfo.Id,
                Url = $"api/v1/images/{imageInfo.Id}/image"
            });
        }
예제 #4
0
        public IActionResult Create([FromBody] ImageCreateDto imageCreateDto)
        {
            ImageGetDto imageGetDto = imageBusinessLogic.Create(imageCreateDto);

            if (imageGetDto == null)
            {
                return(BadRequest());
            }
            return(CreatedAtAction(nameof(Get), new { id = imageGetDto.Id }, imageGetDto));
        }
        public ActionResult <ImageReadDto> CreateImage(ImageCreateDto imageCreateDto)
        {
            var image = _mapper.Map <Image>(imageCreateDto);

            image.AddDate = DateTime.Now;
            _imageRepository.CreateImage(image);
            _imageRepository.SaveChanges();

            var imageReadDto = _mapper.Map <ImageReadDto>(image);

            return(CreatedAtRoute(nameof(GetImageId), new { Id = imageReadDto.ImageId }, imageReadDto));
        }
        ImageGetDto IImageService.Create(ImageCreateDto imageCreateDto)
        {
            HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "/api/v1/images")
            {
                Content = new StringContent(System.Text.Json.JsonSerializer.Serialize(imageCreateDto), Encoding.UTF8, "application/json")
            };
            HttpResponseMessage responseMessage = httpClient.Send(httpRequestMessage);

            if (!responseMessage.IsSuccessStatusCode)
            {
                return(null);
            }
            using var reader = new StreamReader(responseMessage.Content.ReadAsStream());
            ImageGetDto imageGetDto = JsonConvert.DeserializeObject <ImageGetDto>(reader.ReadToEnd());

            return(imageGetDto);
        }
        public async Task <Guid> AddImage(ImageCreateDto imageCreateDto)
        {
            var image = new ImageDto
            {
                BinaryImage   = imageCreateDto.BinaryImage,
                PostId        = imageCreateDto.PostId,
                LikesCount    = 0,
                DislikesCount = 0
            };

            using (var uow = UnitOfWorkProvider.Create())
            {
                var id = imageService.Create(image);
                await uow.Commit();

                return(id);
            }
        }
        private async Task <bool> CreateImageDtosFromFiles(List <HttpPostedFileBase> files, PostDto post)
        {
            foreach (var file in files)
            {
                var image = new ImageCreateDto
                {
                    BinaryImage = createByteImage(file),
                    PostId      = post.Id
                };
                var imageId = await PostFacade.AddImage(image);

                if (imageId == Guid.Empty)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #9
0
        public async Task <IActionResult> Add([FromForm] ImageCreateDto imageCreateDto)
        {
            var           resimler = Path.Combine(_environment.WebRootPath, "productImage");//dizin bilgisi
            List <string> images   = new List <string>();

            foreach (var item in imageCreateDto.ProductImages)
            {
                string imageName = $"{Guid.NewGuid().ToString()}.jpg";//Db ye kaydedilecek olan resimlerin ismi
                using (var fileStream = new FileStream(Path.Combine(resimler, imageName), FileMode.Create))
                {
                    images.Add(imageName);
                    await item.CopyToAsync(fileStream);
                }
            }
            var saveImage = await _productImageService.Add(images, imageCreateDto.ProductId);

            if (saveImage.Success)
            {
                return(Ok(saveImage.Message));
            }
            return(BadRequest());
        }