markus / DownloadManager / Program.cs @ 8295a079
이력 | 보기 | 이력해설 | 다운로드 (14.8 KB)
1 | 9d5b4bc2 | taeseongkim | |
---|---|---|---|
2 | 664ea2e1 | taeseongkim | using log4net; |
3 | 77cdac33 | taeseongkim | using RestSharp; |
4 | 9d5b4bc2 | taeseongkim | using System; |
5 | using System.Diagnostics; |
||
6 | 77cdac33 | taeseongkim | using System.IO; |
7 | 2007ecaa | taeseongkim | using System.Linq; |
8 | 2b1f30fe | taeseongkim | using System.Net; |
9 | 77cdac33 | taeseongkim | using System.Net.Http; |
10 | using System.Net.Http.Headers; |
||
11 | using System.Threading.Tasks; |
||
12 | 2007ecaa | taeseongkim | |
13 | namespace DownloadManager |
||
14 | { |
||
15 | class Program |
||
16 | { |
||
17 | 664ea2e1 | taeseongkim | private static ILog logger = LogManager.GetLogger(typeof(Program)); |
18 | |||
19 | 9d5b4bc2 | taeseongkim | private static string TEMP_FOLDER = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "MARKUS"); |
20 | |||
21 | 2007ecaa | taeseongkim | static IIpc.WcfClient wcfClient; |
22 | static bool IsDownload; |
||
23 | static bool IsDebug; |
||
24 | static int TotalPages = 0; |
||
25 | |||
26 | 9d5b4bc2 | taeseongkim | //private const string SECTION_DOWNLOAD_FILE = "DOWNLOAD_FILE"; |
27 | |||
28 | //private const string SECTION_DOWNLOAD_DIR = "DOWNLOAD_DIR"; |
||
29 | |||
30 | //private const string KEY_VALUES = "VALUE"; |
||
31 | |||
32 | |||
33 | //private static string DOWNLOAD_LOG_FILE = System.IO.Path.Combine(TEMP_FOLDER, "Download.log"); |
||
34 | |||
35 | //private static ConfigParser DOWNLOAD_LOG = new ConfigParser(DOWNLOAD_LOG_FILE, |
||
36 | // new ConfigParserSettings |
||
37 | // { |
||
38 | // MultiLineValues = MultiLineValues.Simple | MultiLineValues.AllowValuelessKeys | MultiLineValues.QuoteDelimitedValues, |
||
39 | // Culture = new CultureInfo("en-US") |
||
40 | // }); |
||
41 | |||
42 | 1223cf64 | taeseongkim | private static void ChangeLogFileName(string name) |
43 | { |
||
44 | log4net.Repository.ILoggerRepository RootRep; |
||
45 | RootRep = logger.Logger.Repository;// LogManager.GetRepository(Assembly.GetCallingAssembly()); |
||
46 | |||
47 | if (RootRep?.GetAppenders().Count() > 0) |
||
48 | { |
||
49 | (RootRep.GetAppenders().First() as log4net.Appender.RollingFileAppender).File = string.Format((RootRep.GetAppenders().First() as log4net.Appender.RollingFileAppender).File, name); |
||
50 | (RootRep.GetAppenders().First() as log4net.Appender.RollingFileAppender).ActivateOptions(); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | 2007ecaa | taeseongkim | static void Main(string[] args) |
55 | { |
||
56 | 9d5b4bc2 | taeseongkim | try |
57 | 2007ecaa | taeseongkim | { |
58 | 66bd3240 | taeseongkim | #if XACNA_TEST |
59 | args = ("MONITOR False 78820 79568 75952 C:\\Users\\kts\\AppData\\Local\\Temp\\MARKUS\\qxpvmgdt.pmr C:\\Users\\kts\\AppData\\Local\\Temp\\MARKUS\\ehdjbhoj.2j5 C:\\Users\\kts\\AppData\\Local\\Temp\\MARKUS\\ow0bp4w4.pdf http://markus.xicna.com:5977/API_V4") |
||
60 | .Split(' ').ToArray(); |
||
61 | #endif |
||
62 | 1223cf64 | taeseongkim | ChangeLogFileName($"{DateTime.Now.ToString("yyMMddssmm")}_{args[0]}"); |
63 | |||
64 | 664ea2e1 | taeseongkim | logger.Info($"param : {string.Join(",", args)}"); |
65 | 2007ecaa | taeseongkim | |
66 | 9d5b4bc2 | taeseongkim | if (args.Length > 0) |
67 | 2007ecaa | taeseongkim | { |
68 | 9d5b4bc2 | taeseongkim | for (int i = 0; i < args.Length; i++) |
69 | { |
||
70 | ConsoleWrite(args[i]); |
||
71 | } |
||
72 | 2007ecaa | taeseongkim | |
73 | 9d5b4bc2 | taeseongkim | //if (args[0] == IIpc.ProcessTypeDefine.DEFINE_DELETE) |
74 | //{ |
||
75 | // IsDebug = bool.Parse(args[1]); |
||
76 | 2007ecaa | taeseongkim | |
77 | 9d5b4bc2 | taeseongkim | // DeleteFiles(); |
78 | //} |
||
79 | if (args[0] == IIpc.ProcessTypeDefine.DEFINE_MONITOR) |
||
80 | { |
||
81 | IsDebug = bool.Parse(args[1]); |
||
82 | int processId = int.Parse(args[2]); /// kcom process id |
||
83 | int thumbnamilPID = int.Parse(args[3]); |
||
84 | int filePID = int.Parse(args[4]); |
||
85 | |||
86 | string folder1 = args[5]; // folder |
||
87 | string folder2 = args[6]; // folder |
||
88 | string file = args[7]; // file |
||
89 | |||
90 | a1e2ba68 | taeseongkim | string baseUri = args[8]; // uri |
91 | |||
92 | CheckAutoUpdate.Validation(baseUri); |
||
93 | |||
94 | 9d5b4bc2 | taeseongkim | try |
95 | { |
||
96 | Process process = FindProcess(processId); |
||
97 | |||
98 | while (process != null) |
||
99 | { |
||
100 | process = FindProcess(processId); |
||
101 | |||
102 | ConsoleWrite("Process Check"); |
||
103 | |||
104 | System.Threading.Thread.Sleep(10000); |
||
105 | } |
||
106 | |||
107 | var p1 = FindProcess(thumbnamilPID); |
||
108 | |||
109 | if (p1 != null) |
||
110 | { |
||
111 | p1.Kill(); |
||
112 | } |
||
113 | |||
114 | |||
115 | var p2 = FindProcess(filePID); |
||
116 | |||
117 | if (p2 != null) |
||
118 | { |
||
119 | p2.Kill(); |
||
120 | } |
||
121 | } |
||
122 | catch (Exception ex) |
||
123 | { |
||
124 | ConsoleWrite(ex.ToString()); |
||
125 | } |
||
126 | |||
127 | DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder1); |
||
128 | DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder2); |
||
129 | DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_FILE, file); |
||
130 | } |
||
131 | else |
||
132 | 2007ecaa | taeseongkim | { |
133 | 9d5b4bc2 | taeseongkim | wcfClient = new IIpc.WcfClient(args[1]); |
134 | IsDebug = bool.Parse(args[2]); |
||
135 | |||
136 | if (args[0] == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL) |
||
137 | { |
||
138 | string BaseUri = args[3]; |
||
139 | string localStoragePath = args[4]; |
||
140 | string fileExt = args[5]; |
||
141 | int totalPages = int.Parse(args[6]); |
||
142 | int takeCount = int.Parse(args[6]); |
||
143 | |||
144 | TotalPages = totalPages; |
||
145 | 2007ecaa | taeseongkim | |
146 | 9d5b4bc2 | taeseongkim | KCOM.PageManager.PageStorage pageStorage = new KCOM.PageManager.PageStorage(BaseUri, localStoragePath, fileExt, totalPages, takeCount); |
147 | pageStorage.PageLoadCompleted += PageStorage_PageLoadCompleted; |
||
148 | pageStorage.DownloadWork(1, totalPages); |
||
149 | } |
||
150 | else if (args[0] == IIpc.ProcessTypeDefine.DEFINE_FILE) |
||
151 | { |
||
152 | 66bd3240 | taeseongkim | //var token = Authenticate(new Uri(System.Web.HttpUtility.UrlDecode(args[3])).ToString()); |
153 | 2b1f30fe | taeseongkim | |
154 | 9d5b4bc2 | taeseongkim | using (System.Net.WebClient client = new System.Net.WebClient()) |
155 | { |
||
156 | 66bd3240 | taeseongkim | //client.Headers.Add(HttpRequestHeader.Authorization, token); |
157 | 2b1f30fe | taeseongkim | |
158 | 9d5b4bc2 | taeseongkim | client.DownloadFileCompleted += Client_DownloadFileCompleted; |
159 | client.DownloadProgressChanged += Client_DownloadProgressChanged; |
||
160 | client.UseDefaultCredentials = true; |
||
161 | System.Net.IWebProxy webProxy = client.Proxy; |
||
162 | |||
163 | if (webProxy != null) |
||
164 | { |
||
165 | webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; |
||
166 | } |
||
167 | |||
168 | 664ea2e1 | taeseongkim | client.DownloadFileAsync(new Uri(System.Web.HttpUtility.UrlDecode(args[3])), args[4]); |
169 | 9d5b4bc2 | taeseongkim | } |
170 | } |
||
171 | 77cdac33 | taeseongkim | else if (args[0] == IIpc.ProcessTypeDefine.DEFINE_REST_FILE) |
172 | { |
||
173 | var result = RestDownloadAsync(System.Web.HttpUtility.UrlDecode(args[3]), args[4]).GetAwaiter().GetResult(); |
||
174 | |||
175 | if (!IsDebug) |
||
176 | wcfClient.SendFileDownloadReceived(100, true); |
||
177 | |||
178 | ConsoleWrite($"Download stream {args[4]} Completed"); |
||
179 | IsDownload = result; |
||
180 | |||
181 | //var client = new RestClient(System.Web.HttpUtility.UrlDecode(args[3])); |
||
182 | //client.Timeout = -1; |
||
183 | //var request = new RestRequest(Method.GET); |
||
184 | //IRestResponse response = client.Execute(request); |
||
185 | |||
186 | //var fileInfo = new FileInfo(args[4]); |
||
187 | |||
188 | //using (var fileStream = fileInfo.OpenWrite()) |
||
189 | //{ |
||
190 | // int position = 0; |
||
191 | // int step = 300000; |
||
192 | // int length = 300000; |
||
193 | |||
194 | // while (position < response.RawBytes.Length) |
||
195 | // { |
||
196 | // if (position + step < response.RawBytes.Length) |
||
197 | // { |
||
198 | // length = position + step; |
||
199 | // } |
||
200 | // else |
||
201 | // { |
||
202 | // length = response.RawBytes.Length - position; |
||
203 | // } |
||
204 | |||
205 | // fileStream.Write(response.RawBytes, position, length); |
||
206 | |||
207 | // position = position + step; |
||
208 | // } |
||
209 | |||
210 | // if (!IsDebug) |
||
211 | // wcfClient.SendFileDownloadReceived(100, true); |
||
212 | |||
213 | // ConsoleWrite($"Download stream {args[4]} Completed"); |
||
214 | // IsDownload = true; |
||
215 | //} |
||
216 | } |
||
217 | 9d5b4bc2 | taeseongkim | |
218 | while (!IsDownload) |
||
219 | 2007ecaa | taeseongkim | { |
220 | 9d5b4bc2 | taeseongkim | System.Threading.Thread.Sleep(1000); |
221 | 2007ecaa | taeseongkim | } |
222 | |||
223 | 9d5b4bc2 | taeseongkim | } |
224 | |||
225 | if (IsDebug) |
||
226 | { |
||
227 | Console.ReadLine(); |
||
228 | 2007ecaa | taeseongkim | } |
229 | } |
||
230 | 9d5b4bc2 | taeseongkim | } |
231 | catch (Exception ex) |
||
232 | { |
||
233 | 664ea2e1 | taeseongkim | logger.Error($"param : {string.Join(",", args)}", ex); |
234 | 9d5b4bc2 | taeseongkim | System.Diagnostics.Debug.WriteLine(ex); |
235 | } |
||
236 | } |
||
237 | |||
238 | 2b1f30fe | taeseongkim | static string Authenticate(string baseUri) |
239 | { |
||
240 | string result = null; |
||
241 | |||
242 | try |
||
243 | { |
||
244 | Uri uri = new Uri(baseUri); |
||
245 | string baseUrl = uri.Scheme + "://" + uri.Host + (uri.IsDefaultPort ? "" : ":" + uri.Port); |
||
246 | |||
247 | using (System.Net.WebClient client = new System.Net.WebClient()) |
||
248 | { |
||
249 | client.Headers.Add(HttpRequestHeader.Authorization, "!dsfadsfa@@~"); |
||
250 | var response = client.DownloadString(baseUrl + "/Authenticate"); |
||
251 | |||
252 | if (response != null) |
||
253 | { |
||
254 | result = response; |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | catch (Exception) |
||
259 | { |
||
260 | |||
261 | throw; |
||
262 | } |
||
263 | |||
264 | return result; |
||
265 | } |
||
266 | |||
267 | 77cdac33 | taeseongkim | private static async Task<bool> RestDownloadAsync(string uri, string savePath) |
268 | { |
||
269 | bool result = false; |
||
270 | |||
271 | try |
||
272 | { |
||
273 | var client = new RestClient(uri); |
||
274 | client.Timeout = -1; |
||
275 | var request = new RestRequest(Method.GET); |
||
276 | IRestResponse response = await client.ExecuteAsync(request); |
||
277 | |||
278 | if (response.StatusCode == System.Net.HttpStatusCode.OK) |
||
279 | { |
||
280 | var fs = File.Create(savePath); |
||
281 | |||
282 | await fs.WriteAsync(response.RawBytes, 0, response.RawBytes.Length); |
||
283 | fs.Close(); |
||
284 | result = true; |
||
285 | } |
||
286 | } |
||
287 | catch (Exception ex) |
||
288 | { |
||
289 | logger.Error($"RestDownloadAsync : {uri} {savePath}", ex); |
||
290 | } |
||
291 | return result; |
||
292 | } |
||
293 | |||
294 | 9d5b4bc2 | taeseongkim | public static void DeleteFiles(string processType, string path) |
295 | { |
||
296 | 664ea2e1 | taeseongkim | try |
297 | 9d5b4bc2 | taeseongkim | { |
298 | 75f6ff19 | taeseongkim | logger.Info($"DeleteFiles Info {processType} {path}"); |
299 | |||
300 | 664ea2e1 | taeseongkim | if (processType == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL) |
301 | { |
||
302 | var dir = System.IO.Directory.GetDirectories(path, "*.*", System.IO.SearchOption.AllDirectories); |
||
303 | 2007ecaa | taeseongkim | |
304 | 664ea2e1 | taeseongkim | for (int i = 0; i < dir.Length; i++) |
305 | { |
||
306 | System.IO.Directory.Delete(dir[i], true); |
||
307 | } |
||
308 | } |
||
309 | else |
||
310 | 9d5b4bc2 | taeseongkim | { |
311 | 664ea2e1 | taeseongkim | if (System.IO.File.Exists(path)) |
312 | { |
||
313 | System.IO.File.Delete(path); |
||
314 | } |
||
315 | 9d5b4bc2 | taeseongkim | } |
316 | } |
||
317 | 664ea2e1 | taeseongkim | catch (Exception ex) |
318 | 9d5b4bc2 | taeseongkim | { |
319 | 664ea2e1 | taeseongkim | logger.Error($"DeleteFiles Error {processType} {path}", ex); |
320 | System.Diagnostics.Debug.WriteLine(ex); |
||
321 | 9d5b4bc2 | taeseongkim | } |
322 | 664ea2e1 | taeseongkim | |
323 | 9d5b4bc2 | taeseongkim | } |
324 | 2007ecaa | taeseongkim | |
325 | 9d5b4bc2 | taeseongkim | private static Process FindProcess(int id) |
326 | { |
||
327 | Process result = null; |
||
328 | |||
329 | try |
||
330 | { |
||
331 | var process = Process.GetProcesses().Where(x=>x.Id == id); |
||
332 | |||
333 | if(process.Count()> 0) |
||
334 | 2007ecaa | taeseongkim | { |
335 | 9d5b4bc2 | taeseongkim | result = process.First(); |
336 | 2007ecaa | taeseongkim | } |
337 | } |
||
338 | 664ea2e1 | taeseongkim | catch (Exception ex) |
339 | 9d5b4bc2 | taeseongkim | { |
340 | 664ea2e1 | taeseongkim | logger.Error($"FindProcess Error {id}", ex); |
341 | System.Diagnostics.Debug.WriteLine(ex); |
||
342 | 9d5b4bc2 | taeseongkim | } |
343 | |||
344 | return result; |
||
345 | 2007ecaa | taeseongkim | } |
346 | |||
347 | 9d5b4bc2 | taeseongkim | |
348 | 2007ecaa | taeseongkim | private static void ConsoleWrite(string data) |
349 | { |
||
350 | 664ea2e1 | taeseongkim | logger.Info(data); |
351 | |||
352 | 2007ecaa | taeseongkim | if (IsDebug) |
353 | { |
||
354 | Console.WriteLine(data); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | |||
359 | private static void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) |
||
360 | { |
||
361 | try |
||
362 | { |
||
363 | wcfClient.SendFileDownloadReceived(100, true); |
||
364 | |||
365 | ConsoleWrite($"Download file Completed"); |
||
366 | IsDownload = true; |
||
367 | } |
||
368 | catch (Exception ex) |
||
369 | { |
||
370 | ConsoleWrite(ex.ToString()); |
||
371 | } |
||
372 | } |
||
373 | |||
374 | private static void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e) |
||
375 | { |
||
376 | try |
||
377 | { |
||
378 | wcfClient.SendFileDownloadReceived(e.ProgressPercentage, false); |
||
379 | |||
380 | ConsoleWrite($"Download file {e.ProgressPercentage}"); |
||
381 | } |
||
382 | catch (Exception ex) |
||
383 | { |
||
384 | ConsoleWrite(ex.ToString()); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | private static void PageStorage_PageLoadCompleted(object sender, KCOM.PageManager.PageLoadCompletedEventArgs e) |
||
389 | { |
||
390 | try |
||
391 | { |
||
392 | 6a19b48d | taeseongkim | wcfClient.SendThumbnailReceived(e.PageItem.PageNo, e.PageItem.LocalFilePath,e.IsLast); |
393 | 2007ecaa | taeseongkim | |
394 | ConsoleWrite($"{e.PageItem.PageNo} {e.PageItem.LocalUri}"); |
||
395 | |||
396 | if (e.PageItem.PageNo == TotalPages) |
||
397 | { |
||
398 | IsDownload = true; |
||
399 | } |
||
400 | } |
||
401 | catch (Exception ex) |
||
402 | { |
||
403 | ConsoleWrite(ex.ToString()); |
||
404 | } |
||
405 | |||
406 | } |
||
407 | } |
||
408 | } |