markus / ConvertService / ServiceBase / Markus.Service.Convert / ConvertService.cs @ 77cdac33
이력 | 보기 | 이력해설 | 다운로드 (25.5 KB)
1 | 53c9637d | taeseongkim | using log4net; |
---|---|---|---|
2 | using Markus.Message; |
||
3 | using Markus.Service.Helper; |
||
4 | using Markus.Service.Interface; |
||
5 | using System; |
||
6 | using System.Collections.Generic; |
||
7 | using System.IO; |
||
8 | using System.Linq; |
||
9 | using System.Net.Http; |
||
10 | using System.ServiceModel; |
||
11 | using System.Text; |
||
12 | using Markus.Service.Extensions; |
||
13 | 06f13e11 | taeseongkim | using Markus.Service.WcfClient.StationServiceAsync; |
14 | 53c9637d | taeseongkim | |
15 | using System.Threading.Tasks; |
||
16 | using System.Web; |
||
17 | 77cdac33 | taeseongkim | using RestSharp; |
18 | 53c9637d | taeseongkim | |
19 | namespace Markus.Service.Convert |
||
20 | { |
||
21 | public class ConvertService : IDisposable |
||
22 | { |
||
23 | // 1GigaBytes |
||
24 | protected ILog logger; |
||
25 | |||
26 | readonly ProcessContext ConvertProcessContext; |
||
27 | private string gTempFileName; |
||
28 | private Markus.SaveTask gSaveTask; |
||
29 | private Markus.MarkusPDF gMarkusPDF; |
||
30 | f363a40e | taeseongkim | |
31 | // 컨버터 완료시 파일갯수를 체크 하여 틀리면 reconvert를 1로 하고 다시 컨버팅 하도록 한다. |
||
32 | private int ReConvert; |
||
33 | |||
34 | 53c9637d | taeseongkim | |
35 | 06f13e11 | taeseongkim | private StationServiceClient StationServiceClient; |
36 | 53c9637d | taeseongkim | |
37 | a53dfe45 | taeseongkim | /// <summary> |
38 | /// 프로세스 초기화 |
||
39 | /// </summary> |
||
40 | /// <param name="convertContext"></param> |
||
41 | 53c9637d | taeseongkim | public ConvertService(ProcessContext convertContext) : base() |
42 | { |
||
43 | logger = LogManager.GetLogger(typeof(ConvertService)); |
||
44 | |||
45 | ConvertProcessContext = convertContext; |
||
46 | |||
47 | BasicHttpBinding myBinding = new BasicHttpBinding(); |
||
48 | EndpointAddress myEndpoint = new EndpointAddress(UriHelper.UriCreate(ConvertProcessContext.ServiceStationUri)); |
||
49 | 06f13e11 | taeseongkim | StationServiceClient = new StationServiceClient(myBinding, myEndpoint); |
50 | 53c9637d | taeseongkim | |
51 | gMarkusPDF = new MarkusPDF(); |
||
52 | gSaveTask = new SaveTask(); |
||
53 | gSaveTask.StateChangeEvent += MarkusPdfStateChangeEvent; |
||
54 | } |
||
55 | |||
56 | public ConvertService() |
||
57 | { |
||
58 | } |
||
59 | |||
60 | public void Dispose() |
||
61 | { |
||
62 | try |
||
63 | { |
||
64 | 65fbe3cb | taeseongkim | if(gMarkusPDF != null) |
65 | { |
||
66 | gMarkusPDF.Dispose(); |
||
67 | gMarkusPDF = null; |
||
68 | } |
||
69 | |||
70 | if (gSaveTask != null) |
||
71 | { |
||
72 | gSaveTask.StateChangeEvent -= MarkusPdfStateChangeEvent; |
||
73 | gSaveTask.Dispose(); |
||
74 | gSaveTask = null; |
||
75 | } |
||
76 | 53c9637d | taeseongkim | |
77 | //if (System.IO.File.Exists(gTempFileName)) |
||
78 | //{ |
||
79 | // File.Delete(gTempFileName); |
||
80 | //} |
||
81 | |||
82 | } |
||
83 | catch (Exception ex) |
||
84 | { |
||
85 | 65fbe3cb | taeseongkim | logger.Error("Convert Service Dispose Error", ex); |
86 | 53c9637d | taeseongkim | } |
87 | finally |
||
88 | { |
||
89 | GC.Collect(2); |
||
90 | GC.Collect(2); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /// <summary> |
||
95 | /// markus lib에서 받는 이벤트 |
||
96 | /// </summary> |
||
97 | /// <param name="sender"></param> |
||
98 | /// <param name="e"></param> |
||
99 | private void MarkusPdfStateChangeEvent(object sender, Markus.Message.StateEventArgs e) |
||
100 | { |
||
101 | try |
||
102 | { |
||
103 | int currentPage = e.SaveItem.CurrentPage; |
||
104 | |||
105 | if (e.SaveItem.CurrentPage == 0) /// 초기 wait status인 경우 0일수 있다. %계산시 오류 방지 |
||
106 | { |
||
107 | currentPage = 1; |
||
108 | } |
||
109 | |||
110 | switch (e.SaveItem.Status) |
||
111 | { |
||
112 | case StatusCodeType.None: |
||
113 | break; |
||
114 | case StatusCodeType.Wait: |
||
115 | break; |
||
116 | case StatusCodeType.PageLoading: |
||
117 | break; |
||
118 | case StatusCodeType.Saving: |
||
119 | break; |
||
120 | case StatusCodeType.Completed: |
||
121 | break; |
||
122 | case StatusCodeType.FileError: |
||
123 | break; |
||
124 | case StatusCodeType.PageError: |
||
125 | break; |
||
126 | case StatusCodeType.NeedsPassword: |
||
127 | break; |
||
128 | case StatusCodeType.Error: |
||
129 | break; |
||
130 | default: |
||
131 | break; |
||
132 | } |
||
133 | a0341bef | taeseongkim | |
134 | StationServiceClient.ConvertProcessStateAsync(e.SaveItem.Id, (int)e.SaveItem.Status, e.SaveItem.CurrentPage, e.SaveItem.TotalPages, e.SaveItem.ErrorMessage); |
||
135 | |||
136 | 53c9637d | taeseongkim | } |
137 | catch (Exception ex) |
||
138 | { |
||
139 | logger.Error($"Status Change Error", ex); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | [System.Security.Permissions.FileIOPermission(System.Security.Permissions.SecurityAction.LinkDemand)] |
||
144 | public async Task<SaveResult> SetFileAsync() |
||
145 | { |
||
146 | SaveResult result = new SaveResult(); |
||
147 | |||
148 | SaveItem saveitem = new SaveItem |
||
149 | { |
||
150 | Id = ConvertProcessContext.ConvertID, |
||
151 | PdfFilePath = ConvertProcessContext.OriginFilePath, |
||
152 | SavePath = ConvertProcessContext.SaveDirectory, |
||
153 | Status = StatusCodeType.None, |
||
154 | 2091a7e5 | taeseongkim | MinimumFontSize = ConvertProcessContext.MinFontSize, |
155 | UseResolution = ConvertProcessContext.UseResolution |
||
156 | 53c9637d | taeseongkim | }; |
157 | |||
158 | try |
||
159 | { |
||
160 | StationServiceClient.ConvertProcessState(ConvertProcessContext.ConvertID, (int)StatusCodeType.Wait, 0, 0, ""); |
||
161 | |||
162 | result = await ConvertAsync(saveitem); |
||
163 | |||
164 | db0d3db3 | taeseongkim | // 플러그인 실행 |
165 | PluginService.Run(saveitem.Id); |
||
166 | 7b095cc3 | taeseongkim | |
167 | this.Dispose(); |
||
168 | |||
169 | 53c9637d | taeseongkim | StationServiceClient.ConvertFinish(saveitem.Id, (int)result.StatusCode, saveitem.CurrentPage, saveitem.TotalPages,result.Message); |
170 | } |
||
171 | catch (Exception ex) |
||
172 | { |
||
173 | db0d3db3 | taeseongkim | result.StatusCode = StatusCodeType.Error; |
174 | 53c9637d | taeseongkim | logger.Error($"File Convert Error",ex); |
175 | db0d3db3 | taeseongkim | StationServiceClient.ConvertFinish(saveitem.Id, (int)result.StatusCode, saveitem.CurrentPage, saveitem.TotalPages, $"ConvertService Error {saveitem.Id} {ex.Message} {ex.InnerException?.ToString()}"); |
176 | 53c9637d | taeseongkim | } |
177 | |||
178 | return result; |
||
179 | } |
||
180 | |||
181 | 65fbe3cb | taeseongkim | [System.Security.Permissions.FileIOPermission(System.Security.Permissions.SecurityAction.LinkDemand)] |
182 | 53c9637d | taeseongkim | private async Task<SaveResult> ConvertAsync(SaveItem saveitem) |
183 | { |
||
184 | SaveResult result = new SaveResult(); |
||
185 | |||
186 | try |
||
187 | { |
||
188 | DateTime deleteTime = DateTime.Now; |
||
189 | |||
190 | while (System.IO.Directory.Exists(saveitem.SavePath)) |
||
191 | { |
||
192 | 7b095cc3 | taeseongkim | try |
193 | { |
||
194 | System.IO.Directory.Delete(saveitem.SavePath, true); |
||
195 | } |
||
196 | catch (Exception) |
||
197 | { |
||
198 | |||
199 | } |
||
200 | 53c9637d | taeseongkim | |
201 | if ((DateTime.Now - deleteTime) > new TimeSpan(0, 5, 0)) |
||
202 | { |
||
203 | string msg = $"ConvertService Error {saveitem.SavePath} Delete Error"; |
||
204 | logger.Error(msg); |
||
205 | result.StatusCode = StatusCodeType.FileError; |
||
206 | c5519c44 | taeseongkim | result.Message = LogHelper.GetStack() + " " +LogHelper.GetStack() + " " + msg; |
207 | 53c9637d | taeseongkim | |
208 | return result; |
||
209 | } |
||
210 | |||
211 | 60723dc9 | taeseongkim | System.Threading.Thread.SpinWait(10); |
212 | 53c9637d | taeseongkim | } |
213 | |||
214 | System.IO.Directory.CreateDirectory(saveitem.SavePath); |
||
215 | |||
216 | //파일 다운로드 |
||
217 | if (await DownloadFileAsync(saveitem)) |
||
218 | { |
||
219 | gTempFileName = saveitem.PdfFilePath; |
||
220 | |||
221 | bae83c92 | taeseongkim | if(gTempFileName.ToCharArray().Any(x=>x.IsKor())) |
222 | { |
||
223 | gTempFileName = Path.Combine(Path.GetTempPath(), GuidExtension.shortGuid() + ".pdf"); |
||
224 | |||
225 | File.Copy(saveitem.PdfFilePath, gTempFileName,true); |
||
226 | |||
227 | System.Threading.Thread.Sleep(100); |
||
228 | } |
||
229 | |||
230 | gMarkusPDF.pdfLoad(gTempFileName, saveitem.MinimumFontSize, saveitem.UseResolution); |
||
231 | 53c9637d | taeseongkim | |
232 | 504c5aa1 | taeseongkim | if(gMarkusPDF.PageCount() > 0) |
233 | 53c9637d | taeseongkim | { |
234 | 504c5aa1 | taeseongkim | /// 설정된 MultiThreadMaxPages에 따른 컨버터 분기 |
235 | e77fc685 | taeseongkim | //if (gMarkusPDF.PageCount() > ConvertProcessContext.MultiThreadMaxPages) |
236 | //{ |
||
237 | // // 큰 사이즈의 파일 컨버팅 |
||
238 | result = ConvertBigFileProcess(saveitem); |
||
239 | //} |
||
240 | //else |
||
241 | //{ |
||
242 | // /// 작은 사이즈의 컨버팅 |
||
243 | // await Task.Factory.StartNew(new Action(() => |
||
244 | // { |
||
245 | // result = gSaveTask.SaveFile(saveitem); |
||
246 | // }), TaskCreationOptions.LongRunning); |
||
247 | //} |
||
248 | 53c9637d | taeseongkim | |
249 | 504c5aa1 | taeseongkim | // 파일 체크 후 갯수가 안맞으면 다시 컨버팅한다. |
250 | if (ReConvert < 1 && (result.PageInfoList.Count() != saveitem.TotalPages |
||
251 | || Directory.EnumerateFiles(saveitem.SavePath, "*.png").Count() != saveitem.TotalPages |
||
252 | || Directory.EnumerateFiles(saveitem.SavePath, "*.jpg").Count() != saveitem.TotalPages)) |
||
253 | { |
||
254 | result.StatusCode = StatusCodeType.PageError; |
||
255 | result.Message = LogHelper.GetStack() + " " +"Page Count Error"; |
||
256 | ReConvert = 1; |
||
257 | f363a40e | taeseongkim | |
258 | 504c5aa1 | taeseongkim | result = ConvertBigFileProcess(saveitem); |
259 | } |
||
260 | f363a40e | taeseongkim | |
261 | 53c9637d | taeseongkim | |
262 | 504c5aa1 | taeseongkim | /// 페이지 정보 저장 |
263 | if (result.PageInfoList?.Count() > 0) |
||
264 | 53c9637d | taeseongkim | { |
265 | 504c5aa1 | taeseongkim | bool docSetResult = false; |
266 | 53c9637d | taeseongkim | |
267 | 504c5aa1 | taeseongkim | using (Markus.Service.DataBase.ConvertDatabase database = new Markus.Service.DataBase.ConvertDatabase(ConvertProcessContext.ConnectionString)) |
268 | 53c9637d | taeseongkim | { |
269 | 504c5aa1 | taeseongkim | List<EntityModel.DOCPAGE> docPageList = new List<EntityModel.DOCPAGE>(); |
270 | 53c9637d | taeseongkim | |
271 | 504c5aa1 | taeseongkim | docPageList = result.PageInfoList.Select(f => new EntityModel.DOCPAGE |
272 | { |
||
273 | ID = GuidExtension.shortGuid(), |
||
274 | PAGE_WIDTH = f.Width.ToString(), |
||
275 | PAGE_HEIGHT = f.Height.ToString(), |
||
276 | PAGE_NUMBER = f.PageNo |
||
277 | }).ToList(); |
||
278 | 53c9637d | taeseongkim | |
279 | 504c5aa1 | taeseongkim | docSetResult = database.SetDocumentInfo(saveitem.Id, result.PageInfoList.Count, docPageList); |
280 | } |
||
281 | |||
282 | if (docSetResult) |
||
283 | { |
||
284 | result.StatusCode = StatusCodeType.Completed; |
||
285 | } |
||
286 | else |
||
287 | { |
||
288 | result.StatusCode = StatusCodeType.FileError; |
||
289 | result.Message = LogHelper.GetStack() + " " + $"Doc Set Error. {result.Message}"; |
||
290 | } |
||
291 | 53c9637d | taeseongkim | } |
292 | else |
||
293 | { |
||
294 | result.StatusCode = StatusCodeType.FileError; |
||
295 | 504c5aa1 | taeseongkim | result.Message = LogHelper.GetStack() + " " + $"Page List Get Error. {result.Message} "; |
296 | 53c9637d | taeseongkim | } |
297 | } |
||
298 | else |
||
299 | { |
||
300 | 504c5aa1 | taeseongkim | result.Message = $"File Error Page Count:0 - File path : { saveitem.PdfFilePath}"; |
301 | 53c9637d | taeseongkim | result.StatusCode = StatusCodeType.FileError; |
302 | } |
||
303 | 504c5aa1 | taeseongkim | |
304 | 53c9637d | taeseongkim | } |
305 | else |
||
306 | { |
||
307 | result.Message = $"File Download Error - File path : { saveitem.PdfFilePath}"; |
||
308 | result.StatusCode = StatusCodeType.FileError; |
||
309 | } |
||
310 | } |
||
311 | catch (Exception ex) |
||
312 | { |
||
313 | c5519c44 | taeseongkim | result.Message = LogHelper.GetStack() + " " +"ConvertService Convert Error" + ex.Message; |
314 | 53c9637d | taeseongkim | result.StatusCode = StatusCodeType.Error; |
315 | a2a64028 | taeseongkim | |
316 | logger.Error(ex); |
||
317 | 53c9637d | taeseongkim | } |
318 | finally |
||
319 | { |
||
320 | |||
321 | } |
||
322 | |||
323 | return result; |
||
324 | } |
||
325 | |||
326 | /// <summary> |
||
327 | /// 파일 다운로드 |
||
328 | /// </summary> |
||
329 | /// <param name="saveItem"></param> |
||
330 | /// <returns></returns> |
||
331 | af9bffc5 | taeseongkim | public async Task<bool> DownloadFileAsync(SaveItem saveItem) |
332 | 53c9637d | taeseongkim | { |
333 | bool result = false; |
||
334 | |||
335 | try |
||
336 | { |
||
337 | Uri pdfFileUri = null; |
||
338 | |||
339 | //if (saveItem.PdfFilePath.Contains("VPCS_DOCLIB")) |
||
340 | //{ |
||
341 | // FileName = DocUri.Remove(0, DocUri.LastIndexOf("/") + 1); |
||
342 | // ProjectFolderPath = DownloadDir_PDF + "\\" + ConverterItem.PROJECT_NO + "_Tile"; //프로젝트 폴더 |
||
343 | // ItemListPath = ProjectFolderPath + "\\" + (System.Convert.ToInt64(ConverterItem.DOCUMENT_ID) / 100).ToString(); |
||
344 | // ItemPath = ItemListPath + "\\" + ConverterItem.DOCUMENT_ID; |
||
345 | // DownloadFilePath = ItemPath + "\\" + FileName; |
||
346 | //} |
||
347 | //else |
||
348 | //{ |
||
349 | // ProjectFolderPath = DownloadDir_PDF + "\\" + ConverterItem.PROJECT_NO + "_Tile"; //프로젝트 폴더 |
||
350 | // ItemListPath = ProjectFolderPath + "\\" + (System.Convert.ToInt64(ConverterItem.DOCUMENT_ID) / 100).ToString(); |
||
351 | // ItemPath = ItemListPath + "\\" + ConverterItem.DOCUMENT_ID; |
||
352 | // FileName = HttpUtility.ParseQueryString(new Uri(DocUri).Query).Get("fileName"); |
||
353 | // DownloadFilePath = string.IsNullOrWhiteSpace(FileName) ? ItemPath + "\\" + FileName : ItemPath + "\\" + FileName; |
||
354 | |||
355 | //} |
||
356 | |||
357 | saveItem.PdfFilePath = HttpUtility.UrlDecode(saveItem.PdfFilePath); //PDF 전체 경로 |
||
358 | |||
359 | 60723dc9 | taeseongkim | |
360 | string FileName = ""; |
||
361 | string downloadFilePath = ""; |
||
362 | 53c9637d | taeseongkim | |
363 | // 드라이브 경로가 포함되었는지 판단. |
||
364 | if (Path.IsPathRooted(saveItem.PdfFilePath)) |
||
365 | { |
||
366 | 60723dc9 | taeseongkim | FileInfo file = new FileInfo(saveItem.PdfFilePath); |
367 | |||
368 | if (file.Exists) |
||
369 | 53c9637d | taeseongkim | { |
370 | 60723dc9 | taeseongkim | FileName = file.Name; |
371 | downloadFilePath = System.IO.Path.Combine(saveItem.SavePath, FileName); |
||
372 | file.CopyTo(downloadFilePath, true); |
||
373 | 53c9637d | taeseongkim | } |
374 | else |
||
375 | { |
||
376 | throw new Exception("File Not Found. Please, check the file path."); |
||
377 | } |
||
378 | } |
||
379 | else if (Uri.TryCreate(saveItem.PdfFilePath, UriKind.RelativeOrAbsolute, out pdfFileUri)) |
||
380 | { |
||
381 | try |
||
382 | { |
||
383 | 77cdac33 | taeseongkim | |
384 | downloadFilePath = await RestDownloadAsync(pdfFileUri, saveItem.SavePath); |
||
385 | /*bseng |
||
386 | */ |
||
387 | /* 벽산으로 임시 주석 |
||
388 | |||
389 | 60723dc9 | taeseongkim | FileName = DownloadUri.GetFileName(saveItem.PdfFilePath); |
390 | downloadFilePath = System.IO.Path.Combine(saveItem.SavePath, FileName); |
||
391 | |||
392 | 77cdac33 | taeseongkim | using (System.Net.WebClient webClient = new System.Net.WebClient()) |
393 | { |
||
394 | webClient.UseDefaultCredentials = true;//.Headers.Add("Authorization: BASIC SGVsbG8="); //가상의 인증 |
||
395 | webClient.Proxy = null; |
||
396 | if (!System.IO.Directory.Exists(ConvertProcessContext.TempDirectory)) |
||
397 | { |
||
398 | System.IO.Directory.CreateDirectory(ConvertProcessContext.TempDirectory); |
||
399 | } |
||
400 | c5519c44 | taeseongkim | |
401 | 77cdac33 | taeseongkim | webClient.DownloadFile(pdfFileUri, downloadFilePath); |
402 | |||
403 | webClient.Dispose(); |
||
404 | } |
||
405 | * */ |
||
406 | c5519c44 | taeseongkim | await Task.Delay(300); |
407 | 53c9637d | taeseongkim | } |
408 | a2a64028 | taeseongkim | catch (Exception ex) |
409 | 53c9637d | taeseongkim | { |
410 | a2a64028 | taeseongkim | logger.Error(ex); |
411 | 77cdac33 | taeseongkim | throw new Exception($"File Download Error. Please, check the file path. {pdfFileUri}"); |
412 | 53c9637d | taeseongkim | } |
413 | } |
||
414 | else |
||
415 | { |
||
416 | throw new Exception("Please, check the file path."); |
||
417 | } |
||
418 | |||
419 | 3aafb914 | taeseongkim | try |
420 | 53c9637d | taeseongkim | { |
421 | 3aafb914 | taeseongkim | if (File.Exists(downloadFilePath)) |
422 | 53c9637d | taeseongkim | { |
423 | 3aafb914 | taeseongkim | var file = File.Open(downloadFilePath, FileMode.Open); |
424 | |||
425 | if (file.Length == 0) |
||
426 | { |
||
427 | throw new Exception("File Size 0. Please, check the file path."); |
||
428 | } |
||
429 | 53c9637d | taeseongkim | |
430 | 3aafb914 | taeseongkim | file.Close(); |
431 | file.Dispose(); |
||
432 | 53c9637d | taeseongkim | |
433 | 3aafb914 | taeseongkim | saveItem.PdfFilePath = downloadFilePath; |
434 | result = true; |
||
435 | 53c9637d | taeseongkim | |
436 | 3aafb914 | taeseongkim | } |
437 | } |
||
438 | a2a64028 | taeseongkim | catch (Exception ex) |
439 | 3aafb914 | taeseongkim | { |
440 | a2a64028 | taeseongkim | logger.Error(ex); |
441 | 3aafb914 | taeseongkim | throw new Exception("File Error ." + downloadFilePath); |
442 | 53c9637d | taeseongkim | } |
443 | } |
||
444 | af9bffc5 | taeseongkim | catch (Exception ex) |
445 | 3aafb914 | taeseongkim | { |
446 | a2a64028 | taeseongkim | logger.Error(ex); |
447 | 3aafb914 | taeseongkim | throw new Exception(ex.ToString()); |
448 | 53c9637d | taeseongkim | } |
449 | |||
450 | return result; |
||
451 | } |
||
452 | |||
453 | 77cdac33 | taeseongkim | private async Task<string> RestDownloadAsync(Uri uri,string savePath) |
454 | { |
||
455 | string downloadFilePath = ""; |
||
456 | |||
457 | var client = new RestClient(uri); |
||
458 | client.Timeout = -1; |
||
459 | var request = new RestRequest(Method.GET); |
||
460 | IRestResponse response = await client.ExecuteAsync(request); |
||
461 | |||
462 | if (response.StatusCode == System.Net.HttpStatusCode.OK) |
||
463 | { |
||
464 | var fileName = DownloadUri.GetFileNameInDisposition(new System.Net.Mime.ContentDisposition(response.Headers.Where(x => x.Name == "Content-Disposition").First().Value.ToString())); |
||
465 | |||
466 | downloadFilePath = System.IO.Path.Combine(savePath, fileName); |
||
467 | |||
468 | var fs = File.Create(downloadFilePath); |
||
469 | |||
470 | await fs.WriteAsync(response.RawBytes, 0, response.RawBytes.Length); |
||
471 | fs.Close(); |
||
472 | } |
||
473 | |||
474 | //var fileInfo = new FileInfo(downloadFilePath); |
||
475 | |||
476 | //using (var fileStream = fileInfo.OpenWrite()) |
||
477 | //{ |
||
478 | // await fileStream.WriteAsync(response.RawBytes, 0, response.RawBytes.Length); |
||
479 | // fileStream.Close(); |
||
480 | // //int position = 0; |
||
481 | // //int step = 300000; |
||
482 | // //int length = 300000; |
||
483 | |||
484 | // //while (position < response.RawBytes.Length) |
||
485 | // //{ |
||
486 | // // if (position + step < response.RawBytes.Length) |
||
487 | // // { |
||
488 | // // length = position + step; |
||
489 | // // } |
||
490 | // // else |
||
491 | // // { |
||
492 | // // length = response.RawBytes.Length - position; |
||
493 | // // } |
||
494 | |||
495 | // // await fileStream.WriteAsync(response.RawBytes, position, length); |
||
496 | |||
497 | // // position = position + step; |
||
498 | // //} |
||
499 | //} |
||
500 | |||
501 | return downloadFilePath; |
||
502 | } |
||
503 | |||
504 | 53c9637d | taeseongkim | /// <summary> |
505 | /// 큰파일 변환 |
||
506 | /// </summary> |
||
507 | /// <param name="saveitem"></param> |
||
508 | /// <returns></returns> |
||
509 | private SaveResult ConvertBigFileProcess(SaveItem saveitem) |
||
510 | { |
||
511 | SaveResult result = new SaveResult(); |
||
512 | |||
513 | try |
||
514 | { |
||
515 | saveitem.TotalPages = gMarkusPDF.PageCount(); |
||
516 | |||
517 | for (int currentPageNo = 1; currentPageNo <= saveitem.TotalPages; currentPageNo++) |
||
518 | { |
||
519 | f363a40e | taeseongkim | try |
520 | { |
||
521 | string saveFile = Path.Combine(saveitem.SavePath, $"{currentPageNo}.png"); |
||
522 | 53c9637d | taeseongkim | |
523 | f363a40e | taeseongkim | if (ReConvert < 1 || (ReConvert == 1 && !File.Exists(saveFile))) |
524 | { |
||
525 | 53c9637d | taeseongkim | |
526 | f363a40e | taeseongkim | var saveResult = gMarkusPDF.SavePage(currentPageNo, saveFile); |
527 | 53c9637d | taeseongkim | |
528 | f363a40e | taeseongkim | saveitem.Status = StatusCodeType.Saving; |
529 | saveitem.CurrentPage = currentPageNo; |
||
530 | saveitem.ErrorMessage = saveResult.Message; |
||
531 | 53c9637d | taeseongkim | |
532 | f363a40e | taeseongkim | int pageListCount = 0; |
533 | |||
534 | if(result.PageInfoList != null) |
||
535 | { |
||
536 | pageListCount = result.PageInfoList.Count(f => f.PageNo == saveResult.SavePageInfo.PageNo); |
||
537 | } |
||
538 | |||
539 | if (pageListCount == 0) |
||
540 | { |
||
541 | result.PageInfoAdd(saveResult.SavePageInfo); |
||
542 | } |
||
543 | |||
544 | MarkusPdfStateChangeEvent(this, new StateEventArgs(saveitem)); |
||
545 | Console.WriteLine($"CurrentPage : {currentPageNo}"); |
||
546 | |||
547 | /// 설정된 최대 페이지이거나 설정된 메모리보다 크면 릴리즈 |
||
548 | e77fc685 | taeseongkim | if ((currentPageNo % ConvertProcessContext.MultiThreadMaxPages == 0 && ConvertProcessContext.MultiThreadMaxPages > 0) |
549 | || ConvertProcessContext.ReleaseWorkMemory < Environment.WorkingSet) |
||
550 | f363a40e | taeseongkim | { |
551 | Console.WriteLine($"physical memory : {Environment.WorkingSet}"); |
||
552 | |||
553 | gMarkusPDF.Dispose(); |
||
554 | gMarkusPDF = null; |
||
555 | System.Threading.Thread.SpinWait(5); |
||
556 | |||
557 | gMarkusPDF = new MarkusPDF(); |
||
558 | bae83c92 | taeseongkim | gMarkusPDF.pdfLoad(gTempFileName, saveitem.MinimumFontSize, saveitem.UseResolution); |
559 | f363a40e | taeseongkim | } |
560 | c5519c44 | taeseongkim | |
561 | f363a40e | taeseongkim | System.Threading.Thread.SpinWait(2); |
562 | } |
||
563 | } |
||
564 | catch (Exception ex) |
||
565 | 53c9637d | taeseongkim | { |
566 | f363a40e | taeseongkim | result.StatusCode = StatusCodeType.PageError; |
567 | c5519c44 | taeseongkim | result.Message = LogHelper.GetStack() + " " +"Save Error - " + ex.Message; |
568 | 53c9637d | taeseongkim | |
569 | f363a40e | taeseongkim | if (gMarkusPDF != null) |
570 | { |
||
571 | gMarkusPDF.Dispose(); |
||
572 | gMarkusPDF = null; |
||
573 | } |
||
574 | 53c9637d | taeseongkim | |
575 | gMarkusPDF = new MarkusPDF(); |
||
576 | bae83c92 | taeseongkim | gMarkusPDF.pdfLoad(gTempFileName, saveitem.MinimumFontSize, saveitem.UseResolution); |
577 | f363a40e | taeseongkim | |
578 | currentPageNo = currentPageNo - 1; |
||
579 | } |
||
580 | finally |
||
581 | { |
||
582 | |||
583 | 53c9637d | taeseongkim | } |
584 | } |
||
585 | |||
586 | result.StatusCode = StatusCodeType.Completed; |
||
587 | } |
||
588 | catch (Exception ex) |
||
589 | { |
||
590 | result.StatusCode = StatusCodeType.Error; |
||
591 | c5519c44 | taeseongkim | result.Message = LogHelper.GetStack() + " " +ex.Message; |
592 | 53c9637d | taeseongkim | } |
593 | |||
594 | return result; |
||
595 | } |
||
596 | |||
597 | public bool Stop() |
||
598 | { |
||
599 | try |
||
600 | { |
||
601 | gSaveTask.Dispose(); |
||
602 | } |
||
603 | catch (Exception ex) |
||
604 | { |
||
605 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
606 | } |
||
607 | |||
608 | return true; |
||
609 | } |
||
610 | |||
611 | |||
612 | /// <summary> |
||
613 | /// 사용안함 |
||
614 | /// </summary> |
||
615 | /// <param name="param"></param> |
||
616 | /// <returns></returns> |
||
617 | //[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions] |
||
618 | //[System.Security.Permissions.FileIOPermission(System.Security.Permissions.SecurityAction.LinkDemand)] |
||
619 | //private SaveResult SetFile() |
||
620 | //{ |
||
621 | // var saveitem = new SaveItem |
||
622 | // { |
||
623 | // Id = ConvertProcessContext.ConvertID, |
||
624 | // PdfFilePath = ConvertProcessContext.OriginFilePath, |
||
625 | // SavePath = ConvertProcessContext.SaveDirectory, |
||
626 | // Status = StatusCodeType.None |
||
627 | // }; |
||
628 | |||
629 | // if (System.IO.Directory.Exists(saveitem.SavePath)) |
||
630 | // { |
||
631 | // System.IO.Directory.Delete(saveitem.SavePath, true); |
||
632 | // } |
||
633 | |||
634 | // System.IO.Directory.CreateDirectory(saveitem.SavePath); |
||
635 | |||
636 | // try |
||
637 | // { |
||
638 | // Uri pdfFileUri = null; |
||
639 | |||
640 | // if (Uri.TryCreate(saveitem.PdfFilePath, UriKind.RelativeOrAbsolute, out pdfFileUri)) |
||
641 | // { |
||
642 | // using (System.Net.WebClient webClient = new System.Net.WebClient()) |
||
643 | // { |
||
644 | // webClient.UseDefaultCredentials = true;//.Headers.Add("Authorization: BASIC SGVsbG8="); //가상의 인증 |
||
645 | |||
646 | // if (!System.IO.Directory.Exists(ConvertProcessContext.TempDirectory)) |
||
647 | // { |
||
648 | // System.IO.Directory.CreateDirectory(ConvertProcessContext.TempDirectory); |
||
649 | // } |
||
650 | |||
651 | // var downloadFilePath = System.IO.Path.Combine(ConvertProcessContext.TempDirectory, saveitem.Id.Replace("-", "") + ".pdf"); |
||
652 | // webClient.DownloadFile(pdfFileUri, downloadFilePath); |
||
653 | |||
654 | // saveitem.PdfFilePath = downloadFilePath; |
||
655 | // } |
||
656 | // } |
||
657 | // } |
||
658 | // catch (Exception ex) |
||
659 | // { |
||
660 | // throw new Exception($"File Download Error - File path : {saveitem.PdfFilePath}"); |
||
661 | // } |
||
662 | |||
663 | // StationServiceClient.ConvertProcessStateAsync(saveitem.Id, (int)saveitem.Status, saveitem.CurrentPage, saveitem.TotalPages, saveitem.ErrorMessage); |
||
664 | |||
665 | // var result = gSaveTask.SaveFile(saveitem); |
||
666 | |||
667 | // return result; |
||
668 | //} |
||
669 | |||
670 | } |
||
671 | } |