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