개정판 2007ecaa
thumbnail을 별도의 프로세스에서 download 하도록 수정
Change-Id: Ica8432a3fbe5e44d92412ec91290097deddeee71
KCOM/PageManager/PageStorage.cs | ||
---|---|---|
1 | 1 |
using System; |
2 |
using System.Collections.Concurrent; |
|
2 | 3 |
using System.Collections.Generic; |
3 | 4 |
using System.ComponentModel; |
4 | 5 |
using System.Linq; |
... | ... | |
11 | 12 |
public class PageStorage |
12 | 13 |
{ |
13 | 14 |
private const int DEFUALT_TALK_PAGE_COUNT = 10; |
14 |
bool IsRunWork = false; |
|
15 |
|
|
16 |
public event EventHandler<PageLoadCompletedEventArgs> PageLoadCompleted; |
|
17 |
|
|
18 |
List<int> WorkItems = new List<int>(); |
|
19 |
ConcurrentBag<PageItem> fileItems = new ConcurrentBag<PageItem>(); |
|
15 | 20 |
BackgroundWorker backgroundWorker; |
16 |
List<PageItem> fileItems = new List<PageItem>(); |
|
21 |
|
|
22 |
//List<PageItem> fileItems = new List<PageItem>(); |
|
17 | 23 |
string _localStorage; |
18 | 24 |
string _fileExt; |
19 | 25 |
string _BaseUri; |
20 | 26 |
int _TotalPages; |
21 | 27 |
int _TakeCount; |
22 | 28 |
|
29 |
bool IsDownload = false; |
|
23 | 30 |
|
24 | 31 |
BitmapFrame PageImage; |
25 | 32 |
|
26 |
public PageStorage(string BaseUri, string localStoragePath,string fileExt,int totalPages,int takeCount = 5)
|
|
33 |
public PageStorage(string BaseUri, string localStoragePath, string fileExt, int totalPages, int takeCount = 5)
|
|
27 | 34 |
{ |
28 | 35 |
try |
29 | 36 |
{ |
30 |
backgroundWorker = new BackgroundWorker {WorkerSupportsCancellation = true };
|
|
37 |
backgroundWorker = new BackgroundWorker { WorkerSupportsCancellation = true, WorkerReportsProgress = true };
|
|
31 | 38 |
backgroundWorker.DoWork += BackgroundWorker_DoWork; |
32 |
|
|
39 |
backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged; |
|
33 | 40 |
_fileExt = fileExt; |
34 | 41 |
_BaseUri = BaseUri; |
35 | 42 |
_localStorage = localStoragePath; |
... | ... | |
46 | 53 |
} |
47 | 54 |
} |
48 | 55 |
|
49 |
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
|
|
56 |
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
|
|
50 | 57 |
{ |
51 |
int StartPageNo = -1; |
|
52 |
int TalkCount = -1; |
|
53 |
|
|
54 |
if (e.Argument != null) |
|
55 |
{ |
|
58 |
System.Diagnostics.Debug.WriteLine(_fileExt + ":" + e.ProgressPercentage.ToString() + "%"); |
|
59 |
} |
|
56 | 60 |
|
57 |
var values = (e.Argument as int[]); |
|
61 |
private async void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) |
|
62 |
{ |
|
58 | 63 |
|
59 |
if (values.Count() == 2) |
|
64 |
while (WorkItems.Count > 0) |
|
65 |
{ |
|
66 |
if (!IsDownload) |
|
60 | 67 |
{ |
68 |
int item = -1; |
|
69 |
try |
|
70 |
{ |
|
61 | 71 |
|
62 |
StartPageNo = values[0]; |
|
63 |
TalkCount = values[1]; |
|
72 |
if (WorkItems.TryFrist(true, out item)) |
|
73 |
{ |
|
74 |
var result = await DownloadPageAsync(item, null); |
|
64 | 75 |
|
65 |
DownloadPagesAsync( StartPageNo, TalkCount); |
|
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 |
} |
|
66 | 89 |
} |
67 | 90 |
} |
68 | 91 |
} |
... | ... | |
78 | 101 |
|
79 | 102 |
} |
80 | 103 |
|
81 |
public async Task<BitmapFrame> GetPageImageAsync(System.Threading.CancellationToken cts,int PageNo,bool IsRestart =false)
|
|
104 |
public async Task<BitmapFrame> GetPageImageAsync(System.Threading.CancellationToken cts, int PageNo, bool IsRestart = false)
|
|
82 | 105 |
{ |
83 | 106 |
try |
84 | 107 |
{ |
85 | 108 |
|
86 |
var localUri = await GetPageUriAsync(cts,PageNo);
|
|
109 |
var localUri = await GetPageUriAsync(null, PageNo);
|
|
87 | 110 |
|
88 | 111 |
if (localUri != null || !cts.IsCancellationRequested) |
89 | 112 |
{ |
... | ... | |
101 | 124 |
return PageImage; |
102 | 125 |
} |
103 | 126 |
|
104 |
public async Task<Uri> GetPageUriAsync( System.Threading.CancellationToken cts, int PageNo, bool IsRestart = false) |
|
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) |
|
105 | 176 |
{ |
106 | 177 |
Uri result = null; |
107 | 178 |
|
... | ... | |
109 | 180 |
{ |
110 | 181 |
System.Diagnostics.Debug.WriteLine("GetPageAsync"); |
111 | 182 |
|
112 |
var pageItem = await DownloadPageAsync(PageNo,cts); |
|
113 |
|
|
114 |
if (!IsRunWork && pageItem != null)
|
|
183 |
var pageItem = await DownloadPageAsync(PageNo, cts);
|
|
184 |
|
|
185 |
if (pageItem != null && 0 < _TakeCount && PageNo + 1 < _TotalPages)
|
|
115 | 186 |
{ |
116 |
var takePageNoList = Enumerable.Range(PageNo + 1, _TakeCount);
|
|
187 |
int takecount = _TakeCount;
|
|
117 | 188 |
|
118 |
if (takePageNoList.Any(x => fileItems.Count(y => y.PageNo == x) == 0))
|
|
189 |
if (_TotalPages < PageNo + 1 + takecount)
|
|
119 | 190 |
{ |
120 |
while (backgroundWorker.IsBusy) |
|
121 |
{ |
|
122 |
if (backgroundWorker.IsBusy) |
|
123 |
{ |
|
124 |
backgroundWorker.CancelAsync(); |
|
125 |
} |
|
191 |
takecount = takecount - (PageNo + 1 + takecount - _TotalPages) - 1; |
|
192 |
} |
|
126 | 193 |
|
127 |
await Task.Delay(100);
|
|
128 |
}
|
|
194 |
DownloadWorkAsync(PageNo + 1, takecount);
|
|
195 |
//var takePageNoList = Enumerable.Range(PageNo + 1, _TakeCount);
|
|
129 | 196 |
|
130 |
backgroundWorker.RunWorkerAsync(new int[] { PageNo + 1, _TakeCount }); |
|
131 | 197 |
|
132 |
if (_TotalPages == _TakeCount) |
|
133 |
{ |
|
134 |
IsRunWork = true; |
|
135 |
} |
|
136 |
} |
|
137 | 198 |
} |
138 | 199 |
|
139 | 200 |
result = pageItem.LocalUri; |
140 | 201 |
} |
141 | 202 |
catch (Exception ex) |
142 | 203 |
{ |
143 |
if (!IsRestart || !cts.IsCancellationRequested) |
|
204 |
if (cts != null) |
|
205 |
{ |
|
206 |
if (cts.Value.IsCancellationRequested) |
|
207 |
{ |
|
208 |
return result; |
|
209 |
} |
|
210 |
} |
|
211 |
|
|
212 |
if (!IsRestart) |
|
144 | 213 |
{ |
145 | 214 |
await Task.Delay(100); |
146 | 215 |
|
147 |
result = await GetPageUriAsync(cts,PageNo, true); |
|
216 |
result = await GetPageUriAsync(cts, PageNo, true);
|
|
148 | 217 |
} |
149 | 218 |
//throw new Exception("GetPageAsync(string BasePageUri,int PageNo)", ex); |
150 | 219 |
} |
... | ... | |
155 | 224 |
return result; |
156 | 225 |
} |
157 | 226 |
|
158 |
public async void DownloadPagesAsync(int StartPageNo, int TalkCount = 5) |
|
159 |
{ |
|
160 |
try |
|
161 |
{ |
|
162 |
if (StartPageNo + TalkCount > _TotalPages) |
|
163 |
{ |
|
164 |
TalkCount = _TotalPages - StartPageNo; |
|
165 |
} |
|
166 |
|
|
167 |
if (TalkCount > 0) |
|
168 |
{ |
|
169 |
for (int i = StartPageNo; i < StartPageNo + TalkCount; i++) |
|
170 |
{ |
|
171 |
try |
|
172 |
{ |
|
173 |
await DownloadPageAsync(i,null); |
|
174 |
System.Threading.Thread.Sleep(200); |
|
175 |
} |
|
176 |
catch (Exception ex) |
|
177 |
{ |
|
178 |
System.Diagnostics.Debug.WriteLine("DownloadPagesAsync err", ex); |
|
179 |
} |
|
180 |
finally |
|
181 |
{ |
|
182 |
} |
|
183 |
|
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
catch (Exception ex) |
|
188 |
{ |
|
189 |
throw new Exception("DownloadPagesAsync : ", ex); |
|
190 |
} |
|
191 |
finally |
|
192 |
{ |
|
193 |
GC.Collect(2); |
|
194 |
GC.Collect(2); |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
public async Task<PageItem> DownloadPageAsync(int PageNo,System.Threading.CancellationToken? cts) |
|
227 |
public async Task<PageItem> DownloadPageAsync(int PageNo, System.Threading.CancellationToken? cts) |
|
199 | 228 |
{ |
200 | 229 |
PageItem result = new PageItem { PageNo = PageNo }; |
201 | 230 |
|
202 | 231 |
try |
203 | 232 |
{ |
204 |
var page = fileItems.Where(x => x.PageNo == PageNo); |
|
233 |
var page = fileItems.Where(x => x.PageNo == PageNo).ToList();
|
|
205 | 234 |
|
206 |
if (page.Count() > 0)
|
|
235 |
if (page.Count > 0) |
|
207 | 236 |
{ |
208 | 237 |
System.Diagnostics.Debug.WriteLine("DownloadPageAsync fileItems"); |
209 | 238 |
|
210 |
PageItem item = page.First();
|
|
239 |
result = page.First();
|
|
211 | 240 |
|
212 | 241 |
/// 파일 체크 후 없으면 다시 다운로드 |
213 |
if (System.IO.File.Exists(item.LocalFilePath))
|
|
242 |
if (!System.IO.File.Exists(result.LocalFilePath))
|
|
214 | 243 |
{ |
215 |
result = page.First(); |
|
216 |
} |
|
217 |
else |
|
218 |
{ |
|
219 |
fileItems.Remove(item); |
|
244 |
//fileItems.(result); |
|
220 | 245 |
|
221 |
result = await DownloadPageAsync(PageNo,cts); |
|
246 |
result = await DownloadPageAsync(PageNo, cts);
|
|
222 | 247 |
} |
223 | 248 |
} |
224 | 249 |
else |
225 | 250 |
{ |
251 |
|
|
226 | 252 |
System.Diagnostics.Debug.WriteLine("DownloadPageAsync down"); |
227 | 253 |
|
228 | 254 |
string downloadFilePath = System.IO.Path.Combine(_localStorage, PageNo.ToString() + "." + _fileExt); |
229 | 255 |
|
230 | 256 |
Uri originalUri = new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString())); |
231 | 257 |
|
258 |
result = new PageItem |
|
259 |
{ |
|
260 |
PageNo = PageNo, |
|
261 |
OriginalUri = originalUri, |
|
262 |
LocalUri = new Uri(downloadFilePath, UriKind.Absolute), |
|
263 |
LocalFilePath = downloadFilePath |
|
264 |
}; |
|
265 |
|
|
266 |
if (fileItems.Where(x => x.PageNo == PageNo).Count() == 0) |
|
267 |
{ |
|
268 |
fileItems.Add(result); |
|
269 |
} |
|
232 | 270 |
|
233 | 271 |
using (System.Net.WebClient client = new System.Net.WebClient()) |
234 | 272 |
{ |
... | ... | |
248 | 286 |
// //(snd as System.Net.WebClient).Dispose(); |
249 | 287 |
//}; |
250 | 288 |
|
251 |
if(cts != null) |
|
289 |
if (cts != null) |
|
290 |
{ |
|
252 | 291 |
client.DownloadProgressChanged += (snd, ect) => |
253 | 292 |
{ |
254 |
if(cts.Value.IsCancellationRequested) |
|
293 |
IsDownload = client.IsBusy; |
|
294 |
|
|
295 |
if (cts.Value.IsCancellationRequested) |
|
255 | 296 |
{ |
256 | 297 |
client.CancelAsync(); |
257 | 298 |
} |
258 | 299 |
}; |
300 |
} |
|
301 |
|
|
302 |
client.DownloadFileCompleted += (snd, evt) => |
|
303 |
{ |
|
304 |
result.IsDownLoad = true; |
|
305 |
System.Diagnostics.Debug.WriteLine("Download : " + downloadFilePath); |
|
259 | 306 |
|
260 |
System.Diagnostics.Debug.WriteLine("Download : " + downloadFilePath); |
|
307 |
PageLoadCompleted?.Invoke(this, new PageLoadCompletedEventArgs(result)); |
|
308 |
}; |
|
261 | 309 |
|
262 | 310 |
await client.DownloadFileTaskAsync(originalUri, downloadFilePath); |
263 |
} |
|
264 |
|
|
265 |
result = new PageItem |
|
266 |
{ |
|
267 |
PageNo = PageNo, |
|
268 |
OriginalUri = originalUri, |
|
269 |
LocalUri = new Uri(downloadFilePath, UriKind.Absolute), |
|
270 |
LocalFilePath = downloadFilePath |
|
271 |
}; |
|
272 | 311 |
|
273 |
lock (fileItems) |
|
274 |
{ |
|
275 |
fileItems.Add(result); |
|
276 | 312 |
} |
313 |
|
|
314 |
//} |
|
277 | 315 |
//System.Diagnostics.Debug.WriteLine("Download : " + result.LocalFilePath); |
278 | 316 |
} |
279 | 317 |
} |
... | ... | |
283 | 321 |
} |
284 | 322 |
finally |
285 | 323 |
{ |
324 |
IsDownload = false; |
|
286 | 325 |
} |
287 | 326 |
|
288 | 327 |
return result; |
... | ... | |
304 | 343 |
backgroundWorker.CancelAsync(); |
305 | 344 |
backgroundWorker.Dispose(); |
306 | 345 |
|
307 |
fileItems.ForEach(x =>
|
|
346 |
foreach (var item in fileItems)
|
|
308 | 347 |
{ |
309 |
System.IO.File.Delete(x.LocalFilePath);
|
|
310 |
});
|
|
348 |
System.IO.File.Delete(item.LocalFilePath);
|
|
349 |
} |
|
311 | 350 |
|
312 | 351 |
System.IO.Directory.Delete(_localStorage, true); |
313 | 352 |
} |
내보내기 Unified diff