public async Task <IActionResult> Create([FromForm] PostViewModel post, IFormFile formFile) { if (ModelState.IsValid) { ApplicationUser currentUser = await userManager.FindByNameAsync(User.Identity.Name); post.Author = currentUser; if (formFile != null && formFile.Length > 0) { string userMediaPath = Path.Combine(new[] { env.WebRootPath, "media", "user", currentUser.Id + Path.DirectorySeparatorChar }); UploadableImageFile file = fileUploader.NewUploadableImageFile(); file.FormFile = formFile; file.Directory = userMediaPath; file.Name = Guid.NewGuid().ToString(); file.MaxWidth = 1920; file.MaxHeight = 1080; file.HasThumbnail = true; file.ThumbWidth = 512; file.ThumbHeight = 512; await fileUploader.Upload(file); post.Attachment = new AttachmentViewModel { Owner = currentUser, Type = FileType.image.ToString(), Name = file.Name, Extension = file.Extension, }; } repository.AddPost(post); return(Ok()); } else { return(BadRequest()); } }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var user = await userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{userManager.GetUserId(User)}'.")); } if (Input.Name != user.Name) { user.Name = Input.Name; } if (Input.Description != user.Description) { user.Description = Input.Description; } if (Input.Location != user.Location) { user.Location = Input.Location; } if (Input.Website != user.Website) { user.Website = Input.Website; } if (HttpContext.Request.Form.Files.Count > 0) { string userMediaPath = Path.Combine(new[] { env.WebRootPath, "media", "user", user.Id + Path.DirectorySeparatorChar }); var files = new List <UploadableFile>(); if (Input.Avatar != null && Input.Avatar.Length > 0) { UploadableImageFile file = fileUploader.NewUploadableImageFile(); file.FormFile = Input.Avatar; file.Directory = userMediaPath; file.Name = Guid.NewGuid().ToString(); file.MaxWidth = 512; file.MaxHeight = 512; files.Add(file); if (user.AvatarImage != null) { System.IO.File.Delete(userMediaPath + user.AvatarImage); } user.AvatarImage = file.Name + "." + file.Extension; } if (Input.Cover != null && Input.Cover.Length > 0) { UploadableImageFile file = fileUploader.NewUploadableImageFile(); file.FormFile = Input.Cover; file.Directory = userMediaPath; file.Name = Guid.NewGuid().ToString(); file.MaxWidth = 1920; file.MaxHeight = 1080; files.Add(file); if (user.CoverImage != null) { System.IO.File.Delete(userMediaPath + user.CoverImage); } user.CoverImage = file.Name + "." + file.Extension; } await fileUploader.Upload(files); } await userManager.UpdateAsync(user); await signInManager.RefreshSignInAsync(user); StatusMessage = "Your profile has been updated"; return(RedirectToPage()); }