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