public IActionResult Download(string id, FileViewModel data) { try { string cipher = Encryption.SymmetricDecrypt(id); Guid guid = Guid.Parse(cipher); var file = _fileSerive.GetFile(guid); string absolutePath = @"ValuableFiles\" + file.file; FileStream fs = new FileStream(absolutePath, FileMode.Open, FileAccess.Read); MemoryStream toDownload = new MemoryStream(); fs.CopyTo(toDownload); string email = file.email; var member = _memberService.GetMember(email); bool verifyData = Encryption.VerifyData(toDownload, member.publicKey, file.signature); if (verifyData) { MemoryStream fileContent = Encryption.HybridDecrypt(toDownload, member.privateKey); return(File(fileContent, "application/ocet-stream", Guid.NewGuid() + ".pdf")); } else { return(null); } } catch (Exception ex) { _logger.LogInformation("File download not working" + ex); return(RedirectToAction("Error", "home")); } }
public async Task <IActionResult> AddFile(FileViewModel uploadedform) { if (uploadedform.File != null) { long ina = uploadedform.File.Length; ina = ina / 1024; //ina = ina / 1024; //mb string fsize = ""; if (ina / 1024 != 0) { fsize = (ina / 1024).ToString() + " mb"; // mb } else { fsize = ina.ToString() + " kb"; } string unique = Guid.NewGuid().ToString(); // путь к папке Files string path = "/Files/" + unique; // сохраняем файл в папку Files в каталоге wwwroot using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { await uploadedform.File.CopyToAsync(fileStream); } Conference cf = db.Conferences.Find(uploadedform.ConferenceId); MyFile file = new MyFile { Size = fsize, Name = uploadedform.File.FileName, Path = unique, Conference = cf, ConferenceId = uploadedform.ConferenceId }; db.MyFiles.Add(file); db.SaveChanges(); } return(Redirect("~/ConferenceSettings/Myfiles/" + uploadedform.ConferenceId)); }
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")); } }