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