markus / FileDownloader / PageStorage - 복사본.cs @ eeb0a39c
이력 | 보기 | 이력해설 | 다운로드 (3.85 KB)
1 | eeb0a39c | taeseongkim | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Text; |
||
5 | using System.Threading.Tasks; |
||
6 | using System.Windows.Media.Imaging; |
||
7 | |||
8 | namespace PageManager |
||
9 | { |
||
10 | public class PageStorage |
||
11 | { |
||
12 | List<PageItem> fileItems = new List<PageItem>(); |
||
13 | string _localStorage; |
||
14 | string _BaseUri; |
||
15 | |||
16 | BitmapFrame PageImage; |
||
17 | |||
18 | public PageStorage(string BaseUri, string localStoragePath) |
||
19 | { |
||
20 | try |
||
21 | { |
||
22 | _BaseUri = BaseUri; |
||
23 | _localStorage = localStoragePath; |
||
24 | |||
25 | System.IO.Directory.CreateDirectory(_localStorage); |
||
26 | |||
27 | |||
28 | //GetPageAsync(1).ConfigureAwait(false); |
||
29 | } |
||
30 | catch (Exception ex) |
||
31 | { |
||
32 | throw new Exception("PageStorage", ex); |
||
33 | } |
||
34 | } |
||
35 | |||
36 | public void ResetImage() |
||
37 | { |
||
38 | if (PageImage != null) |
||
39 | { |
||
40 | PageImage.UriSource = null; |
||
41 | PageImage = null; |
||
42 | } |
||
43 | |||
44 | //PageImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache; |
||
45 | |||
46 | } |
||
47 | |||
48 | public async Task<BitmapFrame> GetPageAsync(int PageNo) |
||
49 | { |
||
50 | try |
||
51 | { = BitmapFrame.Create(new Uri(), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); |
||
52 | ResetImage(); |
||
53 | |||
54 | PageImage = new BitmapImage |
||
55 | { |
||
56 | CacheOption = BitmapCacheOption.OnDemand, |
||
57 | CreateOptions = BitmapCreateOptions.IgnoreImageCache |
||
58 | }; |
||
59 | |||
60 | var page = fileItems.Where(x => x.PageNo == PageNo); |
||
61 | |||
62 | PageImage.BeginInit(); |
||
63 | |||
64 | if (page.Count() > 0) |
||
65 | { |
||
66 | PageImage.UriSource = page.First().LocalUri; |
||
67 | } |
||
68 | else |
||
69 | { |
||
70 | string downloadFilePath = System.IO.Path.Combine(_localStorage,PageNo.ToString() + ".png"); |
||
71 | |||
72 | PageItem item = new PageItem |
||
73 | { |
||
74 | PageNo = PageNo, |
||
75 | OriginalUri = new Uri(_BaseUri.Replace("{PageNo}", PageNo.ToString())), |
||
76 | LocalUri = new Uri(downloadFilePath, UriKind.Absolute), |
||
77 | LocalFilePath = downloadFilePath |
||
78 | }; |
||
79 | |||
80 | fileItems.Add(item); |
||
81 | |||
82 | using (System.Net.WebClient client = new System.Net.WebClient()) |
||
83 | { |
||
84 | client.UseDefaultCredentials = true; |
||
85 | var task = client.DownloadFileTaskAsync(item.OriginalUri, downloadFilePath); |
||
86 | |||
87 | client.DownloadFileCompleted += (snd, evt) => |
||
88 | { |
||
89 | item.IsDownLoad = true; |
||
90 | }; |
||
91 | |||
92 | await task; |
||
93 | } |
||
94 | |||
95 | PageImage.UriSource = item.LocalUri; |
||
96 | } |
||
97 | |||
98 | PageImage.EndInit(); |
||
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 | GC.Collect(2); |
||
108 | } |
||
109 | |||
110 | |||
111 | return PageImage; |
||
112 | } |
||
113 | |||
114 | public void Clear() |
||
115 | { |
||
116 | try |
||
117 | { |
||
118 | ResetImage(); |
||
119 | } |
||
120 | catch (Exception ex) |
||
121 | { |
||
122 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
123 | } |
||
124 | |||
125 | try |
||
126 | { |
||
127 | fileItems.ForEach(x => |
||
128 | { |
||
129 | System.IO.File.Delete(x.LocalFilePath); |
||
130 | }); |
||
131 | |||
132 | System.IO.Directory.Delete(_localStorage, true); |
||
133 | } |
||
134 | catch (Exception ex) |
||
135 | { |
||
136 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | } |