프로젝트

일반

사용자정보

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

markus / KCOM / PageManager / PageStorage.cs @ 366f00c2

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