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