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.

103 lines
3.2 KiB

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