markus / KCOM / PageManager / PageStorage.cs @ 03960fa5
이력 | 보기 | 이력해설 | 다운로드 (11.3 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Concurrent; |
3 |
using System.Collections.Generic; |
4 |
using System.ComponentModel; |
5 |
using System.Linq; |
6 |
using System.Text; |
7 |
using System.Threading.Tasks; |
8 |
using System.Windows.Media.Imaging; |
9 |
|
10 |
namespace KCOM.PageManager |
11 |
{ |
12 |
public class PageStorage |
13 |
{ |
14 |
private const int DEFUALT_TALK_PAGE_COUNT = 10; |
15 |
|
16 |
public event EventHandler<PageLoadCompletedEventArgs> PageLoadCompleted; |
17 |
|
18 |
List<int> WorkItems = new List<int>(); |
19 |
ConcurrentBag<PageItem> fileItems = new ConcurrentBag<PageItem>(); |
20 |
BackgroundWorker backgroundWorker; |
21 |
|
22 |
//List<PageItem> fileItems = new List<PageItem>(); |
23 |
public string LocalStorage; |
24 |
string _fileExt; |
25 |
string _BaseUri; |
26 |
int _TotalPages; |
27 |
int _TakeCount; |
28 |
|
29 |
bool IsDownload = false; |
30 |
|
31 |
BitmapFrame PageImage; |
32 |
|
33 |
public PageStorage(string BaseUri, string localStoragePath, string fileExt, int totalPages, int takeCount = 5) |
34 |
{ |
35 |
try |
36 |
{ |
37 |
backgroundWorker = new BackgroundWorker { WorkerSupportsCancellation = true, WorkerReportsProgress = true }; |
38 |
backgroundWorker.DoWork += BackgroundWorker_DoWork; |
39 |
backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged; |
40 |
_fileExt = fileExt; |
41 |
_BaseUri = BaseUri; |
42 |
LocalStorage = localStoragePath; |
43 |
_TotalPages = totalPages; |
44 |
_TakeCount = takeCount; |
45 |
|
46 |
System.IO.Directory.CreateDirectory(LocalStorage); |
47 |
|
48 |
//backgroundWorker.RunWorkerAsync(new int[] { 1, 10 }); |
49 |
} |
50 |
catch (Exception ex) |
51 |
{ |
52 |
throw new Exception("PageStorage", ex); |
53 |
} |
54 |
} |
55 |
|
56 |
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) |
57 |
{ |
58 |
System.Diagnostics.Debug.WriteLine(_fileExt + ":" + e.ProgressPercentage.ToString() + "%"); |
59 |
} |
60 |
|
61 |
private async void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) |
62 |
{ |
63 |
|
64 |
while (WorkItems.Count > 0) |
65 |
{ |
66 |
if (!IsDownload) |
67 |
{ |
68 |
int item = -1; |
69 |
try |
70 |
{ |
71 |
|
72 |
if (WorkItems.TryFrist(true, out item)) |
73 |
{ |
74 |
var result = await DownloadPageAsync(item, null); |
75 |
|
76 |
if (!result.IsDownLoad) |
77 |
{ |
78 |
System.Threading.Thread.Sleep(100); |
79 |
} |
80 |
await Task.Delay(100); |
81 |
//System.Threading.Thread.Sleep(10); |
82 |
//backgroundWorker.ReportProgress(fileItems.Count / _TotalPages * 100); |
83 |
} |
84 |
} |
85 |
catch (Exception ex) |
86 |
{ |
87 |
System.Diagnostics.Debug.WriteLine(ex.ToString()); |
88 |
} |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
public void ResetImage() |
94 |
{ |
95 |
//if (PageImage != null) |
96 |
//{ |
97 |
// PageImage = null; |
98 |
//} |
99 |
|
100 |
//PageImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache; |
101 |
|
102 |
} |
103 |
|
104 |
public async Task<BitmapFrame> GetPageImageAsync(System.Threading.CancellationToken cts, int PageNo, bool IsRestart = false) |
105 |
{ |
106 |
try |
107 |
{ |
108 |
|
109 |
var localUri = await GetPageUriAsync(null, PageNo); |
110 |
|
111 |
if (localUri != null || !cts.IsCancellationRequested) |
112 |
{ |
113 |
PageImage = BitmapFrame.Create(localUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); |
114 |
} |
115 |
} |
116 |
catch (Exception ex) |
117 |
{ |
118 |
PageImage = BitmapFrame.Create(new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString())), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); |
119 |
} |
120 |
finally |
121 |
{ |
122 |
} |
123 |
|
124 |
return PageImage; |
125 |
} |
126 |
|
127 |
private void DownloadWorkAsync(int startPage, int TakeCount) |
128 |
{ |
129 |
|
130 |
lock (WorkItems) |
131 |
{ |
132 |
WorkItems.AddRange(Enumerable.Range(startPage, TakeCount)); |
133 |
} |
134 |
|
135 |
if (TakeCount == _TotalPages) |
136 |
{ |
137 |
_TakeCount = 0; |
138 |
} |
139 |
|
140 |
// var files = fileItems.Select(x => x.PageNo); |
141 |
|
142 |
|
143 |
//for (int i = startPage; i < TakeCount + 1; i++) |
144 |
//{ |
145 |
// if (i < _TotalPages && !WorkItems.Contains(i) && !files.Contains(i)) |
146 |
// { |
147 |
// WorkItems.Add(i); |
148 |
// // System.Threading.Thread.Sleep(1); |
149 |
// } |
150 |
// else if(i == _TotalPages) |
151 |
// { |
152 |
// _TakeCount = 0; |
153 |
// } |
154 |
//} |
155 |
|
156 |
if (!backgroundWorker.IsBusy && WorkItems.Count() > 0) |
157 |
{ |
158 |
backgroundWorker.RunWorkerAsync(); |
159 |
} |
160 |
|
161 |
//await Task.Delay(10);// System.Threading.Thread.Sleep(10); |
162 |
//while (WorkItems.Count > 0) |
163 |
//{ |
164 |
// int item = -1; |
165 |
|
166 |
// if (WorkItems.TryDequeue(out item)) |
167 |
// { |
168 |
// var result = await DownloadPageAsync(item, null); |
169 |
// } |
170 |
|
171 |
//} |
172 |
|
173 |
} |
174 |
|
175 |
public async Task<Uri> GetPageUriAsync(System.Threading.CancellationToken? cts, int PageNo, bool IsRestart = false) |
176 |
{ |
177 |
Uri result = null; |
178 |
|
179 |
try |
180 |
{ |
181 |
System.Diagnostics.Debug.WriteLine("GetPageAsync"); |
182 |
|
183 |
var pageItem = await DownloadPageAsync(PageNo, cts); |
184 |
|
185 |
if (pageItem != null && 0 < _TakeCount && PageNo + 1 < _TotalPages) |
186 |
{ |
187 |
int takecount = _TakeCount; |
188 |
|
189 |
if (_TotalPages < PageNo + 1 + takecount) |
190 |
{ |
191 |
takecount = takecount - (PageNo + 1 + takecount - _TotalPages) - 1; |
192 |
} |
193 |
|
194 |
DownloadWorkAsync(PageNo + 1, takecount); |
195 |
//var takePageNoList = Enumerable.Range(PageNo + 1, _TakeCount); |
196 |
|
197 |
|
198 |
|
199 |
} |
200 |
|
201 |
result = pageItem.LocalUri; |
202 |
} |
203 |
catch (Exception ex) |
204 |
{ |
205 |
if (cts != null) |
206 |
{ |
207 |
if (cts.Value.IsCancellationRequested) |
208 |
{ |
209 |
return result; |
210 |
} |
211 |
} |
212 |
|
213 |
if (!IsRestart) |
214 |
{ |
215 |
await Task.Delay(100); |
216 |
|
217 |
result = await GetPageUriAsync(cts, PageNo, true); |
218 |
} |
219 |
//throw new Exception("GetPageAsync(string BasePageUri,int PageNo)", ex); |
220 |
} |
221 |
finally |
222 |
{ |
223 |
} |
224 |
|
225 |
return result; |
226 |
} |
227 |
|
228 |
public async Task<PageItem> DownloadPageAsync(int PageNo, System.Threading.CancellationToken? cts) |
229 |
{ |
230 |
PageItem result = new PageItem { PageNo = PageNo }; |
231 |
|
232 |
try |
233 |
{ |
234 |
var page = fileItems.Where(x => x.PageNo == PageNo).ToList(); |
235 |
|
236 |
if (page.Count > 0) |
237 |
{ |
238 |
System.Diagnostics.Debug.WriteLine("DownloadPageAsync fileItems"); |
239 |
|
240 |
result = page.First(); |
241 |
|
242 |
/// 파일 체크 후 없으면 다시 다운로드 |
243 |
if (!System.IO.File.Exists(result.LocalFilePath)) |
244 |
{ |
245 |
//fileItems.(result); |
246 |
|
247 |
result = await DownloadPageAsync(PageNo, cts); |
248 |
} |
249 |
} |
250 |
else |
251 |
{ |
252 |
|
253 |
System.Diagnostics.Debug.WriteLine("DownloadPageAsync down"); |
254 |
|
255 |
string downloadFilePath = System.IO.Path.Combine(LocalStorage, PageNo.ToString() + "." + _fileExt); |
256 |
|
257 |
Uri originalUri = new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString())); |
258 |
|
259 |
result = new PageItem |
260 |
{ |
261 |
PageNo = PageNo, |
262 |
OriginalUri = originalUri, |
263 |
LocalUri = new Uri(downloadFilePath, UriKind.Absolute), |
264 |
LocalFilePath = downloadFilePath |
265 |
}; |
266 |
|
267 |
if (fileItems.Where(x => x.PageNo == PageNo).Count() == 0) |
268 |
{ |
269 |
fileItems.Add(result); |
270 |
} |
271 |
|
272 |
using (System.Net.WebClient client = new System.Net.WebClient()) |
273 |
{ |
274 |
client.UseDefaultCredentials = true; |
275 |
System.Net.IWebProxy webProxy = client.Proxy; |
276 |
|
277 |
if (webProxy != null) |
278 |
{ |
279 |
// Use the default credentials of the logged on user. |
280 |
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; |
281 |
} |
282 |
|
283 |
//client.DownloadFileCompleted += (snd, evt) => |
284 |
//{ |
285 |
|
286 |
|
287 |
// //(snd as System.Net.WebClient).Dispose(); |
288 |
//}; |
289 |
|
290 |
if (cts != null) |
291 |
{ |
292 |
client.DownloadProgressChanged += (snd, ect) => |
293 |
{ |
294 |
IsDownload = client.IsBusy; |
295 |
|
296 |
if (cts.Value.IsCancellationRequested) |
297 |
{ |
298 |
client.CancelAsync(); |
299 |
} |
300 |
}; |
301 |
} |
302 |
|
303 |
client.DownloadFileCompleted += (snd, evt) => |
304 |
{ |
305 |
result.IsDownLoad = true; |
306 |
System.Diagnostics.Debug.WriteLine("Download : " + downloadFilePath); |
307 |
|
308 |
PageLoadCompleted?.Invoke(this, new PageLoadCompletedEventArgs(result)); |
309 |
}; |
310 |
|
311 |
await client.DownloadFileTaskAsync(originalUri, downloadFilePath); |
312 |
|
313 |
} |
314 |
|
315 |
//} |
316 |
//System.Diagnostics.Debug.WriteLine("Download : " + result.LocalFilePath); |
317 |
} |
318 |
} |
319 |
catch (Exception ex) |
320 |
{ |
321 |
throw new Exception("DownloadPageAsync : ", ex); |
322 |
} |
323 |
finally |
324 |
{ |
325 |
IsDownload = false; |
326 |
} |
327 |
|
328 |
return result; |
329 |
} |
330 |
|
331 |
public void Clear() |
332 |
{ |
333 |
try |
334 |
{ |
335 |
ResetImage(); |
336 |
} |
337 |
catch (Exception ex) |
338 |
{ |
339 |
System.Diagnostics.Debug.WriteLine(ex.ToString()); |
340 |
} |
341 |
|
342 |
try |
343 |
{ |
344 |
backgroundWorker.CancelAsync(); |
345 |
backgroundWorker.Dispose(); |
346 |
|
347 |
/// downloadmanager에서 삭제함 |
348 |
//foreach (var item in fileItems) |
349 |
//{ |
350 |
// System.IO.File.Delete(item.LocalFilePath); |
351 |
//} |
352 |
|
353 |
//System.IO.Directory.Delete(LocalStorage, true); |
354 |
} |
355 |
catch (Exception ex) |
356 |
{ |
357 |
System.Diagnostics.Debug.WriteLine(ex.ToString()); |
358 |
} |
359 |
} |
360 |
} |
361 |
} |