markus / KCOM / PageManager / PageStorage.cs @ 96bc6e8e
이력 | 보기 | 이력해설 | 다운로드 (7.52 KB)
1 | d7e20d2d | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.Linq; |
||
5 | using System.Text; |
||
6 | using System.Threading.Tasks; |
||
7 | using System.Windows.Media.Imaging; |
||
8 | |||
9 | namespace KCOM.PageManager |
||
10 | { |
||
11 | public class PageStorage |
||
12 | { |
||
13 | private const int DEFUALT_TALK_PAGE_COUNT = 10; |
||
14 | |||
15 | BackgroundWorker backgroundWorker; |
||
16 | List<PageItem> fileItems = new List<PageItem>(); |
||
17 | string _localStorage; |
||
18 | string _BaseUri; |
||
19 | int _TotalPages; |
||
20 | |||
21 | |||
22 | BitmapFrame PageImage; |
||
23 | |||
24 | public PageStorage(string BaseUri, string localStoragePath,int totalPages) |
||
25 | { |
||
26 | try |
||
27 | { |
||
28 | backgroundWorker = new BackgroundWorker {WorkerSupportsCancellation = true }; |
||
29 | backgroundWorker.DoWork += BackgroundWorker_DoWork; |
||
30 | |||
31 | _BaseUri = BaseUri; |
||
32 | _localStorage = localStoragePath; |
||
33 | _TotalPages = totalPages; |
||
34 | |||
35 | System.IO.Directory.CreateDirectory(_localStorage); |
||
36 | |||
37 | //backgroundWorker.RunWorkerAsync(new int[] { 1, 10 }); |
||
38 | } |
||
39 | catch (Exception ex) |
||
40 | { |
||
41 | throw new Exception("PageStorage", ex); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) |
||
46 | { |
||
47 | int StartPageNo = -1; |
||
48 | int TalkCount = -1; |
||
49 | |||
50 | if (e.Argument != null) |
||
51 | { |
||
52 | |||
53 | var values = (e.Argument as int[]); |
||
54 | |||
55 | if (values.Count() == 2) |
||
56 | { |
||
57 | |||
58 | StartPageNo = values[0]; |
||
59 | TalkCount = values[1]; |
||
60 | |||
61 | DownloadPagesAsync(StartPageNo, TalkCount); |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | |||
66 | public void ResetImage() |
||
67 | { |
||
68 | //if (PageImage != null) |
||
69 | //{ |
||
70 | // PageImage = null; |
||
71 | //} |
||
72 | |||
73 | //PageImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache; |
||
74 | |||
75 | } |
||
76 | |||
77 | 96bc6e8e | taeseongkim | public async Task<BitmapFrame> GetPageAsync(int PageNo,bool IsRestart =false) |
78 | d7e20d2d | taeseongkim | { |
79 | try |
||
80 | { |
||
81 | System.Diagnostics.Debug.WriteLine("GetPageAsync"); |
||
82 | |||
83 | while (backgroundWorker.IsBusy) |
||
84 | { |
||
85 | await Task.Delay(100); |
||
86 | |||
87 | if (backgroundWorker.IsBusy) |
||
88 | { |
||
89 | backgroundWorker.CancelAsync(); |
||
90 | } |
||
91 | |||
92 | } |
||
93 | |||
94 | var pageItem = await DownloadPageAsync(PageNo); |
||
95 | |||
96 | backgroundWorker.RunWorkerAsync(new int[] { PageNo + 1, 5 }); |
||
97 | |||
98 | PageImage = BitmapFrame.Create(pageItem.LocalUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); |
||
99 | } |
||
100 | a7372e37 | taeseongkim | catch (Exception) |
101 | d7e20d2d | taeseongkim | { |
102 | 96bc6e8e | taeseongkim | if (!IsRestart) |
103 | { |
||
104 | await Task.Delay(100); |
||
105 | PageImage = await GetPageAsync(PageNo,true); |
||
106 | } |
||
107 | a7372e37 | taeseongkim | //throw new Exception("GetPageAsync(string BasePageUri,int PageNo)", ex); |
108 | d7e20d2d | taeseongkim | } |
109 | finally |
||
110 | { |
||
111 | } |
||
112 | |||
113 | return PageImage; |
||
114 | } |
||
115 | |||
116 | public void DownloadPagesAsync(int StartPageNo, int TalkCount = 5) |
||
117 | { |
||
118 | e8557bd7 | taeseongkim | try |
119 | d7e20d2d | taeseongkim | { |
120 | e8557bd7 | taeseongkim | if (StartPageNo + TalkCount > _TotalPages) |
121 | { |
||
122 | TalkCount = _TotalPages - StartPageNo; |
||
123 | } |
||
124 | d7e20d2d | taeseongkim | |
125 | e8557bd7 | taeseongkim | if (TalkCount > 0) |
126 | d7e20d2d | taeseongkim | { |
127 | e8557bd7 | taeseongkim | for (int i = StartPageNo; i < StartPageNo + TalkCount; i++) |
128 | d7e20d2d | taeseongkim | { |
129 | e8557bd7 | taeseongkim | try |
130 | { |
||
131 | DownloadPageAsync(i).RunAndForget(); |
||
132 | } |
||
133 | catch (Exception ex) |
||
134 | { |
||
135 | System.Diagnostics.Debug.WriteLine("DownloadPagesAsync err", ex); |
||
136 | } |
||
137 | 72424099 | taeseongkim | finally |
138 | { |
||
139 | } |
||
140 | |||
141 | |||
142 | d7e20d2d | taeseongkim | } |
143 | } |
||
144 | } |
||
145 | e8557bd7 | taeseongkim | catch (Exception ex) |
146 | { |
||
147 | throw new Exception("DownloadPagesAsync : ", ex); |
||
148 | } |
||
149 | finally |
||
150 | { |
||
151 | 72424099 | taeseongkim | |
152 | e8557bd7 | taeseongkim | } |
153 | d7e20d2d | taeseongkim | } |
154 | |||
155 | public async Task<PageItem> DownloadPageAsync(int PageNo) |
||
156 | { |
||
157 | PageItem result = new PageItem { PageNo = PageNo }; |
||
158 | |||
159 | try |
||
160 | { |
||
161 | var page = fileItems.Where(x => x.PageNo == PageNo); |
||
162 | |||
163 | if (page.Count() > 0) |
||
164 | { |
||
165 | System.Diagnostics.Debug.WriteLine("DownloadPageAsync fileItems"); |
||
166 | |||
167 | PageItem item = page.First(); |
||
168 | |||
169 | /// 파일 체크 후 없으면 다시 다운로드 |
||
170 | if (System.IO.File.Exists(item.LocalFilePath)) |
||
171 | { |
||
172 | result = page.First(); |
||
173 | } |
||
174 | else |
||
175 | { |
||
176 | fileItems.Remove(item); |
||
177 | |||
178 | result = await DownloadPageAsync(PageNo); |
||
179 | } |
||
180 | } |
||
181 | else |
||
182 | { |
||
183 | |||
184 | System.Diagnostics.Debug.WriteLine("DownloadPageAsync down"); |
||
185 | |||
186 | string downloadFilePath = System.IO.Path.Combine(_localStorage, PageNo.ToString() + ".png"); |
||
187 | |||
188 | 96bc6e8e | taeseongkim | Uri originalUri = new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString())); |
189 | |||
190 | d7e20d2d | taeseongkim | |
191 | e8557bd7 | taeseongkim | System.Net.WebClient client = new System.Net.WebClient(); |
192 | d7e20d2d | taeseongkim | |
193 | e8557bd7 | taeseongkim | client.UseDefaultCredentials = true; |
194 | System.Net.IWebProxy webProxy = client.Proxy; |
||
195 | d7e20d2d | taeseongkim | |
196 | e8557bd7 | taeseongkim | if (webProxy != null) |
197 | { |
||
198 | // Use the default credentials of the logged on user. |
||
199 | webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; |
||
200 | } |
||
201 | |||
202 | client.DownloadFileCompleted += (snd, evt) => |
||
203 | { |
||
204 | 96bc6e8e | taeseongkim | result = new PageItem |
205 | { |
||
206 | PageNo = PageNo, |
||
207 | OriginalUri = originalUri, |
||
208 | LocalUri = new Uri(downloadFilePath, UriKind.Absolute), |
||
209 | LocalFilePath = downloadFilePath |
||
210 | }; |
||
211 | |||
212 | e8557bd7 | taeseongkim | fileItems.Add(result); |
213 | }; |
||
214 | d7e20d2d | taeseongkim | |
215 | 96bc6e8e | taeseongkim | await client.DownloadFileTaskAsync(originalUri, downloadFilePath); |
216 | d7e20d2d | taeseongkim | |
217 | 72424099 | taeseongkim | GC.Collect(); |
218 | GC.WaitForPendingFinalizers(); |
||
219 | GC.Collect(); |
||
220 | |||
221 | e8557bd7 | taeseongkim | System.Diagnostics.Debug.WriteLine("Download : " + result.LocalFilePath); |
222 | d7e20d2d | taeseongkim | } |
223 | } |
||
224 | catch (Exception ex) |
||
225 | { |
||
226 | throw new Exception("DownloadPageAsync : ", ex); |
||
227 | } |
||
228 | finally |
||
229 | { |
||
230 | } |
||
231 | |||
232 | return result; |
||
233 | } |
||
234 | |||
235 | public void Clear() |
||
236 | { |
||
237 | try |
||
238 | { |
||
239 | ResetImage(); |
||
240 | } |
||
241 | catch (Exception ex) |
||
242 | { |
||
243 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
244 | } |
||
245 | |||
246 | try |
||
247 | { |
||
248 | backgroundWorker.CancelAsync(); |
||
249 | backgroundWorker.Dispose(); |
||
250 | |||
251 | fileItems.ForEach(x => |
||
252 | { |
||
253 | System.IO.File.Delete(x.LocalFilePath); |
||
254 | }); |
||
255 | |||
256 | System.IO.Directory.Delete(_localStorage, true); |
||
257 | } |
||
258 | catch (Exception ex) |
||
259 | { |
||
260 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | } |