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
98 lines
3.0 KiB
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using CDPShared;
|
|
using WMPLib;
|
|
using System.Diagnostics.Metrics;
|
|
|
|
namespace CircleViewer
|
|
{
|
|
public class ProtectCmd
|
|
{
|
|
private CDPWorker _cdp;
|
|
|
|
public ProtectCmd(CDPWorker cdp)
|
|
{
|
|
_cdp = cdp;
|
|
}
|
|
public ProtectCmd()
|
|
{
|
|
_cdp = new CDPWorker();
|
|
while (!_cdp.Ready)
|
|
{
|
|
Thread.Sleep(250);
|
|
}
|
|
}
|
|
|
|
public async Task<string> ProtectFile(string fileName)
|
|
{
|
|
using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name + MethodBase.GetCurrentMethod().Name))
|
|
{
|
|
string ext = Path.GetExtension(fileName);
|
|
|
|
if (!Constants.SupportedFiles.Contains(ext.ToLower()))
|
|
{
|
|
MinLogging.LogIt("File type not supported. " + fileName);
|
|
return "";
|
|
}
|
|
|
|
string circleId = Properties.Settings.Default.DefaultCircleId;
|
|
if (string.IsNullOrEmpty(circleId))
|
|
{
|
|
MinLogging.LogIt("Default Circle is empty");
|
|
return "";
|
|
}
|
|
|
|
string topicId = Properties.Settings.Default.DefaultTopicId;
|
|
if (string.IsNullOrEmpty(topicId))
|
|
{
|
|
MinLogging.LogIt("Default Topic is empty");
|
|
return "";
|
|
}
|
|
|
|
while (!_cdp.Ready)
|
|
{
|
|
MinLogging.LogIt("Waiting...");
|
|
Thread.Sleep(250);
|
|
}
|
|
|
|
MinLogging.LogIt("Securing " + Path.GetFileName(fileName));
|
|
|
|
byte[] encryptedBuf = await _cdp.EncryptFile(circleId, topicId, fileName);
|
|
|
|
string outPath = fileName + ".cir";
|
|
File.WriteAllBytes(outPath, encryptedBuf);
|
|
return outPath;
|
|
}
|
|
}
|
|
|
|
public async Task<Boolean> Process(string fileName)
|
|
{
|
|
using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name + MethodBase.GetCurrentMethod().Name))
|
|
{
|
|
try
|
|
{
|
|
string userName = Environment.UserDomainName + "\\" + Environment.UserName;
|
|
string yfPath = await ProtectFile(fileName);
|
|
var meta = new Dictionary<string, string>();
|
|
meta.Add($"Source ", fileName);
|
|
meta.Add($"Target", yfPath);
|
|
|
|
YellowFile yf = new YellowFile(yfPath);
|
|
await _cdp.TrackAuthorizedAccess(yf, fileName, meta);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MinLogging.LogIt(e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|