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