public async Task <IActionResult> CreateVideo(AssetCreateVideoViewModel model) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadedAssetFile(model); var video = _mapper.Map <Video>(model); video.Status = _context.Statuses.FirstOrDefault(x => x.Name == "Available"); video.ImageUrl = "/images/" + uniqueFileName; video.Location = _branch.GetBranchByName(model.LibraryBranchName); //Prevent exceptions while searching when the director of the video is unknown if (video.Director == null) { video.Director = "-"; } await _assetsService.AddAsync(video); return(RedirectToAction("Create", "Catalog")); } return(View(model)); }
public IActionResult CreateVideo(AssetCreateVideoViewModel model) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadedVideoFile(model); var video = new Video { Title = model.Title, Director = model.Director, Year = model.Year, Status = _context.Statuses.FirstOrDefault(x => x.Name == "Available"), Cost = model.Cost, ImageUrl = "/images/" + uniqueFileName, NumberOfCopies = model.NumberOfCopies, Location = _branch.GetBranchByName(model.LibraryBranchName) }; //Prevent exceptions while searching when the director of the video is unknown if (video.Director == null) { video.Director = "-"; } _assetsService.Add(video); return(RedirectToAction("Create", "Catalog")); } return(View(model)); }
private string ProcessUploadedVideoFile(AssetCreateVideoViewModel model) { string uniqueFileName = null; if (model.Photo != null) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Photo.CopyTo(fileStream); } } return(uniqueFileName); }