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