public async Task <ActionResult> Create(PictureCreateViewModel vm) { try { byte[] uploadedImage = new byte[vm.PictureFile.InputStream.Length]; vm.PictureFile.InputStream.Read(uploadedImage, 0, uploadedImage.Length); vm.Picture.PictureBytes = uploadedImage; var httpClient = TripGalleryHttpClient.GetClient(); var serializedTrip = JsonConvert.SerializeObject(vm.Picture); var response = await httpClient.PostAsync("api/trips/" + vm.TripId.ToString() + "/pictures", new StringContent(serializedTrip, System.Text.Encoding.Unicode, "application/json")).ConfigureAwait(false); if (response.IsSuccessStatusCode) { return(RedirectToAction("Index", "Pictures", new { tripId = vm.TripId })); } else { return(View("Error", new HandleErrorInfo(ExceptionHelper.GetExceptionFromResponse(response), "Pictures", "Create"))); } } catch (Exception ex) { return(View("Error", new HandleErrorInfo(ex, "Pictures", "Create"))); } }
// GET: Admin/Pictures/Create public async Task <IActionResult> Create(int screenId, bool isBackgroundImage) { var vm = new PictureCreateViewModel { IsBackgroundPicture = isBackgroundImage, ScreenId = screenId }; if (!isBackgroundImage) { vm.PromotionSecondsSelectList = new SelectList(SecondsValueManager.GetDictionaryKeysList(false)); var screen = await _bll.Screens.FindAsync(screenId); await _bll.SaveChangesAsync(); if (screen.ShowScheduleSeconds != null) { vm.ScheduleSecondsSelectList = new SelectList( SecondsValueManager.GetDictionaryKeysList(true), screen.ShowScheduleSeconds ); } else { vm.ScheduleSecondsSelectList = new SelectList(SecondsValueManager.GetDictionaryKeysList(true)); } } return(View(vm)); }
public async Task <IActionResult> Create(PictureCreateViewModel vm, IFormFile file) { if (!vm.PictureFromUrl || (vm.PictureFromUrl && string.IsNullOrEmpty(vm.Picture.Path) && file != null && file.Length > 0)) { vm.Picture.Path = ""; ModelState.Clear(); TryValidateModel(vm.Picture); if (file == null || file.Length < 0) { ModelState.AddModelError(string.Empty, Resources.Domain.PictureView.Picture.FileMissing); } else { if (file.Length > 20971520) { ModelState.AddModelError(string.Empty, Resources.Domain.PictureView.Picture.FileTooBigError); } var fileExtension = Path.GetExtension(file.FileName); if (!_imageExtensions.Contains(fileExtension.ToUpper())) { ModelState.AddModelError(string.Empty, Resources.Domain.PictureView.Picture.FileFormatError + string.Join(',', _imageExtensions) + "."); } } if (ModelState.ErrorCount > 0) { if (!vm.IsBackgroundPicture) { vm.PromotionSecondsSelectList = new SelectList( SecondsValueManager.GetDictionaryKeysList(false), vm.ShowPromotionSecondsString); var screen = await _bll.Screens.FindAsync(vm.ScreenId); await _bll.SaveChangesAsync(); if (screen.ShowScheduleSeconds != null) { vm.ScheduleSecondsSelectList = new SelectList( SecondsValueManager.GetDictionaryKeysList(true), screen.ShowScheduleSeconds ); } else { vm.ScheduleSecondsSelectList = new SelectList(SecondsValueManager.GetDictionaryKeysList(true)); } } return(View(vm)); } var innerDirName = vm.IsBackgroundPicture ? BackgroundDirectory : PromotionsDirectory; var directoryPath = Path.Combine(_appEnvironment.ContentRootPath, "wwwroot", "Images", innerDirName); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } var path = Path.Combine(directoryPath, file.FileName); vm.Picture.Path = path.Substring(path.IndexOf("Images", StringComparison.Ordinal) - 1).Replace("\\", "/"); try { await using var fileStream = new FileStream(path, FileMode.Create); await file.CopyToAsync(fileStream); } catch (Exception e) { throw new Exception(e.Message); } } var userId = _userManager.GetUserId(User); vm.Picture.CreatedAt = vm.Picture.ChangedAt = DateTime.Now; vm.Picture.CreatedBy = vm.Picture.ChangedBy = userId; var guid = await _bll.Pictures.AddAsync(vm.Picture); await _bll.SaveChangesAsync(); var picture = _bll.Pictures.GetUpdatesAfterUowSaveChanges(guid); string?showAddSeconds = SecondsValueManager.GetSelectedValue(null, false); // Promotion was added and ShowScheduleSeconds and ShowAddSeconds values were selected if (vm.IsBackgroundPicture == false && vm.ShowPromotionSecondsString != null && vm.ShowScheduleSecondsString != null) { showAddSeconds = vm.ShowPromotionSecondsString; var screen = await _bll.Screens.FindAsync(vm.ScreenId); screen.ShowScheduleSeconds = vm.ShowScheduleSecondsString; _bll.Screens.Update(screen); } await _bll.PictureInScreens.AddAsync(new PictureInScreen { CreatedAt = DateTime.Now, ChangedAt = DateTime.Now, ChangedBy = userId, CreatedBy = userId, IsBackgroundPicture = vm.IsBackgroundPicture, ScreenId = vm.ScreenId, PictureId = picture.Id, ShowAddSeconds = showAddSeconds }); await _bll.SaveChangesAsync(); return(RedirectToAction("Index", "ScreenSettings")); }