Esempio n. 1
0
        public async Task <IActionResult> Create(CreatePictureInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var userId = this.User
                         .FindFirst(ClaimTypes.NameIdentifier)?
                         .Value;

            var wwwrootPath = this.environment
                              .WebRootPath;

            try
            {
                await this.picturesService
                .CreateAsync(input, userId, $"{wwwrootPath}/system_images");
            }
            catch
            {
                return(this.View("Error"));
            }

            return(this.Redirect("/Pictures/All"));
        }
Esempio n. 2
0
        public async Task CreateAsync(CreatePictureInputModel input, string userId, string imagePath)
        {
            // /wwwroot/images/recipes/jhdsi-343g3h453-=g34g.jpg
            Directory.CreateDirectory($"{imagePath}/recipes/");
            foreach (var image in input.Images)
            {
                var extension = Path.GetExtension(image.FileName).TrimStart('.');
                if (!this.allowedExtensions.Any(x => extension.EndsWith(x)))
                {
                    throw new Exception($"Invalid image extension {extension}");
                }

                var dbImage = new CustomerPhoto
                {
                    UserId    = userId,
                    Extension = extension,
                    CigarId   = input.CigarId,
                    PhotoText = input.PhotoText,
                };

                await this.photosEntity.AddAsync(dbImage);

                var physicalPath = $"{imagePath}/CustomerPhotos/{dbImage.Id}.{extension}";
                using Stream fileStream = new FileStream(physicalPath, FileMode.Create);
                await image.CopyToAsync(fileStream);
            }

            await this.photosEntity.SaveChangesAsync();
        }
Esempio n. 3
0
        public async Task CreateAsync(CreatePictureInputModel input, string userId, string imagePath)
        {
            Directory.CreateDirectory($"{imagePath}/Pictures/");

            var extension = Path
                            .GetExtension(input.Picture.FileName)
                            .TrimStart(PictureExtensionDelimiter);

            if (!this.allowedExtensions
                .Any(e => e.ToLower().EndsWith(extension.ToLower())))
            {
                throw new Exception($"Invalid image extension {extension}.");
            }

            var picture = new Picture()
            {
                AddedByUserId = userId,
                CategoryId    = input.CategoryId,
                Name          = input.Name,
                SourceUrl     = input.SourceUrl,
                Extension     = extension,
            };

            await this.picturesRepository.AddAsync(picture);

            await this.picturesRepository.SaveChangesAsync();

            var physicalPath = $"{imagePath}/Pictures/{picture.Id}.{extension}";

            await using var fileStream = new FileStream(physicalPath, FileMode.Create);
            await input.Picture.CopyToAsync(fileStream);
        }
Esempio n. 4
0
        public IActionResult Create()
        {
            var viewModel = new CreatePictureInputModel()
            {
                Categories = this.picturesCategoriesService.GetAll(),
            };

            return(this.View(viewModel));
        }
Esempio n. 5
0
        public async Task <IActionResult> Create(CreatePictureInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var userId = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

            await this.picturesService.CreateAsync(input, userId);

            return(this.Redirect("/Pictures/All"));
        }
Esempio n. 6
0
        public async Task CreateAsync(CreatePictureInputModel input, string userId)
        {
            var picture = new Picture()
            {
                AddedByUserId = userId,
                CategoryId    = input.CategoryId,
                Name          = input.Name,
                Url           = input.Url,
                SourceUrl     = input.SourceUrl,
            };

            await this.picturesRepository.AddAsync(picture);

            await this.picturesRepository.SaveChangesAsync();
        }
Esempio n. 7
0
        public async Task <IActionResult> UploadPicture(CreatePictureInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

            try
            {
                await this.pictureService.CreateAsync(input, userId, $"{this.environment.WebRootPath}/images");
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
                return(this.View(input));
            }

            this.TempData["Message"] = "Picture added successfully.";

            return(this.RedirectToAction("AllPictures"));
        }