AipFileService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using AipGateway.AIP;
  2. using AipGateway.API.Application.Configurations;
  3. using AipGateway.API.Domain.Entities;
  4. using AipGateway.API.Repositories;
  5. using AipGateway.API.Services.Interfaces;
  6. using System.Collections;
  7. using System.Diagnostics;
  8. using AipGateway.API.Application.Utils;
  9. namespace AipGateway.API.Services
  10. {
  11. public class AipFileService : IAipFileService
  12. {
  13. private readonly ILogger<AipFileService> _log;
  14. private readonly IAipDbRepository _repo;
  15. private readonly AipSettings _aipSetting;
  16. private Hashtable _aipLableMap = new Hashtable();
  17. private Hashtable _aipPolicyMap = new Hashtable();
  18. private Hashtable _aipProtectionMap = new Hashtable();
  19. private Hashtable _supportedFileExtMap = new Hashtable();
  20. private Hashtable _labelFileExtMap = new Hashtable();
  21. private Hashtable _deleteLabelFileExtMap = new Hashtable();
  22. private Hashtable _protectFileExtMap = new Hashtable();
  23. private Hashtable _deleteProtectFileExtMap = new Hashtable();
  24. private AipFileManager _aipFileManager;
  25. public AipFileManager aipFileManager { get => _aipFileManager; }
  26. public AipSettings aipSetting { get => _aipSetting; }
  27. public AipFileService(ILogger<AipFileService> log, IAipDbRepository repo, AipFileManager aipFileManager, AipSettings aipSetting)
  28. {
  29. _log = log;
  30. _repo = repo;
  31. _aipSetting = aipSetting;
  32. _aipFileManager = aipFileManager;
  33. Inititialize();
  34. }
  35. private void Inititialize()
  36. {
  37. string supportedFileExt = _aipSetting.SupportedFileExt;
  38. string protectedFileExt = _aipSetting.ProtectedFileExt;
  39. string[] fileExts = supportedFileExt.Split(';');
  40. foreach (string ext in fileExts)
  41. {
  42. string fileExt = ext.Trim();
  43. if (fileExt.Trim() == "") continue;
  44. _supportedFileExtMap[fileExt] = fileExt;
  45. _labelFileExtMap[fileExt] = fileExt;
  46. _deleteLabelFileExtMap[fileExt] = fileExt;
  47. _protectFileExtMap[fileExt] = fileExt;
  48. _deleteProtectFileExtMap[fileExt] = fileExt;
  49. }
  50. string[] protectedExts = protectedFileExt.Split(';');
  51. foreach (string ext in protectedExts)
  52. {
  53. if (ext.Trim() == "") continue;
  54. string[] extFile = ext.Split('-');
  55. if (extFile.Length == 2)
  56. {
  57. string orgFileExt = extFile[0].Trim();
  58. string setFileExt = extFile[1].Trim();
  59. // supportedFileExtMap
  60. if (_supportedFileExtMap.Contains(orgFileExt))
  61. {
  62. _supportedFileExtMap.Remove(orgFileExt);
  63. }
  64. _supportedFileExtMap[orgFileExt] = orgFileExt;
  65. if (_supportedFileExtMap.Contains(setFileExt))
  66. {
  67. _supportedFileExtMap.Remove(setFileExt);
  68. }
  69. _supportedFileExtMap[setFileExt] = setFileExt;
  70. // labelFileExtMap
  71. if (_labelFileExtMap.Contains(orgFileExt))
  72. {
  73. _labelFileExtMap.Remove(orgFileExt);
  74. }
  75. _labelFileExtMap[orgFileExt] = orgFileExt;
  76. if (_labelFileExtMap.Contains(setFileExt))
  77. {
  78. _labelFileExtMap.Remove(setFileExt);
  79. }
  80. _labelFileExtMap[setFileExt] = setFileExt;
  81. // deleteLabelFileExtMap
  82. if (_deleteLabelFileExtMap.Contains(orgFileExt))
  83. {
  84. _deleteLabelFileExtMap.Remove(orgFileExt);
  85. }
  86. _deleteLabelFileExtMap[orgFileExt] = orgFileExt;
  87. if (_deleteLabelFileExtMap.Contains(setFileExt))
  88. {
  89. _deleteLabelFileExtMap.Remove(setFileExt);
  90. }
  91. _deleteLabelFileExtMap[setFileExt] = setFileExt;
  92. // protectFileExtMap
  93. if (_protectFileExtMap.Contains(orgFileExt))
  94. {
  95. _protectFileExtMap.Remove(orgFileExt);
  96. }
  97. _protectFileExtMap[orgFileExt] = setFileExt;
  98. // deleteProtectFileExtMap
  99. if (_deleteProtectFileExtMap.Contains(setFileExt))
  100. {
  101. _deleteProtectFileExtMap.Remove(setFileExt);
  102. }
  103. _deleteProtectFileExtMap[setFileExt] = orgFileExt;
  104. }
  105. }
  106. }
  107. public string GetDispFileName(string dispFileName, string outputFileName)
  108. {
  109. return Path.GetFileNameWithoutExtension(dispFileName) + Path.GetExtension(outputFileName);
  110. }
  111. public string GetRequestFileName(string realFileName)
  112. {
  113. return _aipSetting.SourceFileDir + realFileName;
  114. }
  115. public string GetActualFileName(string fileName)
  116. {
  117. return _aipSetting.TargetFileDir + fileName;
  118. }
  119. public string? GetSupportedFileType(string fileName)
  120. {
  121. string fileExt = Path.GetExtension(fileName).ToLower();
  122. return _supportedFileExtMap[fileExt] as string;
  123. }
  124. //public string? GetSetLabelFileExt(string reqFileName)
  125. //{
  126. // string fileExt = Path.GetExtension(reqFileName).ToLower();
  127. // return _labelFileExtMap[fileExt] as string;
  128. //}
  129. //public string? GetSetProtectFileExt(string reqFileName)
  130. //{
  131. // string fileExt = Path.GetExtension(reqFileName).ToLower();
  132. // return _protectFileExtMap[fileExt] as string;
  133. //}
  134. //public string? GetDeleteLabelFileExt(string fileName)
  135. //{
  136. // string fileExt = Path.GetExtension(fileName).ToLower();
  137. // return _deleteProtectFileExtMap[fileExt] as string;
  138. //}
  139. //public string? GetDeleteProtectFileExt(string fileName)
  140. //{
  141. // string fileExt = Path.GetExtension(fileName).ToLower();
  142. // return _deleteProtectFileExtMap[fileExt] as string;
  143. //}
  144. public AipSettings GetAipSettings()
  145. {
  146. return _aipSetting;
  147. }
  148. public int DownloadAipFileInformations()
  149. {
  150. DownloadAipFileLabels();
  151. DownloadAipFilePolicies();
  152. DownloadAipFileProtections();
  153. return _aipLableMap.Count + _aipPolicyMap.Count + _aipProtectionMap.Count;
  154. }
  155. private void DownloadAipFileLabels()
  156. {
  157. //using var transaction = _dbContext.Database.BeginTransaction();
  158. try
  159. {
  160. Stopwatch sw = Stopwatch.StartNew();
  161. sw.Start();
  162. _log.LogInformation("*** AipFileService.DownloadAipFileLabels: Start.");
  163. Hashtable keyMap = new Hashtable();
  164. List<TbAipLabel> newLabels = new List<TbAipLabel>();
  165. List<TbAipLabel> updLabels = new List<TbAipLabel>();
  166. Hashtable labelMap = new Hashtable();
  167. List<AipLabel>? lavels = _aipFileManager.SensitivityLabels();
  168. _log.LogInformation("AipFileService.DownloadAipFileLabels.SensitivityLabels(): {0} EA.", lavels?.Count);
  169. if (lavels == null)
  170. {
  171. return;
  172. }
  173. var result = _repo.LoadAipLabels().Result;
  174. if (result != null)
  175. {
  176. foreach (TbAipLabel label in result)
  177. {
  178. labelMap.Add(label.LabelGuid, label);
  179. }
  180. }
  181. foreach (AipLabel label in lavels)
  182. {
  183. keyMap.Add(label.Id, label);
  184. if (labelMap.ContainsKey(label.Id))
  185. {
  186. TbAipLabel? orgLabel = labelMap[label.Id] as TbAipLabel;
  187. if (orgLabel != null)
  188. {
  189. if (orgLabel.IsChanged(label))
  190. {
  191. _log.LogInformation("변경된 레벨 데이터: {0}", label.Id);
  192. orgLabel.LabelName = label.Name;
  193. orgLabel.LabelDesc = label.Description;
  194. updLabels.Add(orgLabel);
  195. }
  196. else
  197. {
  198. _log.LogInformation("동일한 레벨 데이터: {0}", label.Id);
  199. }
  200. }
  201. else
  202. {
  203. _log.LogInformation("Not Found Label In Map: {0}", label.Id);
  204. }
  205. }
  206. else
  207. {
  208. _log.LogInformation("새로운 레벨 데이터: {0}", label.Id);
  209. TbAipLabel obj = new TbAipLabel()
  210. {
  211. LabelGuid = label.Id,
  212. LabelName = label.Name,
  213. LabelDesc = label.Description,
  214. CreatedAt = DateTime.Now,
  215. DeletedAt = null,
  216. UseYn = true,
  217. };
  218. newLabels.Add(obj);
  219. }
  220. }
  221. _aipLableMap = keyMap;
  222. _log.LogInformation("AipFileService.DownloadAipFileLabels: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
  223. int updateCount = _repo.UpdateAipLables(updLabels).Result;
  224. int insertCount = _repo.InsertAipLables(newLabels).Result;
  225. sw.Stop();
  226. _log.LogInformation("*** AipFileService.DownloadAipFileLabels: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.",
  227. sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount);
  228. }
  229. catch (Exception ex)
  230. {
  231. _log.LogError($"*** AipFileService.DownloadAipFileLabels: {ex}");
  232. }
  233. }
  234. private void DownloadAipFilePolicies()
  235. {
  236. //using var transaction = _dbContext.Database.BeginTransaction();
  237. try
  238. {
  239. Stopwatch sw = Stopwatch.StartNew();
  240. sw.Start();
  241. _log.LogInformation("*** AipFileService.DownloadAipFilePolicies: Start.");
  242. Hashtable keyMap = new Hashtable();
  243. List<TbAipPolicy> newLabels = new List<TbAipPolicy>();
  244. List<TbAipPolicy> updLabels = new List<TbAipPolicy>();
  245. Hashtable labelMap = new Hashtable();
  246. List<AipLabel>? lavels = _aipFileManager.ListSensitivityLabels();
  247. _log.LogInformation("AipFileService.DownloadAipFilePolicies.ListSensitivityLabels(): {0} EA.", lavels?.Count);
  248. if (lavels == null)
  249. {
  250. return;
  251. }
  252. var result = _repo.LoadAipPolicies().Result;
  253. if (result != null)
  254. {
  255. foreach (TbAipPolicy policy in result)
  256. {
  257. labelMap.Add(policy.PolicyGuid, policy);
  258. }
  259. }
  260. foreach (AipLabel label in lavels)
  261. {
  262. keyMap.Add(label.Id, label);
  263. if (labelMap.ContainsKey(label.Id))
  264. {
  265. TbAipPolicy? orgPolicy = labelMap[label.Id] as TbAipPolicy;
  266. if (orgPolicy != null)
  267. {
  268. if (orgPolicy.IsChanged(label))
  269. {
  270. _log.LogInformation("변경된 정책 데이터: {0}", label.Id);
  271. orgPolicy.PolicyName = label.Name;
  272. orgPolicy.PolicyDesc = label.Description;
  273. updLabels.Add(orgPolicy);
  274. }
  275. else
  276. {
  277. _log.LogInformation("동일한 정책 데이터: {0}", label.Id);
  278. }
  279. }
  280. else
  281. {
  282. _log.LogInformation("Not Found Policy In Map: {0}", label.Id);
  283. }
  284. }
  285. else
  286. {
  287. _log.LogInformation("새로운 정책 데이터: {0}", label.Id);
  288. TbAipPolicy obj = new TbAipPolicy()
  289. {
  290. PolicyGuid = label.Id,
  291. PolicyName = label.Name,
  292. PolicyDesc = label.Description,
  293. CreatedAt = DateTime.Now,
  294. DeletedAt = null,
  295. UseYn = true,
  296. };
  297. newLabels.Add(obj);
  298. }
  299. }
  300. _aipPolicyMap = keyMap;
  301. _log.LogInformation("AipFileService.DownloadAipFilePolicies: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
  302. int updateCount = _repo.UpdateAipPolicies(updLabels).Result;
  303. int insertCount = _repo.InsertAipPolicies(newLabels).Result;
  304. sw.Stop();
  305. _log.LogInformation("*** AipFileService.DownloadAipFilePolicies: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.",
  306. sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount);
  307. }
  308. catch (Exception ex)
  309. {
  310. _log.LogError($"*** AipFileService.DownloadAipFilePolicies: {ex}");
  311. }
  312. }
  313. private void DownloadAipFileProtections()
  314. {
  315. //using var transaction = _dbContext.Database.BeginTransaction();
  316. try
  317. {
  318. Stopwatch sw = Stopwatch.StartNew();
  319. sw.Start();
  320. _log.LogInformation("*** AipFileService.DownloadAipFileProtections: Start.");
  321. Hashtable keyMap = new Hashtable();
  322. List<TbAipProtection> newLabels = new List<TbAipProtection>();
  323. List<TbAipProtection> updLabels = new List<TbAipProtection>();
  324. Hashtable labelMap = new Hashtable();
  325. List<AipTemplate>? templates = _aipFileManager.GetTemplates();
  326. _log.LogInformation("AipFileService.DownloadAipFileProtections.GetTemplates(): {0} EA.", templates?.Count);
  327. if (templates == null)
  328. {
  329. return;
  330. }
  331. var result = _repo.LoadAipTemplates().Result;
  332. if (result != null)
  333. {
  334. foreach (TbAipProtection policy in result)
  335. {
  336. labelMap.Add(policy.ProtectionGuid, policy);
  337. }
  338. }
  339. foreach (AipTemplate template in templates)
  340. {
  341. keyMap.Add(template.Id, template);
  342. if (labelMap.ContainsKey(template.Id))
  343. {
  344. TbAipProtection? orgProtection = labelMap[template.Id] as TbAipProtection;
  345. if (orgProtection != null)
  346. {
  347. if (orgProtection.IsChanged(template))
  348. {
  349. _log.LogInformation("변경된 보호 데이터: {0}", template.Id);
  350. orgProtection.ProtectionName = template.Name;
  351. orgProtection.ProtectionDesc = template.Description;
  352. updLabels.Add(orgProtection);
  353. }
  354. else
  355. {
  356. _log.LogInformation("동일한 보호 데이터: {0}", template.Id);
  357. }
  358. }
  359. else
  360. {
  361. _log.LogInformation("Not Found Policy In Map: {0}", template.Id);
  362. }
  363. }
  364. else
  365. {
  366. _log.LogInformation("새로운 보호 데이터: {0}", template.Id);
  367. TbAipProtection obj = new TbAipProtection()
  368. {
  369. ProtectionGuid = template.Id,
  370. ProtectionName = template.Name,
  371. ProtectionDesc = template.Description,
  372. CreatedAt = DateTime.Now,
  373. DeletedAt = null,
  374. UseYn = true,
  375. };
  376. newLabels.Add(obj);
  377. }
  378. }
  379. _aipProtectionMap = keyMap;
  380. _log.LogInformation("AipFileService.DownloadAipFileProtections: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
  381. int updateCount = _repo.UpdateAipTemplates(updLabels).Result;
  382. int insertCount = _repo.InsertAipTemplates(newLabels).Result;
  383. sw.Stop();
  384. _log.LogInformation("*** AipFileService.DownloadAipFileProtections: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.",
  385. sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount);
  386. }
  387. catch (Exception ex)
  388. {
  389. _log.LogError($"*** AipFileService.DownloadAipFileProtections: {ex}");
  390. }
  391. }
  392. }
  393. }