public async Task <IActionResult> EditImage([FromForm(Name = "Image")] IFormFile file, int recipeId) { var defaultFailureJson = Json(new { success = false, message = "", elementId = SubmitImageErrorId }); var accountId = HttpContext.GetAccountId(); var findRecipe = _recipeService.FindRecipe(recipeId); if (!HttpContext.User.Identity.IsAuthenticated) { return(defaultFailureJson); } if (file != null && FileIsInvalid(file, out JsonResult jsonResult)) { return(jsonResult); } var recipe = await findRecipe; if (recipe == null) { return(defaultFailureJson); } _fileUploadService.DeleteIfNotNull(recipe.ImageFilename, _hostingEnv); var message = ""; var fileName = GenerateImageFilename(accountId, file); if (await _fileUploadService.TryUpload(file, fileName, _hostingEnv)) { recipe.ImageFilename = fileName; if (await _recipeService.Update(recipe).Success()) { return(Json(new { success = true, message, elementId = ImageId, imageSrc = Program.GetRequestPath(recipe.ImageFilename), errorElementIds = new string[] { ImageErrorId } })); } else { recipe.ImageFilename = null; } } message = "The picture could not be saved due to an error. Please try again later."; return(Json(new { success = false, message, elementId = SubmitImageErrorId })); }
public async Task <IActionResult> EditProfilePicture([FromForm(Name = "Image")] IFormFile file) { var accountId = HttpContext.GetAccountId(); var findAccount = _accountService.Find(accountId); if (FileIsInvalid(file, out JsonResult jsonResult)) { return(jsonResult); } var account = await findAccount; string fileName = GenerateImageFilename(account, file.FileName); _fileUploadService.DeleteIfNotNull(account.PictureFilename, _hostingEnv); if (await _fileUploadService.TryUpload(file, fileName, _hostingEnv)) { account.PictureFilename = GenerateImageFilename(account, file.FileName); if (await _accountService.Update(account).Success()) { return(Json(new { success = true, message = "", elementId = ProfilePictureId, imageSrc = Program.GetRequestPath(account.PictureFilename), errorElementIds = new string[] { ProfilePictureErrorId } })); } else { account.PictureFilename = null; } } return(Json(new { success = false, message = "The picture could not be saved due to an error. Please try again later.", elementId = SubmitPicErrorId })); }