1
|
|
2
|
using log4net;
|
3
|
using System;
|
4
|
using System.Diagnostics;
|
5
|
using System.Linq;
|
6
|
|
7
|
namespace DownloadManager
|
8
|
{
|
9
|
class Program
|
10
|
{
|
11
|
private static ILog logger = LogManager.GetLogger(typeof(Program));
|
12
|
|
13
|
private static string TEMP_FOLDER = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "MARKUS");
|
14
|
|
15
|
static IIpc.WcfClient wcfClient;
|
16
|
static bool IsDownload;
|
17
|
static bool IsDebug;
|
18
|
static int TotalPages = 0;
|
19
|
|
20
|
//private const string SECTION_DOWNLOAD_FILE = "DOWNLOAD_FILE";
|
21
|
|
22
|
//private const string SECTION_DOWNLOAD_DIR = "DOWNLOAD_DIR";
|
23
|
|
24
|
//private const string KEY_VALUES = "VALUE";
|
25
|
|
26
|
|
27
|
//private static string DOWNLOAD_LOG_FILE = System.IO.Path.Combine(TEMP_FOLDER, "Download.log");
|
28
|
|
29
|
//private static ConfigParser DOWNLOAD_LOG = new ConfigParser(DOWNLOAD_LOG_FILE,
|
30
|
// new ConfigParserSettings
|
31
|
// {
|
32
|
// MultiLineValues = MultiLineValues.Simple | MultiLineValues.AllowValuelessKeys | MultiLineValues.QuoteDelimitedValues,
|
33
|
// Culture = new CultureInfo("en-US")
|
34
|
// });
|
35
|
|
36
|
private static void ChangeLogFileName(string name)
|
37
|
{
|
38
|
log4net.Repository.ILoggerRepository RootRep;
|
39
|
RootRep = logger.Logger.Repository;// LogManager.GetRepository(Assembly.GetCallingAssembly());
|
40
|
|
41
|
if (RootRep?.GetAppenders().Count() > 0)
|
42
|
{
|
43
|
(RootRep.GetAppenders().First() as log4net.Appender.RollingFileAppender).File = string.Format((RootRep.GetAppenders().First() as log4net.Appender.RollingFileAppender).File, name);
|
44
|
(RootRep.GetAppenders().First() as log4net.Appender.RollingFileAppender).ActivateOptions();
|
45
|
}
|
46
|
}
|
47
|
|
48
|
static void Main(string[] args)
|
49
|
{
|
50
|
try
|
51
|
{
|
52
|
ChangeLogFileName($"{DateTime.Now.ToString("yyMMddssmm")}_{args[0]}");
|
53
|
|
54
|
logger.Info($"param : {string.Join(",", args)}");
|
55
|
|
56
|
if (args.Length > 0)
|
57
|
{
|
58
|
for (int i = 0; i < args.Length; i++)
|
59
|
{
|
60
|
ConsoleWrite(args[i]);
|
61
|
}
|
62
|
|
63
|
//if (args[0] == IIpc.ProcessTypeDefine.DEFINE_DELETE)
|
64
|
//{
|
65
|
// IsDebug = bool.Parse(args[1]);
|
66
|
|
67
|
// DeleteFiles();
|
68
|
//}
|
69
|
if (args[0] == IIpc.ProcessTypeDefine.DEFINE_MONITOR)
|
70
|
{
|
71
|
CheckAutoUpdate.Validation();
|
72
|
|
73
|
IsDebug = bool.Parse(args[1]);
|
74
|
int processId = int.Parse(args[2]); /// kcom process id
|
75
|
int thumbnamilPID = int.Parse(args[3]);
|
76
|
int filePID = int.Parse(args[4]);
|
77
|
|
78
|
string folder1 = args[5]; // folder
|
79
|
string folder2 = args[6]; // folder
|
80
|
string file = args[7]; // file
|
81
|
|
82
|
try
|
83
|
{
|
84
|
Process process = FindProcess(processId);
|
85
|
|
86
|
while (process != null)
|
87
|
{
|
88
|
process = FindProcess(processId);
|
89
|
|
90
|
ConsoleWrite("Process Check");
|
91
|
|
92
|
System.Threading.Thread.Sleep(10000);
|
93
|
}
|
94
|
|
95
|
var p1 = FindProcess(thumbnamilPID);
|
96
|
|
97
|
if (p1 != null)
|
98
|
{
|
99
|
p1.Kill();
|
100
|
}
|
101
|
|
102
|
|
103
|
var p2 = FindProcess(filePID);
|
104
|
|
105
|
if (p2 != null)
|
106
|
{
|
107
|
p2.Kill();
|
108
|
}
|
109
|
}
|
110
|
catch (Exception ex)
|
111
|
{
|
112
|
ConsoleWrite(ex.ToString());
|
113
|
}
|
114
|
|
115
|
DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder1);
|
116
|
DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder2);
|
117
|
DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_FILE, file);
|
118
|
}
|
119
|
else
|
120
|
{
|
121
|
wcfClient = new IIpc.WcfClient(args[1]);
|
122
|
IsDebug = bool.Parse(args[2]);
|
123
|
|
124
|
if (args[0] == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL)
|
125
|
{
|
126
|
string BaseUri = args[3];
|
127
|
string localStoragePath = args[4];
|
128
|
string fileExt = args[5];
|
129
|
int totalPages = int.Parse(args[6]);
|
130
|
int takeCount = int.Parse(args[6]);
|
131
|
|
132
|
TotalPages = totalPages;
|
133
|
|
134
|
KCOM.PageManager.PageStorage pageStorage = new KCOM.PageManager.PageStorage(BaseUri, localStoragePath, fileExt, totalPages, takeCount);
|
135
|
pageStorage.PageLoadCompleted += PageStorage_PageLoadCompleted;
|
136
|
pageStorage.DownloadWork(1, totalPages);
|
137
|
}
|
138
|
else if (args[0] == IIpc.ProcessTypeDefine.DEFINE_FILE)
|
139
|
{
|
140
|
using (System.Net.WebClient client = new System.Net.WebClient())
|
141
|
{
|
142
|
client.DownloadFileCompleted += Client_DownloadFileCompleted;
|
143
|
client.DownloadProgressChanged += Client_DownloadProgressChanged;
|
144
|
client.UseDefaultCredentials = true;
|
145
|
System.Net.IWebProxy webProxy = client.Proxy;
|
146
|
|
147
|
if (webProxy != null)
|
148
|
{
|
149
|
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
|
150
|
}
|
151
|
|
152
|
client.DownloadFileAsync(new Uri(System.Web.HttpUtility.UrlDecode(args[3])), args[4]);
|
153
|
}
|
154
|
}
|
155
|
|
156
|
while (!IsDownload)
|
157
|
{
|
158
|
System.Threading.Thread.Sleep(1000);
|
159
|
}
|
160
|
|
161
|
}
|
162
|
|
163
|
if (IsDebug)
|
164
|
{
|
165
|
Console.ReadLine();
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
catch (Exception ex)
|
170
|
{
|
171
|
logger.Error($"param : {string.Join(",", args)}", ex);
|
172
|
System.Diagnostics.Debug.WriteLine(ex);
|
173
|
}
|
174
|
}
|
175
|
|
176
|
public static void DeleteFiles(string processType, string path)
|
177
|
{
|
178
|
try
|
179
|
{
|
180
|
if (processType == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL)
|
181
|
{
|
182
|
var dir = System.IO.Directory.GetDirectories(path, "*.*", System.IO.SearchOption.AllDirectories);
|
183
|
|
184
|
for (int i = 0; i < dir.Length; i++)
|
185
|
{
|
186
|
System.IO.Directory.Delete(dir[i], true);
|
187
|
}
|
188
|
}
|
189
|
else
|
190
|
{
|
191
|
if (System.IO.File.Exists(path))
|
192
|
{
|
193
|
System.IO.File.Delete(path);
|
194
|
}
|
195
|
}
|
196
|
}
|
197
|
catch (Exception ex)
|
198
|
{
|
199
|
logger.Error($"DeleteFiles Error {processType} {path}", ex);
|
200
|
System.Diagnostics.Debug.WriteLine(ex);
|
201
|
}
|
202
|
|
203
|
}
|
204
|
|
205
|
private static Process FindProcess(int id)
|
206
|
{
|
207
|
Process result = null;
|
208
|
|
209
|
try
|
210
|
{
|
211
|
var process = Process.GetProcesses().Where(x=>x.Id == id);
|
212
|
|
213
|
if(process.Count()> 0)
|
214
|
{
|
215
|
result = process.First();
|
216
|
}
|
217
|
}
|
218
|
catch (Exception ex)
|
219
|
{
|
220
|
logger.Error($"FindProcess Error {id}", ex);
|
221
|
System.Diagnostics.Debug.WriteLine(ex);
|
222
|
}
|
223
|
|
224
|
return result;
|
225
|
}
|
226
|
|
227
|
|
228
|
private static void ConsoleWrite(string data)
|
229
|
{
|
230
|
logger.Info(data);
|
231
|
|
232
|
if (IsDebug)
|
233
|
{
|
234
|
Console.WriteLine(data);
|
235
|
}
|
236
|
}
|
237
|
|
238
|
|
239
|
private static void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
|
240
|
{
|
241
|
try
|
242
|
{
|
243
|
wcfClient.SendFileDownloadReceived(100, true);
|
244
|
|
245
|
ConsoleWrite($"Download file Completed");
|
246
|
IsDownload = true;
|
247
|
}
|
248
|
catch (Exception ex)
|
249
|
{
|
250
|
ConsoleWrite(ex.ToString());
|
251
|
}
|
252
|
}
|
253
|
|
254
|
private static void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
|
255
|
{
|
256
|
try
|
257
|
{
|
258
|
wcfClient.SendFileDownloadReceived(e.ProgressPercentage, false);
|
259
|
|
260
|
ConsoleWrite($"Download file {e.ProgressPercentage}");
|
261
|
}
|
262
|
catch (Exception ex)
|
263
|
{
|
264
|
ConsoleWrite(ex.ToString());
|
265
|
}
|
266
|
}
|
267
|
|
268
|
private static void PageStorage_PageLoadCompleted(object sender, KCOM.PageManager.PageLoadCompletedEventArgs e)
|
269
|
{
|
270
|
try
|
271
|
{
|
272
|
wcfClient.SendThumbnailReceived(e.PageItem.PageNo, e.PageItem.LocalFilePath);
|
273
|
|
274
|
ConsoleWrite($"{e.PageItem.PageNo} {e.PageItem.LocalUri}");
|
275
|
|
276
|
if (e.PageItem.PageNo == TotalPages)
|
277
|
{
|
278
|
IsDownload = true;
|
279
|
}
|
280
|
}
|
281
|
catch (Exception ex)
|
282
|
{
|
283
|
ConsoleWrite(ex.ToString());
|
284
|
}
|
285
|
|
286
|
}
|
287
|
}
|
288
|
}
|