public async Task <IActionResult> UpdateAvatarHash([FromServices] IPFSService iPFSService, [FromForm] UpdateAvatarBindingModel model) { var hash = await iPFSService.AddFile(model?.Avatar?.FileName, model?.Avatar?.OpenReadStream()); if (string.IsNullOrEmpty(hash)) { return(this.BadRequest()); } return(this.Ok(new { hash })); }
public async Task <ActionResult> Index(HttpPostedFileBase avatar) { if (avatar == null) { avatar = Request.Files["avatar"]; } avatar.InputStream.Read(new byte[avatar.ContentLength], 0, avatar.ContentLength); var fileName = Path.GetFileName(avatar.FileName); var path = Path.Combine(Server.MapPath("~/tmp"), fileName); avatar.SaveAs(path); if (!string.IsNullOrEmpty(path)) { var ipfsService = new IPFSService(); var ipfs = ipfsService.GetIpfsClient(); var file = await ipfs.FileSystem.AddFileAsync(path); var userTmp = await UserManager.FindByIdAsync(User.Identity.GetUserId()); userTmp.Avatar = file.Id.Hash.ToString(); await UserManager.UpdateAsync(userTmp); } var userId = User.Identity.GetUserId(); var user = UserManager.FindById(userId); var model = new IndexViewModel { HasPassword = HasPassword(), PhoneNumber = await UserManager.GetPhoneNumberAsync(userId), TwoFactor = await UserManager.GetTwoFactorEnabledAsync(userId), Logins = await UserManager.GetLoginsAsync(userId), BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId), Address = GetAddress(), Avatar = string.IsNullOrEmpty(user.Avatar) ? defaultAvatar : user.Avatar, RefLink = user.RefLink, Init = user.Init.HasValue ? user.Init.Value : false }; return(View(model)); }