public async Task <IActionResult> OnGetAsync(string id) { var image = await _imagesService.FindByIdAsync(id); if (image == null) { return(NotFound()); } PageTitle = await TranslationsService.TranslateAsync("images"); TitleDisplay = await TranslationsService.TranslateAsync("title"); ImageDisplay = await TranslationsService.TranslateAsync("image"); TitleRequiredError = TranslationsService.TranslateErrorByDescriber(ErrorDescriberConstants.RequiredField, $"The field '{TitleDisplay}' is required.", TitleDisplay); TitleLengthError = TranslationsService.TranslateErrorByDescriber(ErrorDescriberConstants.StringLength, $"The field '{TitleDisplay}' must contain a minimum of {4} and a maximum of {30} characters.", TitleDisplay, 4.ToString(), 30.ToString()); ForeignKeyProperties = _imagesService.GetForeignKeyPropertiesToDto(image).ToList(); ViewData["ImgId"] = id; ImageId = id; Vm = new UpdateImageViewModel { Title = image.Name, Url = image.Url }; return(Page()); }
public async Task <ActionResult> Update([FromForm] UpdateImageViewModel updateImageViewModel) { if (updateImageViewModel.Files.Length > 0 && updateImageViewModel.Files.FileName.Length > 0) { try { if (!Directory.Exists(_environment.WebRootPath + "uploads/")) { Directory.CreateDirectory(_environment.WebRootPath + "uploads/"); } using (var reader = new BinaryReader(updateImageViewModel.Files.OpenReadStream())) { var signatures = _fileSignature[Path.GetExtension(updateImageViewModel.Files.FileName).ToLowerInvariant()]; var headerBytes = reader.ReadBytes(signatures.Max(m => m.Length)); validFile = signatures.Any(signature => headerBytes.Take(signature.Length).SequenceEqual(signature)); } if (!validFile) { return(BadRequest()); } System.IO.File.Delete(_environment.WebRootPath + "uploads/" + updateImageViewModel.ResponseCode + ".jpeg"); await using (FileStream filestream = System.IO.File.Create(_environment.WebRootPath + "uploads/" + updateImageViewModel.ResponseCode + ".jpeg")) { await updateImageViewModel.Files.CopyToAsync(filestream); filestream.Flush(); return(Ok()); } } //TODO: Implement authentication schema catch (KeyNotFoundException) { return(BadRequest()); } } return(Ok()); }