markus / DownloadManager / Program.cs @ master
이력 | 보기 | 이력해설 | 다운로드 (14.8 KB)
1 |
|
---|---|
2 |
using log4net; |
3 |
using RestSharp; |
4 |
using System; |
5 |
using System.Diagnostics; |
6 |
using System.IO; |
7 |
using System.Linq; |
8 |
using System.Net; |
9 |
using System.Net.Http; |
10 |
using System.Net.Http.Headers; |
11 |
using System.Threading.Tasks; |
12 |
|
13 |
namespace DownloadManager |
14 |
{ |
15 |
class Program |
16 |
{ |
17 |
private static ILog logger = LogManager.GetLogger(typeof(Program)); |
18 |
|
19 |
private static string TEMP_FOLDER = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "MARKUS"); |
20 |
|
21 |
static IIpc.WcfClient wcfClient; |
22 |
static bool IsDownload; |
23 |
static bool IsDebug; |
24 |
static int TotalPages = 0; |
25 |
|
26 |
//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 |
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 |
static void Main(string[] args) |
55 |
{ |
56 |
try |
57 |
{ |
58 |
#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 |
ChangeLogFileName($"{DateTime.Now.ToString("yyMMddssmm")}_{args[0]}"); |
63 |
|
64 |
logger.Info($"param : {string.Join(",", args)}"); |
65 |
|
66 |
if (args.Length > 0) |
67 |
{ |
68 |
for (int i = 0; i < args.Length; i++) |
69 |
{ |
70 |
ConsoleWrite(args[i]); |
71 |
} |
72 |
|
73 |
//if (args[0] == IIpc.ProcessTypeDefine.DEFINE_DELETE) |
74 |
//{ |
75 |
// IsDebug = bool.Parse(args[1]); |
76 |
|
77 |
// 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 |
string baseUri = args[8]; // uri |
91 |
|
92 |
CheckAutoUpdate.Validation(baseUri); |
93 |
|
94 |
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 |
{ |
133 |
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 |
|
146 |
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 |
//var token = Authenticate(new Uri(System.Web.HttpUtility.UrlDecode(args[3])).ToString()); |
153 |
|
154 |
using (System.Net.WebClient client = new System.Net.WebClient()) |
155 |
{ |
156 |
//client.Headers.Add(HttpRequestHeader.Authorization, token); |
157 |
|
158 |
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 |
client.DownloadFileAsync(new Uri(System.Web.HttpUtility.UrlDecode(args[3])), args[4]); |
169 |
} |
170 |
} |
171 |
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 |
|
218 |
while (!IsDownload) |
219 |
{ |
220 |
System.Threading.Thread.Sleep(1000); |
221 |
} |
222 |
|
223 |
} |
224 |
|
225 |
if (IsDebug) |
226 |
{ |
227 |
Console.ReadLine(); |
228 |
} |
229 |
} |
230 |
} |
231 |
catch (Exception ex) |
232 |
{ |
233 |
logger.Error($"param : {string.Join(",", args)}", ex); |
234 |
System.Diagnostics.Debug.WriteLine(ex); |
235 |
} |
236 |
} |
237 |
|
238 |
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 |
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 |
public static void DeleteFiles(string processType, string path) |
295 |
{ |
296 |
try |
297 |
{ |
298 |
logger.Info($"DeleteFiles Info {processType} {path}"); |
299 |
|
300 |
if (processType == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL) |
301 |
{ |
302 |
var dir = System.IO.Directory.GetDirectories(path, "*.*", System.IO.SearchOption.AllDirectories); |
303 |
|
304 |
for (int i = 0; i < dir.Length; i++) |
305 |
{ |
306 |
System.IO.Directory.Delete(dir[i], true); |
307 |
} |
308 |
} |
309 |
else |
310 |
{ |
311 |
if (System.IO.File.Exists(path)) |
312 |
{ |
313 |
System.IO.File.Delete(path); |
314 |
} |
315 |
} |
316 |
} |
317 |
catch (Exception ex) |
318 |
{ |
319 |
logger.Error($"DeleteFiles Error {processType} {path}", ex); |
320 |
System.Diagnostics.Debug.WriteLine(ex); |
321 |
} |
322 |
|
323 |
} |
324 |
|
325 |
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 |
{ |
335 |
result = process.First(); |
336 |
} |
337 |
} |
338 |
catch (Exception ex) |
339 |
{ |
340 |
logger.Error($"FindProcess Error {id}", ex); |
341 |
System.Diagnostics.Debug.WriteLine(ex); |
342 |
} |
343 |
|
344 |
return result; |
345 |
} |
346 |
|
347 |
|
348 |
private static void ConsoleWrite(string data) |
349 |
{ |
350 |
logger.Info(data); |
351 |
|
352 |
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 |
wcfClient.SendThumbnailReceived(e.PageItem.PageNo, e.PageItem.LocalFilePath,e.IsLast); |
393 |
|
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 |
} |