markus / KCOM / PageManager / PageStorage.cs @ d7e20d2d
이력 | 보기 | 이력해설 | 다운로드 (6.86 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 | public async Task<BitmapFrame> GetPageAsync(int PageNo) |
||
78 | { |
||
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 | catch (Exception ex) |
||
101 | { |
||
102 | throw new Exception("GetPageAsync(string BasePageUri,int PageNo)", ex); |
||
103 | } |
||
104 | finally |
||
105 | { |
||
106 | } |
||
107 | |||
108 | return PageImage; |
||
109 | } |
||
110 | |||
111 | public void DownloadPagesAsync(int StartPageNo, int TalkCount = 5) |
||
112 | { |
||
113 | if (StartPageNo + TalkCount > _TotalPages) |
||
114 | { |
||
115 | TalkCount = _TotalPages - StartPageNo; |
||
116 | } |
||
117 | |||
118 | if (TalkCount > 0) |
||
119 | { |
||
120 | for (int i = StartPageNo; i < StartPageNo + TalkCount; i++) |
||
121 | { |
||
122 | try |
||
123 | { |
||
124 | DownloadPageAsync(i).RunAndForget(); |
||
125 | } |
||
126 | catch (Exception ex) |
||
127 | { |
||
128 | System.Diagnostics.Debug.WriteLine("DownloadPagesAsync err", ex); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public async Task<PageItem> DownloadPageAsync(int PageNo) |
||
135 | { |
||
136 | PageItem result = new PageItem { PageNo = PageNo }; |
||
137 | |||
138 | try |
||
139 | { |
||
140 | var page = fileItems.Where(x => x.PageNo == PageNo); |
||
141 | |||
142 | if (page.Count() > 0) |
||
143 | { |
||
144 | System.Diagnostics.Debug.WriteLine("DownloadPageAsync fileItems"); |
||
145 | |||
146 | PageItem item = page.First(); |
||
147 | |||
148 | /// 파일 체크 후 없으면 다시 다운로드 |
||
149 | if (System.IO.File.Exists(item.LocalFilePath)) |
||
150 | { |
||
151 | result = page.First(); |
||
152 | } |
||
153 | else |
||
154 | { |
||
155 | fileItems.Remove(item); |
||
156 | |||
157 | result = await DownloadPageAsync(PageNo); |
||
158 | } |
||
159 | } |
||
160 | else |
||
161 | { |
||
162 | |||
163 | System.Diagnostics.Debug.WriteLine("DownloadPageAsync down"); |
||
164 | |||
165 | string downloadFilePath = System.IO.Path.Combine(_localStorage, PageNo.ToString() + ".png"); |
||
166 | |||
167 | result = new PageItem |
||
168 | { |
||
169 | PageNo = PageNo, |
||
170 | OriginalUri = new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString())), |
||
171 | LocalUri = new Uri(downloadFilePath, UriKind.Absolute), |
||
172 | LocalFilePath = downloadFilePath |
||
173 | }; |
||
174 | |||
175 | using (System.Net.WebClient client = new System.Net.WebClient()) |
||
176 | { |
||
177 | client.UseDefaultCredentials = true; |
||
178 | System.Net.IWebProxy webProxy = client.Proxy; |
||
179 | |||
180 | if (webProxy != null) |
||
181 | { |
||
182 | // Use the default credentials of the logged on user. |
||
183 | webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; |
||
184 | } |
||
185 | |||
186 | client.DownloadFileCompleted += (snd, evt) => |
||
187 | { |
||
188 | fileItems.Add(result); |
||
189 | }; |
||
190 | |||
191 | await client.DownloadFileTaskAsync(result.OriginalUri, downloadFilePath); |
||
192 | |||
193 | System.Diagnostics.Debug.WriteLine("Download : " + result.LocalFilePath); |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | catch (Exception ex) |
||
198 | { |
||
199 | throw new Exception("DownloadPageAsync : ", ex); |
||
200 | } |
||
201 | finally |
||
202 | { |
||
203 | } |
||
204 | |||
205 | return result; |
||
206 | } |
||
207 | |||
208 | public void Clear() |
||
209 | { |
||
210 | try |
||
211 | { |
||
212 | ResetImage(); |
||
213 | } |
||
214 | catch (Exception ex) |
||
215 | { |
||
216 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
217 | } |
||
218 | |||
219 | try |
||
220 | { |
||
221 | backgroundWorker.CancelAsync(); |
||
222 | backgroundWorker.Dispose(); |
||
223 | |||
224 | fileItems.ForEach(x => |
||
225 | { |
||
226 | System.IO.File.Delete(x.LocalFilePath); |
||
227 | }); |
||
228 | |||
229 | System.IO.Directory.Delete(_localStorage, true); |
||
230 | } |
||
231 | catch (Exception ex) |
||
232 | { |
||
233 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | } |