markus / KCOM / PageManager / PageStorage.cs @ 6a19b48d
이력 | 보기 | 이력해설 | 다운로드 (13.6 KB)
1 | d7e20d2d | taeseongkim | using System; |
---|---|---|---|
2 | 2007ecaa | taeseongkim | using System.Collections.Concurrent; |
3 | d7e20d2d | taeseongkim | using System.Collections.Generic; |
4 | using System.ComponentModel; |
||
5 | using System.Linq; |
||
6 | using System.Text; |
||
7 | using System.Threading.Tasks; |
||
8 | using System.Windows.Media.Imaging; |
||
9 | |||
10 | namespace KCOM.PageManager |
||
11 | { |
||
12 | public class PageStorage |
||
13 | { |
||
14 | private const int DEFUALT_TALK_PAGE_COUNT = 10; |
||
15 | 2007ecaa | taeseongkim | |
16 | public event EventHandler<PageLoadCompletedEventArgs> PageLoadCompleted; |
||
17 | |||
18 | List<int> WorkItems = new List<int>(); |
||
19 | ConcurrentBag<PageItem> fileItems = new ConcurrentBag<PageItem>(); |
||
20 | d7e20d2d | taeseongkim | BackgroundWorker backgroundWorker; |
21 | 2007ecaa | taeseongkim | |
22 | //List<PageItem> fileItems = new List<PageItem>(); |
||
23 | 9d5b4bc2 | taeseongkim | public string LocalStorage; |
24 | 54a28343 | taeseongkim | string _fileExt; |
25 | d7e20d2d | taeseongkim | string _BaseUri; |
26 | int _TotalPages; |
||
27 | 54a28343 | taeseongkim | int _TakeCount; |
28 | d7e20d2d | taeseongkim | |
29 | 6a19b48d | taeseongkim | bool IsBusy = false; |
30 | d7e20d2d | taeseongkim | |
31 | BitmapFrame PageImage; |
||
32 | |||
33 | 2007ecaa | taeseongkim | public PageStorage(string BaseUri, string localStoragePath, string fileExt, int totalPages, int takeCount = 5) |
34 | d7e20d2d | taeseongkim | { |
35 | try |
||
36 | { |
||
37 | 2007ecaa | taeseongkim | backgroundWorker = new BackgroundWorker { WorkerSupportsCancellation = true, WorkerReportsProgress = true }; |
38 | d7e20d2d | taeseongkim | backgroundWorker.DoWork += BackgroundWorker_DoWork; |
39 | 2007ecaa | taeseongkim | backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged; |
40 | 6a19b48d | taeseongkim | |
41 | 54a28343 | taeseongkim | _fileExt = fileExt; |
42 | d7e20d2d | taeseongkim | _BaseUri = BaseUri; |
43 | 9d5b4bc2 | taeseongkim | LocalStorage = localStoragePath; |
44 | d7e20d2d | taeseongkim | _TotalPages = totalPages; |
45 | 54a28343 | taeseongkim | _TakeCount = takeCount; |
46 | d7e20d2d | taeseongkim | |
47 | 77cdac33 | taeseongkim | System.IO.DirectoryInfo info = System.IO.Directory.CreateDirectory(LocalStorage); |
48 | d7e20d2d | taeseongkim | |
49 | //backgroundWorker.RunWorkerAsync(new int[] { 1, 10 }); |
||
50 | } |
||
51 | catch (Exception ex) |
||
52 | { |
||
53 | throw new Exception("PageStorage", ex); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | 2007ecaa | taeseongkim | private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) |
58 | d7e20d2d | taeseongkim | { |
59 | 2007ecaa | taeseongkim | System.Diagnostics.Debug.WriteLine(_fileExt + ":" + e.ProgressPercentage.ToString() + "%"); |
60 | } |
||
61 | d7e20d2d | taeseongkim | |
62 | 2007ecaa | taeseongkim | private async void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) |
63 | { |
||
64 | d7e20d2d | taeseongkim | |
65 | 2007ecaa | taeseongkim | while (WorkItems.Count > 0) |
66 | { |
||
67 | 6a19b48d | taeseongkim | if (!IsBusy) |
68 | d7e20d2d | taeseongkim | { |
69 | 2007ecaa | taeseongkim | int item = -1; |
70 | try |
||
71 | { |
||
72 | d7e20d2d | taeseongkim | |
73 | 2007ecaa | taeseongkim | if (WorkItems.TryFrist(true, out item)) |
74 | { |
||
75 | var result = await DownloadPageAsync(item, null); |
||
76 | d7e20d2d | taeseongkim | |
77 | 2007ecaa | taeseongkim | if (!result.IsDownLoad) |
78 | { |
||
79 | System.Threading.Thread.Sleep(100); |
||
80 | } |
||
81 | await Task.Delay(100); |
||
82 | //System.Threading.Thread.Sleep(10); |
||
83 | //backgroundWorker.ReportProgress(fileItems.Count / _TotalPages * 100); |
||
84 | } |
||
85 | } |
||
86 | catch (Exception ex) |
||
87 | { |
||
88 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
89 | } |
||
90 | d7e20d2d | taeseongkim | } |
91 | } |
||
92 | } |
||
93 | |||
94 | public void ResetImage() |
||
95 | { |
||
96 | //if (PageImage != null) |
||
97 | //{ |
||
98 | // PageImage = null; |
||
99 | //} |
||
100 | |||
101 | //PageImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache; |
||
102 | |||
103 | } |
||
104 | |||
105 | 2007ecaa | taeseongkim | public async Task<BitmapFrame> GetPageImageAsync(System.Threading.CancellationToken cts, int PageNo, bool IsRestart = false) |
106 | d7e20d2d | taeseongkim | { |
107 | try |
||
108 | { |
||
109 | 54a28343 | taeseongkim | |
110 | 6a19b48d | taeseongkim | var localUri = await GetPageUriAsync(cts, PageNo); |
111 | 54a28343 | taeseongkim | |
112 | 6a19b48d | taeseongkim | if (localUri != null) // || !cts.IsCancellationRequested |
113 | 24c5e56c | taeseongkim | { |
114 | PageImage = BitmapFrame.Create(localUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); |
||
115 | } |
||
116 | 54a28343 | taeseongkim | } |
117 | catch (Exception ex) |
||
118 | { |
||
119 | 04d21859 | taeseongkim | PageImage = BitmapFrame.Create(new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString())), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); |
120 | 54a28343 | taeseongkim | } |
121 | finally |
||
122 | { |
||
123 | } |
||
124 | |||
125 | return PageImage; |
||
126 | } |
||
127 | |||
128 | 2007ecaa | taeseongkim | private void DownloadWorkAsync(int startPage, int TakeCount) |
129 | { |
||
130 | |||
131 | lock (WorkItems) |
||
132 | { |
||
133 | WorkItems.AddRange(Enumerable.Range(startPage, TakeCount)); |
||
134 | } |
||
135 | |||
136 | if (TakeCount == _TotalPages) |
||
137 | { |
||
138 | _TakeCount = 0; |
||
139 | } |
||
140 | |||
141 | // var files = fileItems.Select(x => x.PageNo); |
||
142 | |||
143 | |||
144 | //for (int i = startPage; i < TakeCount + 1; i++) |
||
145 | //{ |
||
146 | // if (i < _TotalPages && !WorkItems.Contains(i) && !files.Contains(i)) |
||
147 | // { |
||
148 | // WorkItems.Add(i); |
||
149 | // // System.Threading.Thread.Sleep(1); |
||
150 | // } |
||
151 | // else if(i == _TotalPages) |
||
152 | // { |
||
153 | // _TakeCount = 0; |
||
154 | // } |
||
155 | //} |
||
156 | |||
157 | if (!backgroundWorker.IsBusy && WorkItems.Count() > 0) |
||
158 | { |
||
159 | backgroundWorker.RunWorkerAsync(); |
||
160 | } |
||
161 | |||
162 | //await Task.Delay(10);// System.Threading.Thread.Sleep(10); |
||
163 | //while (WorkItems.Count > 0) |
||
164 | //{ |
||
165 | // int item = -1; |
||
166 | |||
167 | // if (WorkItems.TryDequeue(out item)) |
||
168 | // { |
||
169 | // var result = await DownloadPageAsync(item, null); |
||
170 | // } |
||
171 | |||
172 | //} |
||
173 | |||
174 | } |
||
175 | |||
176 | 43a743e4 | taeseongkim | /// <summary> |
177 | /// |
||
178 | /// </summary> |
||
179 | /// <param name="cts"></param> |
||
180 | /// <param name="PageNo"></param> |
||
181 | /// <param name="IsClone"></param> |
||
182 | /// <param name="IsRestart"></param> |
||
183 | /// <returns></returns> |
||
184 | 2007ecaa | taeseongkim | public async Task<Uri> GetPageUriAsync(System.Threading.CancellationToken? cts, int PageNo, bool IsRestart = false) |
185 | 54a28343 | taeseongkim | { |
186 | Uri result = null; |
||
187 | |||
188 | try |
||
189 | { |
||
190 | d7e20d2d | taeseongkim | System.Diagnostics.Debug.WriteLine("GetPageAsync"); |
191 | 54a28343 | taeseongkim | |
192 | 2007ecaa | taeseongkim | var pageItem = await DownloadPageAsync(PageNo, cts); |
193 | |||
194 | 6a19b48d | taeseongkim | if (pageItem != null) |
195 | d7e20d2d | taeseongkim | { |
196 | 6a19b48d | taeseongkim | result = pageItem.LocalUri; |
197 | d7e20d2d | taeseongkim | |
198 | 6a19b48d | taeseongkim | if(PageNo + _TakeCount < _TotalPages) |
199 | d7e20d2d | taeseongkim | { |
200 | 6a19b48d | taeseongkim | int takecount = 5; |
201 | |||
202 | if (_TotalPages < PageNo + takecount) |
||
203 | { |
||
204 | takecount = takecount - (PageNo + takecount - _TotalPages) - 1; |
||
205 | } |
||
206 | |||
207 | DownloadWorkAsync(PageNo + 1, takecount); |
||
208 | 2007ecaa | taeseongkim | } |
209 | 6a19b48d | taeseongkim | //int takecount = _TakeCount; |
210 | |||
211 | //if (_TotalPages < PageNo + takecount) |
||
212 | //{ |
||
213 | // takecount = takecount - (PageNo + takecount - _TotalPages) - 1; |
||
214 | //} |
||
215 | |||
216 | //DownloadWorkAsync(PageNo + 1, takecount); |
||
217 | |||
218 | d7e20d2d | taeseongkim | |
219 | 2007ecaa | taeseongkim | //var takePageNoList = Enumerable.Range(PageNo + 1, _TakeCount); |
220 | 54a28343 | taeseongkim | } |
221 | 6a19b48d | taeseongkim | else |
222 | { |
||
223 | d7e20d2d | taeseongkim | |
224 | 6a19b48d | taeseongkim | } |
225 | d7e20d2d | taeseongkim | } |
226 | 54a28343 | taeseongkim | catch (Exception ex) |
227 | d7e20d2d | taeseongkim | { |
228 | 6a19b48d | taeseongkim | //if (cts != null) |
229 | //{ |
||
230 | // if (cts.Value.IsCancellationRequested) |
||
231 | // { |
||
232 | // return result; |
||
233 | // } |
||
234 | //} |
||
235 | 2007ecaa | taeseongkim | |
236 | 6a19b48d | taeseongkim | //if (!IsRestart) |
237 | //{ |
||
238 | // await Task.Delay(100); |
||
239 | 54a28343 | taeseongkim | |
240 | 6a19b48d | taeseongkim | // result = await GetPageUriAsync(cts, PageNo, true); |
241 | //} |
||
242 | a7372e37 | taeseongkim | //throw new Exception("GetPageAsync(string BasePageUri,int PageNo)", ex); |
243 | d7e20d2d | taeseongkim | } |
244 | finally |
||
245 | { |
||
246 | } |
||
247 | |||
248 | 54a28343 | taeseongkim | return result; |
249 | d7e20d2d | taeseongkim | } |
250 | |||
251 | 2007ecaa | taeseongkim | public async Task<PageItem> DownloadPageAsync(int PageNo, System.Threading.CancellationToken? cts) |
252 | d7e20d2d | taeseongkim | { |
253 | PageItem result = new PageItem { PageNo = PageNo }; |
||
254 | |||
255 | try |
||
256 | { |
||
257 | 2007ecaa | taeseongkim | var page = fileItems.Where(x => x.PageNo == PageNo).ToList(); |
258 | d7e20d2d | taeseongkim | |
259 | 2007ecaa | taeseongkim | if (page.Count > 0) |
260 | d7e20d2d | taeseongkim | { |
261 | System.Diagnostics.Debug.WriteLine("DownloadPageAsync fileItems"); |
||
262 | |||
263 | 2007ecaa | taeseongkim | result = page.First(); |
264 | d7e20d2d | taeseongkim | |
265 | /// 파일 체크 후 없으면 다시 다운로드 |
||
266 | 2007ecaa | taeseongkim | if (!System.IO.File.Exists(result.LocalFilePath)) |
267 | d7e20d2d | taeseongkim | { |
268 | 2007ecaa | taeseongkim | //fileItems.(result); |
269 | d7e20d2d | taeseongkim | |
270 | 2007ecaa | taeseongkim | result = await DownloadPageAsync(PageNo, cts); |
271 | d7e20d2d | taeseongkim | } |
272 | } |
||
273 | else |
||
274 | { |
||
275 | 6a19b48d | taeseongkim | |
276 | 2007ecaa | taeseongkim | |
277 | 77cdac33 | taeseongkim | if (!System.IO.Directory.Exists(LocalStorage)) |
278 | { |
||
279 | System.IO.Directory.CreateDirectory(LocalStorage); |
||
280 | 6a19b48d | taeseongkim | System.Threading.Thread.Sleep(50); |
281 | 77cdac33 | taeseongkim | } |
282 | |||
283 | d7e20d2d | taeseongkim | System.Diagnostics.Debug.WriteLine("DownloadPageAsync down"); |
284 | |||
285 | 43a743e4 | taeseongkim | string downloadFilePath = System.IO.Path.Combine(LocalStorage, System.IO.Path.GetRandomFileName()); /// PageNo.ToString() + "." + _fileExt); |
286 | d7e20d2d | taeseongkim | |
287 | 96bc6e8e | taeseongkim | Uri originalUri = new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString())); |
288 | |||
289 | 2007ecaa | taeseongkim | result = new PageItem |
290 | { |
||
291 | PageNo = PageNo, |
||
292 | OriginalUri = originalUri, |
||
293 | LocalUri = new Uri(downloadFilePath, UriKind.Absolute), |
||
294 | LocalFilePath = downloadFilePath |
||
295 | }; |
||
296 | |||
297 | 448c5399 | taeseongkim | using (System.Net.WebClient client = new System.Net.WebClient()) |
298 | 24c5e56c | taeseongkim | { |
299 | 6a19b48d | taeseongkim | System.Diagnostics.Debug.WriteLine("download start " + downloadFilePath); |
300 | 77cdac33 | taeseongkim | client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); |
301 | client.Headers.Add("Cache-Control", "no-cache"); |
||
302 | 448c5399 | taeseongkim | client.UseDefaultCredentials = true; |
303 | System.Net.IWebProxy webProxy = client.Proxy; |
||
304 | d7e20d2d | taeseongkim | |
305 | 448c5399 | taeseongkim | if (webProxy != null) |
306 | d97dbc7f | taeseongkim | { |
307 | 448c5399 | taeseongkim | // Use the default credentials of the logged on user. |
308 | webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; |
||
309 | d97dbc7f | taeseongkim | } |
310 | |||
311 | 448c5399 | taeseongkim | //client.DownloadFileCompleted += (snd, evt) => |
312 | //{ |
||
313 | 24c5e56c | taeseongkim | |
314 | d7e20d2d | taeseongkim | |
315 | 448c5399 | taeseongkim | // //(snd as System.Net.WebClient).Dispose(); |
316 | //}; |
||
317 | d7e20d2d | taeseongkim | |
318 | 2007ecaa | taeseongkim | if (cts != null) |
319 | { |
||
320 | 6a19b48d | taeseongkim | cts.Value.Register(() => |
321 | 24c5e56c | taeseongkim | { |
322 | 6a19b48d | taeseongkim | client.CancelAsync(); |
323 | Console.WriteLine("Request cancelled!"); |
||
324 | result = null; |
||
325 | }); |
||
326 | 2007ecaa | taeseongkim | |
327 | 6a19b48d | taeseongkim | client.DownloadProgressChanged += (snd, ect) => |
328 | { |
||
329 | IsBusy = client.IsBusy; |
||
330 | |||
331 | //if(cts.Value.IsCancellationRequested) |
||
332 | //{ |
||
333 | // System.Diagnostics.Debug.WriteLine("download cancel " + downloadFilePath); |
||
334 | // client.CancelAsync(); |
||
335 | // client.Dispose(); |
||
336 | //} |
||
337 | 24c5e56c | taeseongkim | }; |
338 | 2007ecaa | taeseongkim | } |
339 | |||
340 | client.DownloadFileCompleted += (snd, evt) => |
||
341 | { |
||
342 | 6a19b48d | taeseongkim | if (!evt.Cancelled) |
343 | { |
||
344 | result.IsDownLoad = true; |
||
345 | 24c5e56c | taeseongkim | |
346 | 6a19b48d | taeseongkim | if (fileItems.Where(x => x.PageNo == PageNo).Count() == 0) |
347 | { |
||
348 | fileItems.Add(result); |
||
349 | } |
||
350 | |||
351 | System.Diagnostics.Debug.WriteLine("Download completed finish : " + downloadFilePath); |
||
352 | |||
353 | PageLoadCompleted?.Invoke(this, new PageLoadCompletedEventArgs(result)); |
||
354 | |||
355 | client.Dispose(); |
||
356 | } |
||
357 | else |
||
358 | { |
||
359 | System.Diagnostics.Debug.WriteLine("Download completed cancelled : " + downloadFilePath); |
||
360 | |||
361 | client.Dispose(); |
||
362 | System.IO.File.Delete(downloadFilePath); |
||
363 | } |
||
364 | 2007ecaa | taeseongkim | }; |
365 | 72424099 | taeseongkim | |
366 | 448c5399 | taeseongkim | await client.DownloadFileTaskAsync(originalUri, downloadFilePath); |
367 | |||
368 | } |
||
369 | 2007ecaa | taeseongkim | |
370 | //} |
||
371 | 54a28343 | taeseongkim | //System.Diagnostics.Debug.WriteLine("Download : " + result.LocalFilePath); |
372 | d7e20d2d | taeseongkim | } |
373 | } |
||
374 | catch (Exception ex) |
||
375 | { |
||
376 | throw new Exception("DownloadPageAsync : ", ex); |
||
377 | } |
||
378 | finally |
||
379 | { |
||
380 | 6a19b48d | taeseongkim | IsBusy = false; |
381 | d7e20d2d | taeseongkim | } |
382 | |||
383 | return result; |
||
384 | } |
||
385 | |||
386 | public void Clear() |
||
387 | { |
||
388 | try |
||
389 | { |
||
390 | ResetImage(); |
||
391 | } |
||
392 | catch (Exception ex) |
||
393 | { |
||
394 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
395 | } |
||
396 | |||
397 | try |
||
398 | { |
||
399 | backgroundWorker.CancelAsync(); |
||
400 | backgroundWorker.Dispose(); |
||
401 | |||
402 | 03960fa5 | taeseongkim | /// downloadmanager에서 삭제함 |
403 | //foreach (var item in fileItems) |
||
404 | //{ |
||
405 | // System.IO.File.Delete(item.LocalFilePath); |
||
406 | //} |
||
407 | d7e20d2d | taeseongkim | |
408 | 03960fa5 | taeseongkim | //System.IO.Directory.Delete(LocalStorage, true); |
409 | d7e20d2d | taeseongkim | } |
410 | catch (Exception ex) |
||
411 | { |
||
412 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
413 | } |
||
414 | } |
||
415 | } |
||
416 | } |