You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.0 KiB

1 year ago
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using CDPShared;
  10. using WMPLib;
  11. using System.Diagnostics.Metrics;
  12. namespace CircleViewer
  13. {
  14. public class ProtectCmd
  15. {
  16. private CDPWorker _cdp;
  17. public ProtectCmd(CDPWorker cdp)
  18. {
  19. _cdp = cdp;
  20. }
  21. public ProtectCmd()
  22. {
  23. _cdp = new CDPWorker();
  24. while (!_cdp.Ready)
  25. {
  26. Thread.Sleep(250);
  27. }
  28. }
  29. public async Task<string> ProtectFile(string fileName)
  30. {
  31. using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name + MethodBase.GetCurrentMethod().Name))
  32. {
  33. string ext = Path.GetExtension(fileName);
  34. if (!Constants.SupportedFiles.Contains(ext.ToLower()))
  35. {
  36. MinLogging.LogIt("File type not supported. " + fileName);
  37. return "";
  38. }
  39. string circleId = Properties.Settings.Default.DefaultCircleId;
  40. if (string.IsNullOrEmpty(circleId))
  41. {
  42. MinLogging.LogIt("Default Circle is empty");
  43. return "";
  44. }
  45. string topicId = Properties.Settings.Default.DefaultTopicId;
  46. if (string.IsNullOrEmpty(topicId))
  47. {
  48. MinLogging.LogIt("Default Topic is empty");
  49. return "";
  50. }
  51. while (!_cdp.Ready)
  52. {
  53. MinLogging.LogIt("Waiting...");
  54. Thread.Sleep(250);
  55. }
  56. MinLogging.LogIt("Securing " + Path.GetFileName(fileName));
  57. byte[] encryptedBuf = await _cdp.EncryptFile(circleId, topicId, fileName);
  58. string outPath = fileName + ".cir";
  59. File.WriteAllBytes(outPath, encryptedBuf);
  60. return outPath;
  61. }
  62. }
  63. public async Task<Boolean> Process(string fileName)
  64. {
  65. using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name + MethodBase.GetCurrentMethod().Name))
  66. {
  67. try
  68. {
  69. string userName = Environment.UserDomainName + "\\" + Environment.UserName;
  70. string yfPath = await ProtectFile(fileName);
  71. var meta = new Dictionary<string, string>();
  72. meta.Add($"Source ", fileName);
  73. meta.Add($"Target", yfPath);
  74. YellowFile yf = new YellowFile(yfPath);
  75. await _cdp.TrackAuthorizedAccess(yf, fileName, meta);
  76. return true;
  77. }
  78. catch (Exception e)
  79. {
  80. MinLogging.LogIt(e.Message);
  81. return false;
  82. }
  83. }
  84. }
  85. }
  86. }