using AipGateway.API.Application.Configurations; using AipGateway.API.Domain.Entities; using AipGateway.AIP.Service.Repositories; using AipGateway.AIP.Service.Services.Interfaces; using System.Collections; using System.Diagnostics; using AipGateway.API.Application.Utils; namespace AipGateway.AIP.Service.Services { public class AipFileService : IAipFileService { private readonly ILogger _log; private readonly IAipDbRepository _repo; private readonly AipSettings _aipSetting; private Hashtable _aipLableMap = new Hashtable(); private Hashtable _aipPolicyMap = new Hashtable(); private Hashtable _aipProtectionMap = new Hashtable(); private Hashtable _supportedFileExtMap = new Hashtable(); private Hashtable _labelFileExtMap = new Hashtable(); private Hashtable _deleteLabelFileExtMap = new Hashtable(); private Hashtable _protectFileExtMap = new Hashtable(); private Hashtable _deleteProtectFileExtMap = new Hashtable(); private AipFileManager _aipFileManager; public AipFileManager aipFileManager { get => _aipFileManager; } public AipSettings aipSetting { get => _aipSetting; } public AipFileService(ILogger log, IAipDbRepository repo, AipFileManager aipFileManager, AipSettings aipSetting) { _log = log; _repo = repo; _aipSetting = aipSetting; _aipFileManager = aipFileManager; Inititialize(); } private void Inititialize() { string supportedFileExt = _aipSetting.SupportedFileExt; string protectedFileExt = _aipSetting.ProtectedFileExt; string[] fileExts = supportedFileExt.Split(';'); foreach (string ext in fileExts) { string fileExt = ext.Trim(); if (fileExt.Trim() == "") continue; _supportedFileExtMap[fileExt] = fileExt; _labelFileExtMap[fileExt] = fileExt; _deleteLabelFileExtMap[fileExt] = fileExt; _protectFileExtMap[fileExt] = fileExt; _deleteProtectFileExtMap[fileExt] = fileExt; } string[] protectedExts = protectedFileExt.Split(';'); foreach (string ext in protectedExts) { if (ext.Trim() == "") continue; string[] extFile = ext.Split('-'); if (extFile.Length == 2) { string orgFileExt = extFile[0].Trim(); string setFileExt = extFile[1].Trim(); // supportedFileExtMap if (_supportedFileExtMap.Contains(orgFileExt)) { _supportedFileExtMap.Remove(orgFileExt); } _supportedFileExtMap[orgFileExt] = orgFileExt; if (_supportedFileExtMap.Contains(setFileExt)) { _supportedFileExtMap.Remove(setFileExt); } _supportedFileExtMap[setFileExt] = setFileExt; // labelFileExtMap if (_labelFileExtMap.Contains(orgFileExt)) { _labelFileExtMap.Remove(orgFileExt); } _labelFileExtMap[orgFileExt] = orgFileExt; if (_labelFileExtMap.Contains(setFileExt)) { _labelFileExtMap.Remove(setFileExt); } _labelFileExtMap[setFileExt] = setFileExt; // deleteLabelFileExtMap if (_deleteLabelFileExtMap.Contains(orgFileExt)) { _deleteLabelFileExtMap.Remove(orgFileExt); } _deleteLabelFileExtMap[orgFileExt] = orgFileExt; if (_deleteLabelFileExtMap.Contains(setFileExt)) { _deleteLabelFileExtMap.Remove(setFileExt); } _deleteLabelFileExtMap[setFileExt] = setFileExt; // protectFileExtMap if (_protectFileExtMap.Contains(orgFileExt)) { _protectFileExtMap.Remove(orgFileExt); } _protectFileExtMap[orgFileExt] = setFileExt; // deleteProtectFileExtMap if (_deleteProtectFileExtMap.Contains(setFileExt)) { _deleteProtectFileExtMap.Remove(setFileExt); } _deleteProtectFileExtMap[setFileExt] = orgFileExt; } } } public string GetDispFileName(string dispFileName, string outputFileName) { return Path.GetFileNameWithoutExtension(dispFileName) + Path.GetExtension(outputFileName); } public string GetRequestFileName(string realFileName) { return _aipSetting.SourceFileDir + realFileName; } public string GetActualFileName(string fileName) { return _aipSetting.TargetFileDir + fileName; } public string? GetSupportedFileType(string fileName) { string fileExt = Path.GetExtension(fileName).ToLower(); return _supportedFileExtMap[fileExt] as string; } //public string? GetSetLabelFileExt(string reqFileName) //{ // string fileExt = Path.GetExtension(reqFileName).ToLower(); // return _labelFileExtMap[fileExt] as string; //} //public string? GetSetProtectFileExt(string reqFileName) //{ // string fileExt = Path.GetExtension(reqFileName).ToLower(); // return _protectFileExtMap[fileExt] as string; //} //public string? GetDeleteLabelFileExt(string fileName) //{ // string fileExt = Path.GetExtension(fileName).ToLower(); // return _deleteProtectFileExtMap[fileExt] as string; //} //public string? GetDeleteProtectFileExt(string fileName) //{ // string fileExt = Path.GetExtension(fileName).ToLower(); // return _deleteProtectFileExtMap[fileExt] as string; //} public AipSettings GetAipSettings() { return _aipSetting; } public int DownloadAipFileInformations() { DownloadAipFileLabels(); DownloadAipFilePolicies(); DownloadAipFileProtections(); return _aipLableMap.Count + _aipPolicyMap.Count + _aipProtectionMap.Count; } private void DownloadAipFileLabels() { //using var transaction = _dbContext.Database.BeginTransaction(); try { Stopwatch sw = Stopwatch.StartNew(); sw.Start(); _log.LogInformation("*** AipFileService.DownloadAipFileLabels: Start."); Hashtable keyMap = new Hashtable(); List newLabels = new List(); List updLabels = new List(); Hashtable labelMap = new Hashtable(); List? lavels = _aipFileManager.SensitivityLabels(); _log.LogInformation("AipFileService.DownloadAipFileLabels.SensitivityLabels(): {0} EA.", lavels?.Count); if (lavels == null) { return; } var result = _repo.LoadAipLabels().Result; if (result != null) { foreach (TbAipLabel label in result) { labelMap.Add(label.LabelGuid, label); } } foreach (AipLabel label in lavels) { keyMap.Add(label.Id, label); if (labelMap.ContainsKey(label.Id)) { TbAipLabel? orgLabel = labelMap[label.Id] as TbAipLabel; if (orgLabel != null) { if (orgLabel.IsChanged(label)) { _log.LogInformation("변경된 레벨 데이터: {0}", label.Id); orgLabel.LabelName = label.Name; orgLabel.LabelDesc = label.Description; updLabels.Add(orgLabel); } else { _log.LogInformation("동일한 레벨 데이터: {0}", label.Id); } } else { _log.LogInformation("Not Found Label In Map: {0}", label.Id); } } else { _log.LogInformation("새로운 레벨 데이터: {0}", label.Id); TbAipLabel obj = new TbAipLabel() { LabelGuid = label.Id, LabelName = label.Name, LabelDesc = label.Description, CreatedAt = DateTime.Now, DeletedAt = null, UseYn = true, }; newLabels.Add(obj); } } _aipLableMap = keyMap; _log.LogInformation("AipFileService.DownloadAipFileLabels: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count); int updateCount = _repo.UpdateAipLables(updLabels).Result; int insertCount = _repo.InsertAipLables(newLabels).Result; sw.Stop(); _log.LogInformation("*** AipFileService.DownloadAipFileLabels: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.", sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount); } catch (Exception ex) { _log.LogError($"*** AipFileService.DownloadAipFileLabels: {ex}"); } } private void DownloadAipFilePolicies() { //using var transaction = _dbContext.Database.BeginTransaction(); try { Stopwatch sw = Stopwatch.StartNew(); sw.Start(); _log.LogInformation("*** AipFileService.DownloadAipFilePolicies: Start."); Hashtable keyMap = new Hashtable(); List newLabels = new List(); List updLabels = new List(); Hashtable labelMap = new Hashtable(); List? lavels = _aipFileManager.ListSensitivityLabels(); _log.LogInformation("AipFileService.DownloadAipFilePolicies.ListSensitivityLabels(): {0} EA.", lavels?.Count); if (lavels == null) { return; } var result = _repo.LoadAipPolicies().Result; if (result != null) { foreach (TbAipPolicy policy in result) { labelMap.Add(policy.PolicyGuid, policy); } } foreach (AipLabel label in lavels) { keyMap.Add(label.Id, label); if (labelMap.ContainsKey(label.Id)) { TbAipPolicy? orgPolicy = labelMap[label.Id] as TbAipPolicy; if (orgPolicy != null) { if (orgPolicy.IsChanged(label)) { _log.LogInformation("변경된 정책 데이터: {0}", label.Id); orgPolicy.PolicyName = label.Name; orgPolicy.PolicyDesc = label.Description; updLabels.Add(orgPolicy); } else { _log.LogInformation("동일한 정책 데이터: {0}", label.Id); } } else { _log.LogInformation("Not Found Policy In Map: {0}", label.Id); } } else { _log.LogInformation("새로운 정책 데이터: {0}", label.Id); TbAipPolicy obj = new TbAipPolicy() { PolicyGuid = label.Id, PolicyName = label.Name, PolicyDesc = label.Description, CreatedAt = DateTime.Now, DeletedAt = null, UseYn = true, }; newLabels.Add(obj); } } _aipPolicyMap = keyMap; _log.LogInformation("AipFileService.DownloadAipFilePolicies: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count); int updateCount = _repo.UpdateAipPolicies(updLabels).Result; int insertCount = _repo.InsertAipPolicies(newLabels).Result; sw.Stop(); _log.LogInformation("*** AipFileService.DownloadAipFilePolicies: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.", sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount); } catch (Exception ex) { _log.LogError($"*** AipFileService.DownloadAipFilePolicies: {ex}"); } } private void DownloadAipFileProtections() { //using var transaction = _dbContext.Database.BeginTransaction(); try { Stopwatch sw = Stopwatch.StartNew(); sw.Start(); _log.LogInformation("*** AipFileService.DownloadAipFileProtections: Start."); Hashtable keyMap = new Hashtable(); List newLabels = new List(); List updLabels = new List(); Hashtable labelMap = new Hashtable(); List? templates = _aipFileManager.GetTemplates(); _log.LogInformation("AipFileService.DownloadAipFileProtections.GetTemplates(): {0} EA.", templates?.Count); if (templates == null) { return; } var result = _repo.LoadAipTemplates().Result; if (result != null) { foreach (TbAipProtection policy in result) { labelMap.Add(policy.ProtectionGuid, policy); } } foreach (AipTemplate template in templates) { keyMap.Add(template.Id, template); if (labelMap.ContainsKey(template.Id)) { TbAipProtection? orgProtection = labelMap[template.Id] as TbAipProtection; if (orgProtection != null) { if (orgProtection.IsChanged(template)) { _log.LogInformation("변경된 보호 데이터: {0}", template.Id); orgProtection.ProtectionName = template.Name; orgProtection.ProtectionDesc = template.Description; updLabels.Add(orgProtection); } else { _log.LogInformation("동일한 보호 데이터: {0}", template.Id); } } else { _log.LogInformation("Not Found Policy In Map: {0}", template.Id); } } else { _log.LogInformation("새로운 보호 데이터: {0}", template.Id); TbAipProtection obj = new TbAipProtection() { ProtectionGuid = template.Id, ProtectionName = template.Name, ProtectionDesc = template.Description, CreatedAt = DateTime.Now, DeletedAt = null, UseYn = true, }; newLabels.Add(obj); } } _aipProtectionMap = keyMap; _log.LogInformation("AipFileService.DownloadAipFileProtections: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count); int updateCount = _repo.UpdateAipTemplates(updLabels).Result; int insertCount = _repo.InsertAipTemplates(newLabels).Result; sw.Stop(); _log.LogInformation("*** AipFileService.DownloadAipFileProtections: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.", sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount); } catch (Exception ex) { _log.LogError($"*** AipFileService.DownloadAipFileProtections: {ex}"); } } } }