프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / PageManager / PageStorage.cs @ 00143658

이력 | 보기 | 이력해설 | 다운로드 (9.08 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 54a28343 taeseongkim
        bool IsRunWork = false;
15 d7e20d2d taeseongkim
        BackgroundWorker backgroundWorker;
16
        List<PageItem> fileItems = new List<PageItem>();
17
        string _localStorage;
18 54a28343 taeseongkim
        string _fileExt;
19 d7e20d2d taeseongkim
        string _BaseUri;
20
        int _TotalPages;
21 54a28343 taeseongkim
        int _TakeCount;
22 d7e20d2d taeseongkim
23
24
        BitmapFrame PageImage;
25
26 54a28343 taeseongkim
        public PageStorage(string BaseUri, string localStoragePath,string fileExt,int totalPages,int takeCount = 5)
27 d7e20d2d taeseongkim
        {
28
            try
29
            {
30
                backgroundWorker = new BackgroundWorker {WorkerSupportsCancellation = true };
31
                backgroundWorker.DoWork += BackgroundWorker_DoWork;
32
33 54a28343 taeseongkim
                _fileExt = fileExt;
34 d7e20d2d taeseongkim
                _BaseUri = BaseUri;
35
                _localStorage = localStoragePath;
36
                _TotalPages = totalPages;
37 54a28343 taeseongkim
                _TakeCount = takeCount;
38 d7e20d2d taeseongkim
39
                System.IO.Directory.CreateDirectory(_localStorage);
40
41
                //backgroundWorker.RunWorkerAsync(new int[] { 1, 10 });
42
            }
43
            catch (Exception ex)
44
            {
45
                throw new Exception("PageStorage", ex);
46
            }
47
        }
48
49
        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
50
        {
51
            int StartPageNo = -1;
52
            int TalkCount = -1;
53
54
            if (e.Argument != null)
55
            {
56
57
                var values = (e.Argument as int[]);
58
59
                if (values.Count() == 2)
60
                {
61
62
                    StartPageNo = values[0];
63
                    TalkCount = values[1];
64
65
                    DownloadPagesAsync(StartPageNo, TalkCount);
66
                }
67
            }
68
        }
69
70
        public void ResetImage()
71
        {
72
            //if (PageImage != null)
73
            //{
74
            //    PageImage = null;
75
            //}
76
77
            //PageImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
78
79
        }
80
81 54a28343 taeseongkim
        public async Task<BitmapFrame> GetPageImageAsync(int PageNo,bool IsRestart =false)
82 d7e20d2d taeseongkim
        {
83
            try
84
            {
85 54a28343 taeseongkim
86
                var localUri = await GetPageUriAsync(PageNo);
87
88
                PageImage = BitmapFrame.Create(localUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
89
            }
90
            catch (Exception ex)
91
            {
92
                throw new Exception("GetPageImageAsync(int PageNo,bool IsRestart =false)", ex);
93
            }
94
            finally
95
            {
96
            }
97
98
            return PageImage;
99
        }
100
101
        public async Task<Uri> GetPageUriAsync(int PageNo, bool IsRestart = false)
102
        {
103
            Uri result = null;
104
105
            try
106
            {
107 d7e20d2d taeseongkim
                System.Diagnostics.Debug.WriteLine("GetPageAsync");
108 54a28343 taeseongkim
109
                var pageItem = await DownloadPageAsync(PageNo);
110 d7e20d2d taeseongkim
                
111 54a28343 taeseongkim
                if (!IsRunWork && pageItem != null)
112 d7e20d2d taeseongkim
                {
113 54a28343 taeseongkim
                    var takePageNoList = Enumerable.Range(PageNo + 1, _TakeCount);
114 d7e20d2d taeseongkim
115 54a28343 taeseongkim
                    if (takePageNoList.Any(x => fileItems.Count(y => y.PageNo == x) == 0))
116 d7e20d2d taeseongkim
                    {
117 54a28343 taeseongkim
                        while (backgroundWorker.IsBusy)
118
                        {
119
                            if (backgroundWorker.IsBusy)
120
                            {
121
                                backgroundWorker.CancelAsync();
122
                            }
123 d7e20d2d taeseongkim
124 54a28343 taeseongkim
                            await Task.Delay(100);
125
                        }
126 d7e20d2d taeseongkim
127 54a28343 taeseongkim
                        backgroundWorker.RunWorkerAsync(new int[] { PageNo + 1, _TakeCount });
128
129
                        if (_TotalPages == _TakeCount)
130
                        {
131
                            IsRunWork = true;
132
                        }
133
                    }
134
                }
135 d7e20d2d taeseongkim
136 54a28343 taeseongkim
                result = pageItem.LocalUri;
137 d7e20d2d taeseongkim
            }
138 54a28343 taeseongkim
            catch (Exception ex)
139 d7e20d2d taeseongkim
            {
140 96bc6e8e taeseongkim
                if (!IsRestart)
141
                {
142
                    await Task.Delay(100);
143 54a28343 taeseongkim
144
                    result = await GetPageUriAsync(PageNo, true);
145 96bc6e8e taeseongkim
                }
146 a7372e37 taeseongkim
                //throw new Exception("GetPageAsync(string BasePageUri,int PageNo)", ex);
147 d7e20d2d taeseongkim
            }
148
            finally
149
            {
150
            }
151
152 54a28343 taeseongkim
            return result;
153 d7e20d2d taeseongkim
        }
154
155 448c5399 taeseongkim
        public async void DownloadPagesAsync(int StartPageNo, int TalkCount = 5)
156 d7e20d2d taeseongkim
        {
157 e8557bd7 taeseongkim
            try
158 d7e20d2d taeseongkim
            {
159 e8557bd7 taeseongkim
                if (StartPageNo + TalkCount > _TotalPages)
160
                {
161
                    TalkCount = _TotalPages - StartPageNo;
162
                }
163 d7e20d2d taeseongkim
164 e8557bd7 taeseongkim
                if (TalkCount > 0)
165 d7e20d2d taeseongkim
                {
166 e8557bd7 taeseongkim
                    for (int i = StartPageNo; i < StartPageNo + TalkCount; i++)
167 d7e20d2d taeseongkim
                    {
168 e8557bd7 taeseongkim
                        try
169
                        {
170 448c5399 taeseongkim
                            await DownloadPageAsync(i);
171
                            System.Threading.Thread.Sleep(200);
172 e8557bd7 taeseongkim
                        }
173
                        catch (Exception ex)
174
                        {
175
                            System.Diagnostics.Debug.WriteLine("DownloadPagesAsync err", ex);
176
                        }
177 72424099 taeseongkim
                        finally
178
                        {
179
                        }
180
181 d7e20d2d taeseongkim
                    }
182
                }
183
            }
184 e8557bd7 taeseongkim
            catch (Exception ex)
185
            {
186
                throw new Exception("DownloadPagesAsync : ", ex);
187
            }
188
            finally
189
            {
190 54a28343 taeseongkim
                GC.Collect(2);
191
                GC.Collect(2);
192 e8557bd7 taeseongkim
            }
193 d7e20d2d taeseongkim
        }
194
195
        public async Task<PageItem> DownloadPageAsync(int PageNo)
196
        {
197
            PageItem result = new PageItem { PageNo = PageNo };
198
199
            try
200
            {
201
                var page = fileItems.Where(x => x.PageNo == PageNo);
202
203
                if (page.Count() > 0)
204
                {
205
                    System.Diagnostics.Debug.WriteLine("DownloadPageAsync fileItems");
206
207
                    PageItem item = page.First();
208
209
                    /// 파일 체크 후 없으면 다시 다운로드
210
                    if (System.IO.File.Exists(item.LocalFilePath))
211
                    {
212
                        result = page.First();
213
                    }
214
                    else
215
                    {
216
                        fileItems.Remove(item);
217
218
                        result = await DownloadPageAsync(PageNo);
219
                    }
220
                }
221
                else
222
                {
223
                    System.Diagnostics.Debug.WriteLine("DownloadPageAsync down");
224
225 54a28343 taeseongkim
                    string downloadFilePath = System.IO.Path.Combine(_localStorage, PageNo.ToString() + "." + _fileExt);
226 d7e20d2d taeseongkim
227 96bc6e8e taeseongkim
                    Uri originalUri = new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString()));
228
229 d7e20d2d taeseongkim
230 448c5399 taeseongkim
                    using (System.Net.WebClient client = new System.Net.WebClient())
231
                    { 
232
                        client.UseDefaultCredentials = true;
233
                        System.Net.IWebProxy webProxy = client.Proxy;
234 d7e20d2d taeseongkim
235 448c5399 taeseongkim
                        if (webProxy != null)
236 d97dbc7f taeseongkim
                        {
237 448c5399 taeseongkim
                            // Use the default credentials of the logged on user.
238
                            webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
239 d97dbc7f taeseongkim
                        }
240
241 448c5399 taeseongkim
                        //client.DownloadFileCompleted += (snd, evt) =>
242
                        //{
243
                           
244 d7e20d2d taeseongkim
245 448c5399 taeseongkim
                        //    //(snd as System.Net.WebClient).Dispose();
246
                        //};
247 d7e20d2d taeseongkim
248 448c5399 taeseongkim
                        System.Diagnostics.Debug.WriteLine("Download : " + downloadFilePath);
249 72424099 taeseongkim
250 448c5399 taeseongkim
                        await client.DownloadFileTaskAsync(originalUri, downloadFilePath);
251
                    }
252
253
                    result = new PageItem
254
                    {
255
                        PageNo = PageNo,
256
                        OriginalUri = originalUri,
257
                        LocalUri = new Uri(downloadFilePath, UriKind.Absolute),
258
                        LocalFilePath = downloadFilePath
259
                    };
260
261
                    lock (fileItems)
262
                    {
263
                        fileItems.Add(result);
264
                    }
265 54a28343 taeseongkim
                    //System.Diagnostics.Debug.WriteLine("Download : " + result.LocalFilePath);
266 d7e20d2d taeseongkim
                }
267
            }
268
            catch (Exception ex)
269
            {
270
                throw new Exception("DownloadPageAsync : ", ex);
271
            }
272
            finally
273
            {
274
            }
275
276
            return result;
277
        }
278
279
        public void Clear()
280
        {
281
            try
282
            {
283
                ResetImage();
284
            }
285
            catch (Exception ex)
286
            {
287
                System.Diagnostics.Debug.WriteLine(ex.ToString());
288
            }
289
290
            try
291
            {
292
                backgroundWorker.CancelAsync();
293
                backgroundWorker.Dispose();
294
295
                fileItems.ForEach(x =>
296
                {
297
                    System.IO.File.Delete(x.LocalFilePath);
298
                });
299
300
                System.IO.Directory.Delete(_localStorage, true);
301
            }
302
            catch (Exception ex)
303
            {
304
                System.Diagnostics.Debug.WriteLine(ex.ToString());
305
            }
306
        }
307
    }
308
}
클립보드 이미지 추가 (최대 크기: 500 MB)