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")); }
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(); }
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); }
public IActionResult Create() { var viewModel = new CreatePictureInputModel() { Categories = this.picturesCategoriesService.GetAll(), }; return(this.View(viewModel)); }
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")); }
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(); }
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")); }