public IActionResult PostCreate() { var httpRequest = _httpContextAccessor.HttpContext.Request; //Upload Image var file = httpRequest.Form.Files[0]; if (file == null) { _toastNotification.AddErrorToastMessage("Please choose image for upload!"); _logger.LogError("User " + User.Identity.Name + " tried to create post without image!"); return(NotFound()); } if (!FileUploadCheck.CheckImageFormat(file)) { _toastNotification.AddWarningToastMessage("Incorrect format of uploading file!"); _logger.LogError("User " + User.Identity.Name + " tried to upload not image file!"); return(NotFound()); } string uniqueFileName = null; if (file.Length > 0 && FileUploadCheck.CheckImageFormat(file)) { string uploadsFolder = Path.Combine(_hostingEnviroment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid() + "_" + file.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); file.CopyTo(new FileStream(filePath, FileMode.Create)); } var dto = new PostDto { Title = httpRequest.Form["Title"], Description = httpRequest.Form["Description"], ImagePath = uniqueFileName, AuthorFullName = httpRequest.Form["AuthorFullName"], ApplicationUserId = Guid.Parse(httpRequest.Form["ApplicationUserId"]) }; _postService.CreatePost(dto); return(Ok(dto)); }
public async Task <IActionResult> EditPost() { var httpRequest = _httpContextAccessor.HttpContext.Request; var id = int.Parse(httpRequest.Form["Id"]); string uniqueFileName = null; //Upload Image if (httpRequest.Form.Files.Count > 0) { var file = httpRequest.Form.Files[0]; if (!FileUploadCheck.CheckImageFormat(file)) { _toastNotification.AddWarningToastMessage("Incorrect format of uploading file!"); _logger.LogError("User " + User.Identity.Name + " tried to upload not image file!"); return(NotFound()); } if (file.Length > 0 && FileUploadCheck.CheckImageFormat(file)) { string uploadsFolder = Path.Combine(_hostingEnviroment.WebRootPath, "assets"); uniqueFileName = Guid.NewGuid() + "_" + file.FileName; var filePath = Path.Combine(uploadsFolder, uniqueFileName); file.CopyTo(new FileStream(filePath, FileMode.Create)); } } var postDto = await _postService.GetPostById(id); postDto.Title = httpRequest.Form["Title"]; postDto.Description = httpRequest.Form["Description"]; if (uniqueFileName != null) { postDto.ImagePath = uniqueFileName; } var post = _mapper.Map <Post>(postDto); _postService.UpdatePost(post); return(Ok(post)); }