BaseService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using AipGateway.API.Application;
  2. using AipGateway.API.Application.Utils;
  3. using AipGateway.API.Domain.Models.App;
  4. using AipGateway.API.Domain.Models.Response;
  5. using AipGateway.AIP.Service.Services.Interfaces;
  6. using AipGateway.AIP.Service.Utils;
  7. namespace AipGateway.AIP.Service.Services
  8. {
  9. public class BaseService
  10. {
  11. protected readonly IAipFileService _aipFileService;
  12. public BaseService(IAipFileService aipFileService)
  13. {
  14. _aipFileService = aipFileService;
  15. }
  16. protected CheckAip CheckAipRequest(string apiGuid, string dispFileName, string realFileName, bool isStream, int aipJobType)
  17. {
  18. string reqFileName = "";
  19. if (isStream)
  20. {
  21. reqFileName = _aipFileService.GetRequestFileName(apiGuid + AipFileUtils.getExtension(dispFileName));
  22. }
  23. else
  24. {
  25. reqFileName = _aipFileService.GetRequestFileName(realFileName);
  26. if (!AipFileUtils.isExists(reqFileName))
  27. {
  28. return new CheckAip(101, "파일이 존재하지 않습니다.", dispFileName);
  29. }
  30. }
  31. string? newFileExt = _aipFileService.GetSupportedFileType(reqFileName);
  32. if (newFileExt == null)
  33. {
  34. return new CheckAip(102, "지원하지 않는 파일형식 입니다.", dispFileName);
  35. }
  36. CheckAip result = new CheckAip(0, GlobalConstants.API_RESULT_SUCCESS, dispFileName);
  37. result.reqFileName = reqFileName;
  38. result.newFileExt = newFileExt;
  39. if (isStream)
  40. {
  41. result.dispFileName = AipFileUtils.getFileNameWithoutExtension(dispFileName) + newFileExt;
  42. }
  43. else
  44. {
  45. result.dispFileName = AipFileUtils.getFileNameWithoutExtension(realFileName) + newFileExt;
  46. }
  47. result.actualFileName = _aipFileService.GetActualFileName(AipFileUtils.getFileNameWithoutExtension(realFileName) + newFileExt);
  48. //result.actualFileName = _aipFileService.GetActualFileName(apiGuid + newFileExt);
  49. return result;
  50. }
  51. protected ResponseFile ResponseFileException(Exception exception, string displayFileName)
  52. {
  53. if (exception is AipFileException)
  54. {
  55. AipFileException aipFileException = (AipFileException)exception;
  56. return new ResponseFile
  57. {
  58. errorCode = aipFileException.ErrorCode,
  59. errorMessage = aipFileException.Message,
  60. dispFileName = displayFileName,
  61. };
  62. }
  63. else
  64. {
  65. return new ResponseFile
  66. {
  67. errorCode = 109,
  68. errorMessage = exception.Message ?? "파일 작업 중에 시스템 오류가 발생했습니다.",
  69. dispFileName = displayFileName,
  70. };
  71. }
  72. }
  73. protected ResponseStream ResponseStreamException(Exception exception, string displayFileName)
  74. {
  75. if (exception is AipFileException)
  76. {
  77. AipFileException aipFileException = (AipFileException)exception;
  78. return new ResponseStream
  79. {
  80. errorCode = aipFileException.ErrorCode,
  81. errorMessage = aipFileException.Message,
  82. dispFileName = displayFileName,
  83. };
  84. }
  85. else
  86. {
  87. return new ResponseStream
  88. {
  89. errorCode = 109,
  90. errorMessage = exception.Message ?? "파일 스트림 작업 중에 시스템 오류가 발생했습니다.",
  91. dispFileName = displayFileName,
  92. };
  93. }
  94. }
  95. protected ResponseInfo ResponseInfoException(Exception exception)
  96. {
  97. if (exception is AipFileException)
  98. {
  99. AipFileException aipFileException = (AipFileException)exception;
  100. return new ResponseInfo
  101. {
  102. errorCode = aipFileException.ErrorCode,
  103. errorMessage = aipFileException.Message,
  104. };
  105. }
  106. else
  107. {
  108. return new ResponseInfo
  109. {
  110. errorCode = 109,
  111. errorMessage = exception.Message ?? "작업 중에 시스템 오류가 발생했습니다."
  112. };
  113. }
  114. }
  115. protected ResponseInfo ResponseGetFileInfo(AipFileInfo fileInfo, string displayFileName)
  116. {
  117. bool isLabled = false;
  118. bool isProtected = false;
  119. DateTime creationTime = DateTime.Now;
  120. string contentId = "";
  121. string owner = "";
  122. string labelId = "";
  123. string labelName = "";
  124. string templateId = "";
  125. string templateName = "";
  126. long fileSize = 0;
  127. if (fileInfo != null)
  128. {
  129. fileSize = fileInfo.FileSize;
  130. if (fileInfo.Content != null)
  131. {
  132. creationTime = fileInfo.Content.CreationTime;
  133. }
  134. if (fileInfo.Label != null)
  135. {
  136. isLabled = true;
  137. labelId = fileInfo.Label.Id;
  138. labelName = fileInfo.Label.Name;
  139. }
  140. if (fileInfo.Protection != null)
  141. {
  142. owner = fileInfo.Protection.Owner;
  143. contentId = fileInfo.Protection.ContentId;
  144. if (fileInfo.Protection.ProtectionDescriptor != null)
  145. {
  146. isProtected = true;
  147. templateId = fileInfo.Protection.ProtectionDescriptor.TemplateId;
  148. templateName = fileInfo.Protection.ProtectionDescriptor.Name;
  149. }
  150. }
  151. }
  152. return new ResponseInfo
  153. {
  154. errorCode = 0,
  155. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  156. isLabled = isLabled,
  157. isProtected = isProtected,
  158. creationTime = creationTime,
  159. contentId = contentId,
  160. owner = owner,
  161. labelId = labelId,
  162. labelName = labelName,
  163. templateId = templateId,
  164. templateName = templateName,
  165. dispFileName = displayFileName,
  166. FileLabelGuid = labelId,
  167. FileProtectionGuid = templateId,
  168. FileOwner = owner,
  169. FileExt = Path.GetExtension(displayFileName),
  170. FileSize = fileSize,
  171. };
  172. }
  173. protected ResponseBase ResponseBase(SetFileInfo fileInfo, string displayFileName, string realFileName, string actualFileName)
  174. {
  175. ResponseBase result = new ResponseBase
  176. {
  177. errorCode = fileInfo.errorNo,
  178. errorMessage = fileInfo.errorMsg,
  179. dispFileName = displayFileName,
  180. realFileName = realFileName,
  181. };
  182. result.FileExt = AipFileUtils.getExtension(displayFileName);
  183. result.FileOwner = fileInfo.fileOwner;
  184. result.FileSize = fileInfo.fileSize;
  185. result.FileLabelGuid = fileInfo.labelGuid;
  186. result.FileProtectionGuid = fileInfo.templateGuid;
  187. result.NewFileName = displayFileName;
  188. result.NewFileOwner = fileInfo.newFileOwner;
  189. result.NewFileExt = AipFileUtils.getExtension(actualFileName);
  190. result.NewFileSize = fileInfo.newFileSize;
  191. result.NewFileLabelGuid = fileInfo.newFileLabelGuid;
  192. result.NewFileProtectionGuid = fileInfo.newFileTemplateGuid;
  193. return result;
  194. }
  195. protected ResponseFile ResponseSetFile(SetFileInfo fileInfo, string displayFileName, string realFileName, string actualFileName)
  196. {
  197. ResponseBase baseResponse = ResponseBase(fileInfo, displayFileName, realFileName, actualFileName);
  198. ResponseFile result = new ResponseFile
  199. {
  200. errorCode = baseResponse.errorCode,
  201. errorMessage = baseResponse.errorMessage,
  202. dispFileName = _aipFileService.GetDispFileName(baseResponse.dispFileName, actualFileName),
  203. FileId = baseResponse.FileId,
  204. FileName = baseResponse.FileName,
  205. FileLabelGuid = baseResponse.FileLabelGuid,
  206. FileProtectionGuid = baseResponse.FileProtectionGuid,
  207. FileOwner = baseResponse.FileOwner,
  208. FileExt = baseResponse.FileExt,
  209. FileSize = baseResponse.FileSize,
  210. NewFileName = baseResponse.NewFileName,
  211. NewFileLabelGuid = baseResponse.NewFileLabelGuid,
  212. NewFileProtectionGuid = baseResponse.NewFileProtectionGuid,
  213. NewFileOwner = baseResponse.NewFileOwner,
  214. NewFileExt = baseResponse.NewFileExt,
  215. NewFileSize = baseResponse.NewFileSize
  216. };
  217. result.realFileName = realFileName;
  218. result.actualFileName = AipFileUtils.getFileName(actualFileName);
  219. return result;
  220. }
  221. protected ResponseStream ResponseSetStream(SetFileInfo fileInfo, string displayFileName, string actualFileName)
  222. {
  223. ResponseBase baseResponse = ResponseBase(fileInfo, displayFileName, "", actualFileName);
  224. ResponseStream result = new ResponseStream
  225. {
  226. errorCode = baseResponse.errorCode,
  227. errorMessage = baseResponse.errorMessage,
  228. dispFileName = _aipFileService.GetDispFileName(baseResponse.dispFileName, actualFileName),
  229. FileId = baseResponse.FileId,
  230. FileName = baseResponse.FileName,
  231. FileLabelGuid = baseResponse.FileLabelGuid,
  232. FileProtectionGuid = baseResponse.FileProtectionGuid,
  233. FileOwner = baseResponse.FileOwner,
  234. FileExt = baseResponse.FileExt,
  235. FileSize = baseResponse.FileSize,
  236. NewFileName = baseResponse.NewFileName,
  237. NewFileLabelGuid = baseResponse.NewFileLabelGuid,
  238. NewFileProtectionGuid = baseResponse.NewFileProtectionGuid,
  239. NewFileOwner = baseResponse.NewFileOwner,
  240. NewFileExt = baseResponse.NewFileExt,
  241. NewFileSize = baseResponse.NewFileSize
  242. };
  243. result.outputFileName = AipFileUtils.getFileName(actualFileName);
  244. result.fileData = FileUtils.GetFileData(_aipFileService.GetActualFileName(actualFileName));
  245. return result;
  246. }
  247. public CheckMultipartFile ValidateMultipartFile(IFormFile file, string apiGuid)
  248. {
  249. CheckMultipartFile result = new CheckMultipartFile
  250. {
  251. errorCode = 0,
  252. };
  253. string dispFileName = file.FileName;
  254. if (dispFileName == null)
  255. {
  256. result.errorCode = 81;
  257. result.errorMessage = "작업 할 파일 이름을 알 수가 없습니다.";
  258. result.dispFileName = "UnknownFileName";
  259. return result;
  260. }
  261. if (dispFileName.Contains(".."))
  262. {
  263. result.errorCode = 82;
  264. result.errorMessage = "파일 이름에 유효 하지 않은 경로가 포함 되어 있습니다.";
  265. return result;
  266. }
  267. if (file.Length <= 0)
  268. {
  269. result.errorCode = 83;
  270. result.errorMessage = "파일의 내용이 없습니다.";
  271. return result;
  272. }
  273. string saveFileName = apiGuid + AipFileUtils.getExtension(dispFileName);
  274. string fileStorageLocation = _aipFileService.GetRequestFileName(saveFileName);
  275. result.dispFileName = dispFileName;
  276. result.saveFileName = saveFileName;
  277. try
  278. {
  279. using (var stream = System.IO.File.Create(fileStorageLocation))
  280. {
  281. file.CopyTo(stream);
  282. }
  283. }
  284. catch (IOException)
  285. {
  286. result.errorCode = 84;
  287. result.errorMessage = "파일 저장 중에 오류가 발생하였습니다.";
  288. return result;
  289. }
  290. return result;
  291. }
  292. }
  293. }