프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ConverterService / DZConverterLib / Copy of DZConverter.cs @ dd0f7089

이력 | 보기 | 이력해설 | 다운로드 (24.2 KB)

1 7ca218b3 KangIngu
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Collections.ObjectModel;
6
using System.Data.EntityClient;
7
using System.Data.SqlClient;
8
using DeepViewDataModel.DataModel;
9
using DeepViewDataModel.Common;
10
using System.Collections.Concurrent;
11
using System.IO;
12
using DZConverterLib.DAL;
13
using DZConverterLib.Auth;
14
using pdftron;
15
using System.Data.Objects.DataClasses;
16
using pdftron.PDF;
17
using System.Drawing;
18
using System.Drawing.Imaging;
19
using pdftron.SDF;
20
using Microsoft.DeepZoomTools;
21
using DeepView.Toolkit.GuidGenerator;
22
using System.Threading;
23
using System.Net.Cache;
24
namespace DZConverterLib
25
{
26
    public class DZConverter: IDisposable
27
    {
28
        //public const string ProjectRoot = @"D:\DeepViewConverter\";
29
        //public const string DownloadDir_PDF = ProjectRoot + @"\PDFPath";
30
        public string DownloadDir_PDF;
31
        private List<String> DeepZoopDest = null;
32
        private EntityCollection<DocPage> pageSet = null;
33
        public int pageCount = 0;
34
        public ConverterPDF ConverterItem;
35
        public event EventHandler<MakeConverterErrorArgs> ConverterMakeError;
36
        public event EventHandler<EndConverterEventArgs> EndConverter;
37
38
        public DZConverter()
39
        {
40
            pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA");
41
            DeepZoopDest = new List<string>();
42
            pageSet = new EntityCollection<DocPage>();
43
        }
44
45
        private void SendError(string Error,bool ThreadStop)
46
        {
47
            if (ConverterMakeError != null)
48
                ConverterMakeError(this, new MakeConverterErrorArgs { ThreadStop = ThreadStop, ConverterID = ConverterItem.ID, Message = Error });
49
        }
50
51
        public void GetSilgleFile(object obj)
52
        {
53
            try
54
            {
55
                ConverterItem = obj as ConverterPDF;
56
57
                using (DeepViewEntities _deepview = new DeepViewEntities(ConnectStringBuilder.DeepViewConnectionString().ToString()))
58
                {
59
                    var _property = _deepview.Properties.Where(pro => pro.Property == ConverterItem.ProjectNo);
60
61
                    if (_property.Count() > 0)
62
                    {
63
                        DownloadDir_PDF = _property.Where(t => t.Type == PropertiesType.PropertiesType.Const_TileSorceStorage).First().Value;
64
65
                        if (!Directory.Exists(DownloadDir_PDF)) // 폴더 없을 시 생성
66
                        {
67
                            Directory.CreateDirectory(DownloadDir_PDF);
68
                        }
69
                    }
70
                }
71
72
                    ConvertProcess();
73
               
74
            }
75
            catch (Exception e)
76
            {
77
                SendError("GetSilgleFile : " + e.ToString(), true);
78
            }
79
        }
80
81
        private void SetState(IConverterPDF.ConverterStatus Status)
82
        {
83
            using (DeepViewEntities _entity = new DeepViewEntities(ConnectStringBuilder.DeepViewConnectionString().ToString()))
84
            {
85
                var _items = _entity.ConverterPDF.Where(converter => converter.ID == this.ConverterItem.ID);
86
                if (_items.Count() > 0)
87
                {
88
                    _items.First().Status = (int)Status;
89
                    _entity.SaveChanges();
90
                }
91
            }  
92
        }
93
94
        private void SetCurrentPage(int CurrentPage)
95
        {
96
            using (DeepViewEntities _entity = new DeepViewEntities(ConnectStringBuilder.DeepViewConnectionString().ToString()))
97
            {
98
                var _items = _entity.ConverterPDF.Where(converter => converter.ID == this.ConverterItem.ID);
99
                if (_items.Count() > 0)
100
                {
101
                    _items.First().CurrentPage = CurrentPage;
102
                    _entity.SaveChanges();
103
                }
104
            }
105
        }
106
107
        private static object _lock = new object();
108
109
        public static void DeleteDirectory(string target_dir)
110
        {
111
            DeleteDirectoryFiles(target_dir);
112
            while (Directory.Exists(target_dir))
113
            {
114
                lock (_lock)
115
                {
116
                    DeleteDirectoryDirs(target_dir);
117
                }
118
            }
119
        }
120
121
        private static void DeleteDirectoryDirs(string target_dir)
122
        {
123
            System.Threading.Thread.Sleep(100);
124
125
            if (Directory.Exists(target_dir))
126
            {
127
128
                string[] dirs = Directory.GetDirectories(target_dir);
129
130
                if (dirs.Length == 0)
131
                    Directory.Delete(target_dir, true);
132
                else
133
                    foreach (string dir in dirs)
134
                        DeleteDirectoryDirs(dir);
135
            }
136
        }
137
138
        private static void DeleteDirectoryFiles(string target_dir)
139
        {
140
            string[] files = Directory.GetFiles(target_dir);
141
            string[] dirs = Directory.GetDirectories(target_dir);
142
143
            foreach (string file in files)
144
            {
145
                File.SetAttributes(file, FileAttributes.Normal);
146
                File.Delete(file);
147
            }
148
149
            foreach (string dir in dirs)
150
            {
151
                DeleteDirectoryFiles(dir);
152
            }
153
        }
154
155
        void DeteteDirectory(string Path)
156
        {
157
            Thread _stathread = new Thread(new ParameterizedThreadStart(DeteteWork));
158
            _stathread.SetApartmentState(ApartmentState.MTA);
159
            _stathread.Start(Path);
160
        }
161
162
        void DeteteWork(object Path)
163
        {
164
            DirectoryInfo _dir = new DirectoryInfo(Path.ToString());
165
166
            _dir.EnumerateDirectories().AsParallel().ForAll(d =>
167
            {
168
                d.Delete(true);
169
            });
170
171
            _dir.Delete(true);
172
        }
173
174
        public void ConvertProcess()
175
        {
176
            #region 기본 설정
177
            string DocUri = ConverterItem.PdfUrl; //PDF 전체 경로
178
            string FileName = DocUri.Remove(0, DocUri.LastIndexOf("/") + 1);
179
            string ProjectFolderPath = DownloadDir_PDF + "\\" + ConverterItem.ProjectNo + "_Tile"; //프로젝트 폴더
180
            string ItemListPath = ProjectFolderPath + "\\" + (System.Convert.ToInt64(ConverterItem.SharepointItemID)/100).ToString();
181
            string ItemPath = ItemListPath + "\\" + ConverterItem.SharepointItemID;
182
            string DownloadFilePath = ItemPath + "\\" + FileName;
183
            string _delPath = ItemPath;
184
            #endregion
185
186
            #region 폴더 생성
187
188
            try
189
            {
190
                DirectoryInfo _dir = new DirectoryInfo(ItemPath);
191
192
                if (_dir.Exists)
193
                {
194
                    for (int i = 0; i < 100; i++)
195
                    {
196
                        _delPath = ItemPath + "_" + i.ToString();
197
                        if (!Directory.Exists(ItemPath + "_" + i.ToString()))
198
                        {
199
                            _dir.MoveTo(_delPath);
200
                            DeteteDirectory(_delPath);
201
                            break;
202
                        }
203
                    }
204
                }
205
206
                Directory.CreateDirectory(ItemPath);
207
208
            }
209
            catch (Exception ex)
210
            {
211
                SendError(" 폴더삭제 에러 " + ex.ToString(), false);
212
            }
213
  
214
            #endregion
215
216
            #region 다운로드
217
218
            #region 다운로드
219
            Filefactory _fileFactory = new Filefactory();
220
            if (AuthUser2(_fileFactory, DocUri))
221
            {
222
                _fileFactory.DownloadFile(new Uri(DocUri), DownloadFilePath);
223
            }
224
            else
225
            {
226
                SendError("다운로드에 실패하였습니다", true);
227
            }
228
            #endregion      
229
            //System.Net.WebClient _fileFactory = new System.Net.WebClient();
230
            ////HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
231
            ////_fileFactory.CachePolicy = policy;
232
            //var _auth = AuthUser(DocUri);
233
            //if (_auth != null)
234
            //{
235
            //    _fileFactory.Credentials = _auth;
236
            //    _fileFactory.DownloadFile(new Uri(DocUri), DownloadFilePath);
237
            //}
238
            //else
239
            //{
240
            //    SendError("다운로드에 실패하였습니다", true);
241
            //} 
242
                #endregion           
243
244
            ResultSet Result_Check;
245
            #region PDF 체크
246
            try
247
            {
248
                Result_Check = Check_PDF(DownloadFilePath);
249
            }
250
            catch (Exception ex)
251
            {
252
                System.Diagnostics.Debug.WriteLine(ex.ToString());
253
                throw;
254
            }
255
            
256
            if (!Result_Check.result)
257
            {
258
                SendError(" PDF 체크 실패, 원인 : " + Result_Check.Message, true);
259
            } 
260
            #endregion
261
262
            SetState(IConverterPDF.ConverterStatus.Create);
263
264
            #region PDF에서 이미지로 변환
265
            if (!ConvertImage(ItemPath, FileName))
266
            {
267
                SendError("PDF 이미지로 변환 실패, 원인 : 데이터베이스 쓰기 실패", true);
268
            } 
269
            #endregion
270
271
            SetState(IConverterPDF.ConverterStatus.Crop);
272
273
            #region 이미지에서 딥줌 이미지로
274
            if (!ConvertDeepZoomImage(DeepZoopDest))
275
            {
276
                SendError("딥줌 이미지 변경 실패", true);
277
            }
278
            #endregion
279
280
            #region 데이터베이스 입력
281
            if (!SetPageInfoToDB())
282
            {
283
                SendError("데이터베이스 입력 실패", true);
284
            }
285
            #endregion
286
287
            //// virtual dir생성은 로컬에서만 가능
288
            //try
289
            //{
290
            //    VirtualDirCreate.CreateVirtualDir("comment.daelim.com", "MACHINE/WEBROOT/APPHOST/DeepView", prjNo + "_Tile", DownloadDir_PDF + "\\" + prjNo + "_Tile");
291
            //}
292
            //catch (Exception ex)
293
            //{
294
            //    SendError("VirtualDirCreate Error");
295
            //}
296
297
            if (EndConverter != null)
298
                EndConverter(this, new EndConverterEventArgs { ConverterPDF = ConverterItem});
299
        }
300
301
        public ResultSet Check_PDF(string filePath)
302
        {
303
            pdftron.PDF.PDFDoc doc = new pdftron.PDF.PDFDoc(filePath);
304
            #region 접근권한 체크
305
            if (!doc.InitSecurityHandler())
306
            {
307
                PDFNet.Terminate();
308
                return new ResultSet { result = false, Message = "Document authentication Error" };
309
            } 
310
            #endregion
311
            #region 암호화 여부
312
            if (doc.IsEncrypted())
313
            {
314
                PDFNet.Terminate();
315
                return new ResultSet { result = false, Message = "Document is Encrypted" };
316
            } 
317
            #endregion            
318
            return new ResultSet { result = true };
319
        }
320
321
322
        #region private Method
323
        private bool AuthUser2(Filefactory fileFactory, string PDFurl)
324
        {
325
            try
326
            {
327
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(PDFurl);
328
                System.Net.NetworkCredential credentials = Crediential.GetAdminCrediential(PDFurl.ToString());
329
                request.Credentials = credentials;
330
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
331
                response.Close();
332
                fileFactory.Credentials = credentials;
333
            }
334
            catch (Exception ex)
335
            {
336
                System.Diagnostics.Debug.WriteLine(ex.ToString());
337
                return false;
338
            }
339
340
            return true;
341
        }
342
        private System.Net.ICredentials AuthUser(string PDFurl)
343
        {
344
            System.Net.NetworkCredential credentials = null;
345
            try
346
            {
347
                var _url = new UriBuilder(new Uri(PDFurl).AbsoluteUri.Replace(new Uri(PDFurl).PathAndQuery, "/"));
348
                var _dns = _url.Host.Split('.');
349
350
                if (_dns.Count() == 3)
351
                    if (_dns[0].ToLower() != "www")
352
                    {
353
                        _dns[0] = "www";
354
                        _url.Host = _dns[0] + "." + _dns[1] + "." + _dns[2];
355
                    }
356
357
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(_url.Uri);
358
                credentials = new System.Net.NetworkCredential("DAELIM\\esbsystem", "kdm707");
359
                request.Credentials = credentials;
360
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();   
361
                response.Close();
362
            }
363
            catch (Exception ex)
364
            {
365
                return credentials;
366
            }
367
368
            return credentials;
369
        }
370
371
        private ImageCodecInfo GetEncoderInfo(String mimeType)
372
        {
373
            int j;
374
            ImageCodecInfo[] encoders;
375
            encoders = ImageCodecInfo.GetImageEncoders();
376
            for (j = 0; j < encoders.Length; ++j)
377
            {
378
                if (encoders[j].MimeType == mimeType)
379
                    return encoders[j];
380
            }
381
            return null;
382
        }
383
        private bool ConvertImage(string FilePath, string fileName)
384
        {
385
            pageSet = new EntityCollection<DocPage>();
386
            using (PDFDoc doc = new PDFDoc(FilePath + "\\" + fileName))
387
            {
388
                pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA");
389
                Bitmap newBmp_;
390
                ImageCodecInfo DefaultImageCodecInfo = GetEncoderInfo("image/png");
391
                EncoderParameters DefaultEncoderParameters = new EncoderParameters(2);
392
                System.Drawing.Imaging.Encoder QualityEncoder = System.Drawing.Imaging.Encoder.Quality;
393
                System.Drawing.Imaging.Encoder ColorDepthEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
394
                DefaultEncoderParameters.Param[0] = new EncoderParameter(QualityEncoder, 100L);
395
                DefaultEncoderParameters.Param[1] = new EncoderParameter(ColorDepthEncoder, 8L);
396
                DeepZoopDest = new List<string>();
397
                pageCount = doc.GetPageCount();
398
399
                using (DeepViewEntities _entity = new DeepViewEntities(ConnectStringBuilder.DeepViewConnectionString().ToString()))
400
                {
401
                    var _items = _entity.ConverterPDF.Where(converter => converter.ID == this.ConverterItem.ID);
402
                    if (_items.Count() > 0)
403
                    {
404
                        _items.First().TotalPages = pageCount;
405
                        _entity.SaveChanges();
406
                    }
407
                }
408
409
                #region 이미지 만들기
410
                for (int i = 1; i < doc.GetPageCount() + 1; i++)
411
                {
412
                    try
413
                    {
414
                        SetCurrentPage(i);
415
                        using (pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw())
416
                        {
417
                            ElementBuilder bld = new ElementBuilder();
418
                            ElementWriter writer = new ElementWriter();
419
420
                            var widthData = (int)doc.GetPage(i).GetPageWidth();
421
                            int heightData = (int)doc.GetPage(i).GetPageHeight();
422
                            pageSet.Add(new DocPage 
423
                                        {
424
                                            Id=  GuidGenerator.GetUniqueGuid() , 
425
                                            PageWidth = (widthData*2).ToString(), 
426
                                            PageHeight = (heightData*2).ToString(), 
427
                                            PageNumber = i, 
428
                                            PageAngle = 0,
429
                                        });
430
431
                            var rotation = doc.GetPage(i).GetRotation();
432
                            draw.SetImageSize((int)(widthData * 2), (int)(heightData * 2), true);
433
                            //draw.SetImageSize((int)(widthData), (int)(heightData), true);
434
                            draw.SetAntiAliasing(true);
435
                            draw.SetImageSmoothing(true);
436
                            newBmp_ = draw.GetBitmap(doc.GetPage(i));
437
438
                            using (MemoryStream _savestream = new MemoryStream())
439
                            {
440
441
                                //newBmp_.Save(@"c:\test.jpg");
442
                                newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters);
443
                                //newBmp_.Save(_savestream, ImageFormat.Png);
444
                                newBmp_ = new Bitmap(_savestream);
445
                                ObjSet objset = new ObjSet();
446
                                Obj jbig2_hint = objset.CreateName("png");
447
448
                                string pagePath = FilePath + "\\" + i;
449
450
                                Directory.CreateDirectory(pagePath);
451
                                DeepZoopDest.Add(pagePath + "\\" + i + ".jpg");
452
                                newBmp_.Save(pagePath + "\\" + i + ".jpg");
453
                                newBmp_.Dispose();
454
      
455
                            }
456
457
                            GC.Collect();
458
                            GC.WaitForPendingFinalizers();
459
                        }
460
                    }
461
                    catch (Exception ex)
462
                    {
463
                        SendError(ex.ToString(), false);
464
                        return false;
465
                    }
466
                }
467
                #endregion
468
                return true;
469
            }
470
        }
471
472
        private bool ConvertDeepZoomImage(List<String> FileSet)
473
        {
474
            ImageCreator crator = new ImageCreator();
475
            crator.TileSize = 256;
476
            crator.TileFormat = Microsoft.DeepZoomTools.ImageFormat.AutoSelect;
477
            crator.CopyMetadata = false;
478
479
            try
480
            {
481
                for (int i = 0; i < FileSet.Count; i++)
482
                {
483
                        SetCurrentPage(i + 1);
484
                        crator.Create(FileSet[i], FileSet[i].ToString().Replace(".jpg", ".xml"));
485
                }
486
            }
487
            catch (Exception ex)
488
            {
489
                System.Diagnostics.Debug.WriteLine(ex.ToString());
490
                new Exception("Converter Image Error");
491
            }
492
              
493
            return true;
494
        }
495
496
        private bool SetPageInfoToDB()
497
        {
498
            #region 다 쓴 이미지 삭제
499
            foreach (var item in DeepZoopDest)
500
            {
501
                try
502
                {
503
                    if (File.Exists(item.ToString()))
504
                    {
505
                        File.Delete(item.ToString());
506
                    }
507
                }
508
                catch (Exception e)
509
                {
510
                    /// 서버로 보내는 에러메시지 필요
511
                }
512
513
            } 
514
            #endregion
515
            #region DB에 페이지 정보 입력
516
            try
517
            {
518
519
                using (CI_Entities deepViewEntity = new CI_Entities(ConnectStringBuilder.ProjectCIConnectString(ConverterItem.ProjectNo).ToString()))
520
                {
521
                    var _docinfo = deepViewEntity
522
                                    .DocInfo.Where(info => info.OrginPDFUrl == ConverterItem.PdfUrl);
523
524
                    if (_docinfo.Count() > 0)
525
                    {
526
                        System.Diagnostics.Stopwatch _watch = new System.Diagnostics.Stopwatch();
527
                        _watch.Start();
528
                        var _doc = _docinfo.First();
529
                        _doc.SharepointItemID = ConverterItem.SharepointItemID;
530
                        _doc.PageCount = pageCount;
531
532
                        pageSet.AsParallel().ForAll(p => p.DocInfo_Id = _doc.Id);
533
534
                        var _exceptDocInfo = (from newinfo in pageSet
535
                                              select new { newinfo.PageNumber, newinfo.DocInfo_Id,newinfo.PageWidth,newinfo.PageHeight})
536
                                                  .Except
537
                                                        (from oldinfo in _doc.DocPage
538
                                                         select new { oldinfo.PageNumber, oldinfo.DocInfo_Id, oldinfo.PageWidth, oldinfo.PageHeight });
539
540
                        var _delDocInfo = (from oldinfo in _doc.DocPage
541
                                           select new { oldinfo.PageNumber, oldinfo.DocInfo_Id, oldinfo.PageWidth, oldinfo.PageHeight })
542
                                                  .Except
543
                                                        (from newinfo in pageSet
544
                                                         select new { newinfo.PageNumber, newinfo.DocInfo_Id, newinfo.PageWidth, newinfo.PageHeight })
545
                                                         .ToList(); /// 삭제되는 collection은 하나씩 제거 될때마다 수정되기 때문에
546
                                                                    ///  foreach가 안된다. 그러므로 tolist로 가져와서 작업한다.
547
548
                        foreach (var item in _delDocInfo)
549
                        {
550
                            var _delitem = _doc.DocPage.Where(p => p.PageNumber == item.PageNumber).First();
551
                             deepViewEntity.DocPage.DeleteObject(_delitem);
552
                            //deepViewEntity.DocPage.DeleteObject(item);
553
                        }
554
                        
555
                        deepViewEntity.SaveChanges();
556
557
                        foreach (var exceptitem in _exceptDocInfo)
558
                        {
559
                            var _lstPage = deepViewEntity.DocPage.Where(page => page.PageNumber == exceptitem.PageNumber
560
                                                                                    && page.DocInfo_Id == exceptitem.DocInfo_Id);
561
562
                            if (_lstPage.Count() > 0)
563
                            {
564
                                var _page = _lstPage.First();
565
                                _page.PageHeight = exceptitem.PageHeight;
566
                                _page.PageWidth = exceptitem.PageWidth;
567
                            }
568
                            else
569
                            {
570
                                _doc.DocPage.Add(pageSet.Where(p=>p.PageNumber == exceptitem.PageNumber).First());
571
                            }
572
                        }
573
574
                        deepViewEntity.SaveChanges();
575
576
                        _watch.Stop();
577
                        System.Diagnostics.Debug.WriteLine(_watch.ElapsedMilliseconds);
578
                    }
579
                    else
580
                    {
581
582
                        Guid guid = GuidGenerator.GetUniqueGuid();
583
                        DocInfo instace = new DocInfo
584
                        {
585
                            Id = guid,
586
                            OrginPDFUrl = ConverterItem.PdfUrl,
587
                            SharepointItemID = ConverterItem.SharepointItemID,
588
                            PageCount = pageCount,
589
                            DocPage = pageSet
590
                        };
591
                        deepViewEntity.DocInfo.AddObject(instace);
592
                    }
593
594
                    deepViewEntity.SaveChanges();
595
                }
596
            }
597
            catch (Exception e)
598
            {
599
                SendError("SetPageInfoToDB : " + e.ToString(),true);
600
                return false;
601
            }
602
               
603
            #endregion
604
605
            return true;
606
        }
607
    #endregion
608
609
        public void Dispose()
610
        {
611
            pdftron.PDFNet.Terminate();
612
            GC.Collect();
613
            GC.SuppressFinalize(this);
614
        }
615
     
616
    }
617
618
    public enum FinalStatus
619
    {
620
        /// <summary>
621
        /// Final PDF를 만들기 위한 순서에 추가
622
        /// </summary>
623
        Insert = 0,
624
625
        /// <summary>
626
        /// 앞의 final이 처리중일때 대기
627
        /// </summary>
628
        Wait = 1,
629
630
        /// <summary>
631
        /// 만들고 있는중
632
        /// 이때 데이터베이스에 Current Page는 만들고 있는 페이지
633
        /// </summary>
634
        Create = 2,
635
636
        /// <summary>
637
        /// final pdf 완료시
638
        /// </summary>
639
        Seccess = 99,
640
641
        /// <summary>
642
        /// 에러
643
        /// </summary>
644
        Error = 88
645
    }
646
647
}
클립보드 이미지 추가 (최대 크기: 500 MB)