public Task <string> Upload(string directoryName, string uniqueId = null) { _logger.Debug("Requesting to upload a new file to directory {directoryName}", directoryName); try { var file = Request.Form.Files.First(); return(_uploadRepository.UploadFileAsync(directoryName, file, uniqueId)); } catch (Exception e) { _logger.Warning(e, "Could not upload file"); throw; } }
/// <summary> /// Upload the Assignment into Upload Respository /// </summary> /// <param name="file"></param> /// <param name="courseId"></param> /// <returns></returns> public async Task <Upload> UploadFileAsync(IFormFile file, string fileName) { Upload upload; try { using (Stream stream = new MemoryStream()) { string filePath = ""; file.OpenReadStream(); await file.CopyToAsync(stream); stream.Seek(0, SeekOrigin.Begin); bool doesBlockExitsAsync = await _blobHelper.DoesBlobExistsAsync("assignments", fileName); if (doesBlockExitsAsync) { bool deleteBlobAsync = await _blobHelper.DeleteBlobAsync("assignments", fileName); if (deleteBlobAsync) { filePath = await _blobHelper.UploadFileToBlobAsync("assignments", fileName, stream); } } else { filePath = await _blobHelper.UploadFileToBlobAsync("assignments", fileName, stream); } upload = new Upload { Id = Guid.NewGuid(), FileName = fileName, FilePath = filePath }; await _uploadRepository.UploadFileAsync(upload); } } catch (Exception ex) { Logging.Logger.LogException(ex); return(null); } return(upload); }
public async Task <IActionResult> Register(RegisterViewModel vm, string returnUrl) { if (!ModelState.IsValid) { return(View(vm)); } if (!await _userRepository.InviteExistsAsync(vm.UniqueCode)) { _logger.Information("A user with email {userEmail} tried to register a new account using invite code {inviteCode}, but that code does not have an active invite", vm.Email, vm.UniqueCode); ModelState.AddModelError("", "hittade ingen inbjudan på denna kod"); return(View(vm)); } if (await _userRepository.NickExistsAsync(vm.NickName)) { _logger.Information("A user with email {userEmail} tried to register a new account using nickname {nickName}, but that nickname already exist", vm.Email, vm.NickName); ModelState.AddModelError("", "nicket finns redan registrerat"); return(View(vm)); } var user = new AppUser { UserName = vm.Email, Email = vm.Email, Bio = vm.Bio, Joined = DateTime.Now }; if (string.Equals(vm.Password, vm.ConfirmPassword)) { var result = await _userManager.CreateAsync(user, vm.Password); if (result.Succeeded) { await _userRepository.CreateNewNickNameAsync(user.Id, vm.NickName); await _userRepository.SetInviteAsAccountCreatedAsync(user.Id, vm.UniqueCode); if (vm.Avatar?.Length <= 700000) { var fileName = await _uploadRepository.UploadFileAsync("avatars", vm.Avatar, user.Id); await _userRepository.UpdateAvatarFileNameForUserWithIdAsync(user.Id, fileName); } if (vm.RegionId > 0) { await _userRepository.CreateRegionToUserAsync(user.Id, vm.RegionId); } await _signInManager.SignInAsync(user, isPersistent : true); if (string.IsNullOrWhiteSpace(returnUrl)) { return(RedirectToAction("index", "forum")); } return(Redirect(returnUrl)); } foreach (var error in result.Errors) { switch (error.Code) { case "DuplicateUserName": ModelState.AddModelError("", "- användarnamnet (email) finns redan registrerad"); break; case "PasswordRequiresUpper": ModelState.AddModelError("", "- lösenordet måste innehålla minst en stor bokstav"); break; case "PasswordRequiresNonAlphanumeric": ModelState.AddModelError("", "- lösenordet måste innehålla minst ett icke alfanumeriskt tecken, t.ex ett utropstecken eller punkt"); break; case "PasswordRequiresDigit": ModelState.AddModelError("", "- lösenordet måste innehålla minst en siffra"); break; } } _logger.Information("Could not register account due to the following errors {@registerModelErrors}", result.Errors.Select(x => x.Code)); } else { ModelState.AddModelError("", "lösenorden matchar inte"); } return(View(vm)); }