public async Task <IActionResult> AddPictureGem(PictureGemManagerViewModel gem) { if (ModelState.IsValid && gem.PictureMetadata != null && gem.PictureMetadata.PictureFile.ContentType.StartsWith("image")) { var svc = new PictureService(); await svc.SavePictureToDatabase(gem, await _userManager.GetUserAsync(User), _pathsOpt.Value, _context); } return(RedirectToAction("PictureGemManager")); }
public async Task SavePictureToDatabase(PictureGemManagerViewModel gem, ChilledUser user, Paths paths, ChilledDbContext context) { var gemDB = new Gem() { Title = gem.PictureMetadata.Title, CreatedBy = user, GemType = GemType.Picture, SummaryText = gem.PictureMetadata.SummaryText, PictureData = new Picture() { HoverText = gem.PictureMetadata.HoverText, ArtistName = gem.PictureMetadata.ArtistName, ArtistLink = gem.PictureMetadata.ArtistLink, CreatedDate = DateTime.Now, FileSize = gem.PictureMetadata.PictureFile.Length } }; context.Gems.Add(gemDB); context.SaveChanges(); var filePath = paths.PicturesDirectory; var fileFolder = Path.Combine(filePath, gemDB.Id.ToString()); var fileName = gem.PictureMetadata.PictureFile.FileName; var filetype = gem.PictureMetadata.PictureFile.ContentType; var fullyQualified = Path.Combine(fileFolder, fileName); Directory.CreateDirectory(fileFolder); using (var stream = new FileStream(fullyQualified, FileMode.CreateNew)) { await gem.PictureMetadata.PictureFile.CopyToAsync(stream); stream.Flush(); } gemDB.PictureData.Location = $"//{paths.URLPath}/uploads/pictures/{gemDB.Id}/{fileName}"; gemDB.FilePath = fullyQualified; context.SaveChanges(); }