public async Task <object> Add(File file) { bool isValid = await _fileService.AddFile(file); _notification.SetNotificationMessage(isValid, Title, file.UID); return(_notification); }
public IActionResult Create(IFormFile file, FileViewModel data, Guid id) { try { data.task = _tasksService.GetTask(id); if (data.task.deadline > DateTime.Now) { if (ModelState.IsValid) { string uniqueFilename; if (System.IO.Path.GetExtension(file.FileName) == ".pdf" && file.Length < 1048576) { //137 80 78 71 13 10 26 10 byte[] whiteList = new byte[] { 37, 80, 68, 70 }; if (file != null) { MemoryStream msIn = new MemoryStream(); using (var f = file.OpenReadStream()) { f.Position = 0; byte[] buffer = new byte[4]; f.Read(buffer, 0, 4); for (int i = 0; i < whiteList.Length; i++) { if (whiteList[i] == buffer[i]) { } else { ModelState.AddModelError("file", "file is not valid and accapteable"); return(View()); } } //...other reading of bytes happening f.Position = 0; // f.CopyTo(msIn); //hybird encrypt //uploading the file //correctness uniqueFilename = Guid.NewGuid() + Path.GetExtension(file.FileName); data.file = uniqueFilename; string absolutePath = @"ValuableFiles\" + uniqueFilename; try { var member = _memberService.GetMember(User.Identity.Name); file.CopyTo(msIn); var encryptedData = Encryption.HybridEncrypt(msIn, member.publicKey); System.IO.File.WriteAllBytes(absolutePath, encryptedData.ToArray()); data.signature = Encryption.SignData(encryptedData, member.privateKey); f.Close(); } catch (Exception ex) { //log _logger.LogError(ex, "Error happend while saving file"); return(View("Error", new ErrorViewModel() { Message = "Error while saving the file. Try again later" })); } } } } else { ModelState.AddModelError("file", "File is not valid and acceptable or size is greater than 10Mb"); return(View()); } //once the file has been inserted successfully in the db data.email = HttpContext.User.Identity.Name; //this is the currently logged in user _fileSerive.AddFile(data); var remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress; _logger.LogInformation("Current user uploading in files section: " + remoteIpAddress + " TimeStamp: " + System.DateTime.Now + " User: "******" File Id: " + data.file); TempData["message"] = "File inserted successfully"; return(View()); } else { ModelState.AddModelError("", "Check your input. Operation failed"); return(View(data)); } } else { TempData["error"] = "File deadline already passes"; return(View()); } } catch (Exception ex) { _logger.LogInformation("File upload not working" + ex); return(RedirectToAction("Error", "home")); } }