프로젝트

일반

사용자정보

개정판 43e1d368

ID43e1d3684849725d38b5c74b43d4bb593f546d7a
상위 d91efe5c
하위 42d49521

김태성이(가) 약 2년 전에 추가함

issue #0000 코드 정리

Change-Id: I7c8ba4ef6d78a7e4f51b72f226507cddac722939

차이점 보기:

ConvertService/ServiceBase/Markus.Service.Convert/ConvertService.cs
14 14

  
15 15
using System.Threading.Tasks;
16 16
using System.Web;
17
using RestSharp;
18 17

  
19 18
namespace Markus.Service.Convert
20 19
{
......
146 145
#if PROCESS_TEST
147 146
                Console.WriteLine($"send wcf service {e.SaveItem.Status}  currentPage : {e.SaveItem.CurrentPage} error message : {(string.IsNullOrWhiteSpace(e.SaveItem.ErrorMessage)?"null":e.SaveItem.ErrorMessage)}");
148 147
#endif
149
                StationServiceClient.ConvertProcessStateAsync(e.SaveItem.Id, (int)e.SaveItem.Status, e.SaveItem.CurrentPage, e.SaveItem.TotalPages, e.SaveItem.ErrorMessage);
148
                StationServiceClient.ConvertProcessState(e.SaveItem.Id, (int)e.SaveItem.Status, e.SaveItem.CurrentPage, e.SaveItem.TotalPages, e.SaveItem.ErrorMessage);
150 149

  
151 150
            }
152 151
            catch (Exception ex)
......
155 154
            }
156 155
        }
157 156

  
157
        [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
158 158
        [System.Security.Permissions.FileIOPermission(System.Security.Permissions.SecurityAction.LinkDemand)]
159
        public async Task<SaveItem> SetFileAsync()
159
        public SaveItem SetFile()
160 160
        {
161 161
            SaveItem saveitem = new SaveItem
162 162
            {
......
177 177
#endif
178 178
                StationServiceClient.ConvertProcessState(ConvertProcessContext.ConvertID, (int)StatusCodeType.Wait, 0, 0, "");
179 179

  
180
                var result = await ConvertAsync(saveitem);
180
                var result = Convert(saveitem);
181 181

  
182 182
                saveitem.Status = result.StatusCode;
183 183
                saveitem.ErrorMessage = result.Message;
......
187 187
#endif     
188 188

  
189 189
                StationServiceClient.ConvertFinish(saveitem.Id, (int)saveitem.Status, saveitem.CurrentPage, saveitem.TotalPages, saveitem.ErrorMessage);
190
  
190

  
191 191
            }
192 192
            catch (Exception ex)
193 193
            {
194
                saveitem.Status = StatusCodeType.Error;
195
                saveitem.ErrorMessage = ex.ToString();
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

  
196 206
                logger.Error($"File Convert Error", ex);
197 207

  
198 208
#if PROCESS_TEST
199 209

  
200 210
                Console.WriteLine($"run Plugin : {saveitem.Id}");
201 211
#endif
202
                logger.Error($"saveitem.Id : {saveitem.Id} , StatusCode:{(int)saveitem.Status}, CurrentPage : {saveitem.CurrentPage}, TotalPages : {saveitem.TotalPages} , Error : {saveitem.ErrorMessage} , inner error : {ex.Message} {ex.InnerException?.ToString()}");
203
                StationServiceClient.ConvertFinish(saveitem.Id, (int)saveitem.Status, saveitem.CurrentPage, saveitem.TotalPages, $"ConvertService Error    {saveitem.Id} {ex.Message} {ex.InnerException?.ToString()}");
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 }");
204 214

  
205 215
            }
206 216
            finally
......
218 228
            return saveitem;
219 229
        }
220 230

  
221
        [System.Security.Permissions.FileIOPermission(System.Security.Permissions.SecurityAction.LinkDemand)]
222
        private async Task<SaveResult> ConvertAsync(SaveItem saveitem)
231
        private SaveResult Convert(SaveItem saveitem)
223 232
        { 
224 233
            SaveResult result = new SaveResult();
225 234
         
......
265 274
                    if (gMarkusPDF.PageCount() > 0)
266 275
                    {
267 276
                        /// 설정된 MultiThreadMaxPages에 따른 컨버터 분기
268
                        //if (gMarkusPDF.PageCount() > ConvertProcessContext.MultiThreadMaxPages)
269
                        //{
270
                        //    // 큰 사이즈의 파일 컨버팅
271
                          result = ConvertBigFileProcess(saveitem,downloadFilePath);
272
                        //}
273
                        //else
274
                        //{
275
                        //    /// 작은 사이즈의 컨버팅
276
                        //    await Task.Factory.StartNew(new Action(() =>
277
                        //    {
278
                        //        result = gSaveTask.SaveFile(saveitem);
279
                        //    }), TaskCreationOptions.LongRunning);
280
                        //}
281
                        
282
                        if((int)result.StatusCode <= (int)StatusCodeType.Completed)
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)
283 295
                        {
284 296
                            // 파일 체크 후 갯수가 안맞으면 다시 컨버팅한다.
285 297
                            if (ReConvert < 1 && (result.PageInfoList?.Count() != saveitem.TotalPages
......
393 405
            return result;
394 406
        }
395 407

  
396
        /// <summary>
397
        /// 파일 다운로드
398
        /// </summary>
399
        /// <param name="saveItem"></param>
400
        /// <returns></returns>
401
        public async Task<bool> DownloadFileAsync(SaveItem saveItem)
402
        {
403
            bool result = false;
404

  
405
            try
406
            {
407
                Uri pdfFileUri = null;
408

  
409
                //if (saveItem.PdfFilePath.Contains("VPCS_DOCLIB"))
410
                //{
411
                //    FileName = DocUri.Remove(0, DocUri.LastIndexOf("/") + 1);
412
                //    ProjectFolderPath = DownloadDir_PDF + "\\" + ConverterItem.PROJECT_NO + "_Tile"; //프로젝트 폴더
413
                //    ItemListPath = ProjectFolderPath + "\\" + (System.Convert.ToInt64(ConverterItem.DOCUMENT_ID) / 100).ToString();
414
                //    ItemPath = ItemListPath + "\\" + ConverterItem.DOCUMENT_ID;
415
                //    DownloadFilePath = ItemPath + "\\" + FileName;
416
                //}
417
                //else
418
                //{
419
                //    ProjectFolderPath = DownloadDir_PDF + "\\" + ConverterItem.PROJECT_NO + "_Tile"; //프로젝트 폴더
420
                //    ItemListPath = ProjectFolderPath + "\\" + (System.Convert.ToInt64(ConverterItem.DOCUMENT_ID) / 100).ToString();
421
                //    ItemPath = ItemListPath + "\\" + ConverterItem.DOCUMENT_ID;
422
                //    FileName = HttpUtility.ParseQueryString(new Uri(DocUri).Query).Get("fileName");
423
                //    DownloadFilePath = string.IsNullOrWhiteSpace(FileName) ? ItemPath + "\\" + FileName : ItemPath + "\\" + FileName;
424

  
425
                //}
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;
426 416

  
427
                saveItem.PdfFilePath = HttpUtility.UrlDecode(saveItem.PdfFilePath); //PDF 전체 경로
417
        //    try
418
        //    {
419
        //        Uri pdfFileUri = null;
428 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;
429 436

  
430
                string FileName = "";
431
                string downloadFilePath = "";
437
        //        //}
432 438

  
433
                // 드라이브 경로가 포함되었는지 판단.
434
                if (Path.IsPathRooted(saveItem.PdfFilePath))
435
                {
436
                    FileInfo file = new FileInfo(saveItem.PdfFilePath);
439
        //        saveItem.PdfFilePath = HttpUtility.UrlDecode(saveItem.PdfFilePath); //PDF 전체 경로
437 440

  
438
                    if (file.Exists)
439
                    {
440
                        FileName = file.Name;
441
                        downloadFilePath = System.IO.Path.Combine(saveItem.SavePath, FileName);
442
                        file.CopyTo(downloadFilePath, true);
443
                    }
444
                    else
445
                    {
446
                        throw new Exception("File Not Found. Please, check the file path.");
447
                    }
448
                }
449
                else if (Uri.TryCreate(saveItem.PdfFilePath, UriKind.RelativeOrAbsolute, out pdfFileUri))
450
                {
451
                    try
452
                    {
453
                        if (gIsApiDownload)
454
                        {
455
                            ///read api download 
456
                            ///벽산
457
                            downloadFilePath = await RestDownloadAsync(pdfFileUri, saveItem.SavePath);
458
                        }
459
                        else
460
                        {
461
                            FileName = DownloadUri.GetFileName(saveItem.PdfFilePath);
462
                            downloadFilePath = System.IO.Path.Combine(saveItem.SavePath, FileName);
463 441

  
464
                            using (System.Net.WebClient webClient = new System.Net.WebClient())
465
                            {
466
                                webClient.UseDefaultCredentials = true;
467
                                webClient.Headers.Add("Authorization: BASIC SGVsbG8="); //가상의 인증
468
                                webClient.Proxy = null;
469
                                if (!System.IO.Directory.Exists(ConvertProcessContext.TempDirectory))
470
                                {
471
                                    System.IO.Directory.CreateDirectory(ConvertProcessContext.TempDirectory);
472
                                }
442
        //        string FileName = "";
443
        //        string downloadFilePath = "";
473 444

  
474
                                webClient.DownloadFile(pdfFileUri, downloadFilePath);
445
        //        // 드라이브 경로가 포함되었는지 판단.
446
        //        if (Path.IsPathRooted(saveItem.PdfFilePath))
447
        //        {
448
        //            FileInfo file = new FileInfo(saveItem.PdfFilePath);
475 449

  
476
                                webClient.Dispose();
477
                            }
478
                        }
479
                        await Task.Delay(300);
480
                    }
481
                    catch (Exception ex)
482
                    {
483
                        logger.Error(ex);
484
                        throw new Exception($"File Download Error. Please, check the file path. {pdfFileUri}");
485
                    }
486
                }
487
                else
488
                {
489
                    throw new Exception("Please, check the file path.");
490
                }
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
        //        }
491 503

  
492
                try
493
                {
494
                    if (File.Exists(downloadFilePath))
495
                    {
496
                        var file = File.Open(downloadFilePath, FileMode.Open);
504
        //        try
505
        //        {
506
        //            if (File.Exists(downloadFilePath))
507
        //            {
508
        //                var file = File.Open(downloadFilePath, FileMode.Open);
497 509

  
498
                        if (file.Length == 0)
499
                        {
500
                            throw new Exception("File Size 0. Please, check the file path.");
501
                        }
510
        //                if (file.Length == 0)
511
        //                {
512
        //                    throw new Exception("File Size 0. Please, check the file path.");
513
        //                }
502 514

  
503
                        file.Close();
504
                        file.Dispose();
515
        //                file.Close();
516
        //                file.Dispose();
505 517

  
506
                        saveItem.PdfFilePath = downloadFilePath;
507
                        result = true;
518
        //                saveItem.PdfFilePath = downloadFilePath;
519
        //                result = true;
508 520

  
509
                    }
510
                }
511
                catch (Exception ex)
512
                {
513
                    logger.Error(ex);
514
                    throw new Exception("File Error ." + downloadFilePath);
515
                }
516
            }
517
            catch (Exception ex)
518
            {
519
                logger.Error(ex);
520
                throw new Exception(ex.ToString());
521
            }
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
        //    }
522 534

  
523
            return result;
524
        }
535
        //    return result;
536
        //}
525 537

  
526
        private  async Task<string> RestDownloadAsync(Uri uri,string savePath)
527
        {
528
            string downloadFilePath = "";
538
        //private  async Task<string> RestDownloadAsync(Uri uri,string savePath)
539
        //{
540
        //    string downloadFilePath = "";
529 541

  
530
            var client = new RestClient(uri);
531
            client.Timeout = -1;
532
            var request = new RestRequest(Method.GET);
533
            IRestResponse response = await client.ExecuteAsync(request);
542
        //    var client = new RestClient(uri);
543
        //    client.Timeout = -1;
544
        //    var request = new RestRequest(Method.GET);
545
        //    IRestResponse response = await client.ExecuteAsync(request);
534 546

  
535
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
536
            {
537
                var fileName = DownloadUri.GetFileNameInDisposition(new System.Net.Mime.ContentDisposition(response.Headers.Where(x => x.Name == "Content-Disposition").First().Value.ToString()));
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()));
538 550

  
539
                downloadFilePath = System.IO.Path.Combine(savePath, fileName);
551
        //        downloadFilePath = System.IO.Path.Combine(savePath, fileName);
540 552

  
541
                var fs = File.Create(downloadFilePath);
553
        //        var fs = File.Create(downloadFilePath);
542 554

  
543
                await fs.WriteAsync(response.RawBytes, 0, response.RawBytes.Length);
544
                fs.Close();
545
            }
555
        //        await fs.WriteAsync(response.RawBytes, 0, response.RawBytes.Length);
556
        //        fs.Close();
557
        //    }
546 558

  
547
            return downloadFilePath;
548
        }
559
        //    return downloadFilePath;
560
        //}
549 561

  
550 562
        /// <summary>
551 563
        /// 큰파일 변환
......
568 580

  
569 581
                        if (ReConvert < 1 || (ReConvert == 1 && !File.Exists(saveFile)))
570 582
                        {
571
                            var saveResult = gMarkusPDF.SavePage(currentPageNo, saveFile);
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
                            }
572 593

  
573 594
                            if(saveResult.StatusCode == StatusCodeType.Completed)
574 595
                            {
......
617 638
                                result.Message = saveitem.ErrorMessage;
618 639

  
619 640
                                MarkusPdfStateChangeEvent(this, new StateEventArgs(saveitem));
641
                                
620 642
                                Console.WriteLine($"CurrentPage : {currentPageNo}");
621 643
                                Console.WriteLine($"convert ID : {saveitem.Id}");
622 644
                                Console.WriteLine($"pdf file Path : {saveitem.PdfFilePath}");

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)