AipFileService.cs 18 KB

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