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