Esempio n. 1
0
        public async Task <IActionResult> AddCategoryImage(int categoryId, int id, string type, [FromForm] ImageForUploadDto imageForUploadDto)
        {
            var file         = imageForUploadDto.File;
            var uploadResult = new ImageUploadResult();
            int imageType    = 0;

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(200).Height(250).Crop("fill")//.Gravity("face")
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            imageForUploadDto.Url      = uploadResult.Uri.ToString();
            imageForUploadDto.PublicId = uploadResult.PublicId;

            //var image =  _mapper.Map<Image>(imageForUploadDto);

            if (type == "subcat")
            {
                imageType = 1;
            }

            var image = new Image
            {
                CategoryId    = categoryId,
                Url           = imageForUploadDto.Url,
                PublicId      = imageForUploadDto.PublicId,
                isMain        = false,
                type          = imageType,
                SubCategoryId = id
            };

            await _repo.AddImage(image);

            return(Ok());
        }