1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.Net;
|
---|
4 | using System.Xml;
|
---|
5 | using System.Diagnostics;
|
---|
6 |
|
---|
7 | using SharpVectors.Net;
|
---|
8 |
|
---|
9 | namespace SharpVectors.Renderers.Utils
|
---|
10 | {
|
---|
11 | public sealed class WpfCacheManager : ICacheManager
|
---|
12 | {
|
---|
13 | private XmlElement lastCacheElm;
|
---|
14 | private Uri lastUri;
|
---|
15 |
|
---|
16 | private string cacheDir;
|
---|
17 | private string cacheDocPath;
|
---|
18 | private XmlDocument cacheDoc = new XmlDocument();
|
---|
19 |
|
---|
20 | public WpfCacheManager()
|
---|
21 | {
|
---|
22 | cacheDoc = new XmlDocument();
|
---|
23 |
|
---|
24 | cacheDir = Path.Combine(WpfApplicationContext.ExecutableDirectory.FullName, "cache/");
|
---|
25 | cacheDocPath = Path.Combine(cacheDir, "cache.xml");
|
---|
26 |
|
---|
27 | LoadDoc();
|
---|
28 | }
|
---|
29 |
|
---|
30 | public WpfCacheManager(string cacheDir)
|
---|
31 | {
|
---|
32 | cacheDoc = new XmlDocument();
|
---|
33 |
|
---|
34 | this.cacheDir = Path.Combine(
|
---|
35 | WpfApplicationContext.ExecutableDirectory.FullName,
|
---|
36 | cacheDir);
|
---|
37 |
|
---|
38 | cacheDocPath = Path.Combine(cacheDir, "cache.xml");
|
---|
39 |
|
---|
40 | LoadDoc();
|
---|
41 | }
|
---|
42 |
|
---|
43 | private void LoadDoc()
|
---|
44 | {
|
---|
45 | if (cacheDoc == null)
|
---|
46 | {
|
---|
47 | cacheDoc = new XmlDocument();
|
---|
48 | }
|
---|
49 |
|
---|
50 | if(File.Exists(cacheDocPath))
|
---|
51 | {
|
---|
52 | cacheDoc.Load(cacheDocPath);
|
---|
53 | }
|
---|
54 | else
|
---|
55 | {
|
---|
56 | Directory.CreateDirectory(Directory.GetParent(cacheDocPath).FullName);
|
---|
57 | cacheDoc.LoadXml("<cache />");
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private void SaveDoc()
|
---|
62 | {
|
---|
63 | if (cacheDoc == null)
|
---|
64 | {
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | cacheDoc.Save(cacheDocPath);
|
---|
68 | }
|
---|
69 |
|
---|
70 | private XmlElement GetCacheElm(Uri uri)
|
---|
71 | {
|
---|
72 | if(uri == lastUri && lastCacheElm != null)
|
---|
73 | {
|
---|
74 | return lastCacheElm;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | //string xpath = "/cache/resource[@url='" + uri.ToString() + "']";
|
---|
79 | string xpath = "/cache/resource[@url='" + uri.ToString().Replace("'", "'") + "']";
|
---|
80 | XmlNode node = cacheDoc.SelectSingleNode(xpath);
|
---|
81 | if(node != null)
|
---|
82 | {
|
---|
83 | lastCacheElm = node as XmlElement;
|
---|
84 | }
|
---|
85 | else
|
---|
86 | {
|
---|
87 | lastCacheElm = cacheDoc.CreateElement("resource");
|
---|
88 | cacheDoc.DocumentElement.AppendChild(lastCacheElm);
|
---|
89 | lastCacheElm.SetAttribute("url", uri.ToString());
|
---|
90 | }
|
---|
91 |
|
---|
92 | lastUri = uri;
|
---|
93 | return lastCacheElm;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | private Uri GetLocalPathUri(XmlElement cacheElm)
|
---|
98 | {
|
---|
99 | if (cacheElm.HasAttribute("local-path"))
|
---|
100 | {
|
---|
101 | string path = Path.Combine(cacheDir, cacheElm.GetAttribute("local-path"));
|
---|
102 | if(File.Exists(path))
|
---|
103 | {
|
---|
104 | path = "file:///" + path.Replace('\\', '/');
|
---|
105 | return new Uri(path);
|
---|
106 | }
|
---|
107 | else
|
---|
108 | {
|
---|
109 | cacheElm.RemoveAttribute("local-path");
|
---|
110 | return null;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | return null;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public long Size
|
---|
120 | {
|
---|
121 | get
|
---|
122 | {
|
---|
123 | DirectoryInfo di = new DirectoryInfo(cacheDir);
|
---|
124 | FileInfo[] files = di.GetFiles();
|
---|
125 | long size = 0;
|
---|
126 | foreach(FileInfo file in files)
|
---|
127 | {
|
---|
128 | size += file.Length;
|
---|
129 | }
|
---|
130 | return size;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void Clear()
|
---|
135 | {
|
---|
136 | DirectoryInfo di = new DirectoryInfo(cacheDir);
|
---|
137 | FileInfo[] files = di.GetFiles();
|
---|
138 | foreach(FileInfo file in files)
|
---|
139 | {
|
---|
140 | try
|
---|
141 | {
|
---|
142 | file.Delete();
|
---|
143 | }
|
---|
144 | catch{}
|
---|
145 | }
|
---|
146 |
|
---|
147 | cacheDoc = new XmlDocument();
|
---|
148 |
|
---|
149 | LoadDoc();
|
---|
150 | }
|
---|
151 |
|
---|
152 | public CacheInfo GetCacheInfo(Uri uri)
|
---|
153 | {
|
---|
154 | XmlElement cacheElm = GetCacheElm(uri);
|
---|
155 |
|
---|
156 | DateTime expires = DateTime.MinValue;
|
---|
157 | if(cacheElm.HasAttribute("expires"))
|
---|
158 | {
|
---|
159 | expires = DateTime.Parse(cacheElm.GetAttribute("expires"));
|
---|
160 | }
|
---|
161 |
|
---|
162 | DateTime lastModified = DateTime.MinValue;
|
---|
163 | if(cacheElm.HasAttribute("last-modified"))
|
---|
164 | {
|
---|
165 | lastModified = DateTime.Parse(cacheElm.GetAttribute("last-modified"));
|
---|
166 | }
|
---|
167 |
|
---|
168 | Uri cachedUri = GetLocalPathUri(cacheElm);
|
---|
169 |
|
---|
170 | return new CacheInfo(expires, cacheElm.GetAttribute("etag"), lastModified, cachedUri, cacheElm.GetAttribute("content-type"));
|
---|
171 | }
|
---|
172 |
|
---|
173 | public void SetCacheInfo(Uri uri, CacheInfo cacheInfo, Stream stream)
|
---|
174 | {
|
---|
175 | XmlElement cacheElm = GetCacheElm(uri);
|
---|
176 |
|
---|
177 | if(cacheInfo != null)
|
---|
178 | {
|
---|
179 | if(cacheInfo.ETag != null)
|
---|
180 | {
|
---|
181 | cacheElm.SetAttribute("etag", cacheInfo.ETag);
|
---|
182 | }
|
---|
183 | else
|
---|
184 | {
|
---|
185 | cacheElm.RemoveAttribute("etag");
|
---|
186 | }
|
---|
187 |
|
---|
188 | if(cacheInfo.ContentType != null)
|
---|
189 | {
|
---|
190 | cacheElm.SetAttribute("content-type", cacheInfo.ContentType);
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | cacheElm.RemoveAttribute("content-type");
|
---|
195 | }
|
---|
196 |
|
---|
197 | if(cacheInfo.Expires != DateTime.MinValue)
|
---|
198 | {
|
---|
199 | cacheElm.SetAttribute("expires", cacheInfo.Expires.ToString("s"));
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | cacheElm.RemoveAttribute("expires");
|
---|
204 | }
|
---|
205 |
|
---|
206 | if(cacheInfo.LastModified != DateTime.MinValue)
|
---|
207 | {
|
---|
208 | cacheElm.SetAttribute("last-modified", cacheInfo.LastModified.ToString("s"));
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | cacheElm.RemoveAttribute("last-modified");
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | if (stream != null)
|
---|
217 | {
|
---|
218 | string localPath;
|
---|
219 | if(cacheElm.HasAttribute("local-path"))
|
---|
220 | {
|
---|
221 | localPath = cacheElm.GetAttribute("local-path");
|
---|
222 | }
|
---|
223 | else
|
---|
224 | {
|
---|
225 | localPath = Guid.NewGuid().ToString() + ".cache";
|
---|
226 | cacheElm.SetAttribute("local-path", localPath);
|
---|
227 | }
|
---|
228 |
|
---|
229 | stream.Position = 0;
|
---|
230 | int count;
|
---|
231 | byte[] buffer = new byte[4096];
|
---|
232 |
|
---|
233 | FileStream fs = File.OpenWrite(Path.Combine(cacheDir, localPath));
|
---|
234 | while((count = stream.Read(buffer, 0, 4096)) > 0) fs.Write(buffer, 0, count);
|
---|
235 | fs.Flush();
|
---|
236 | fs.Close();
|
---|
237 | }
|
---|
238 | SaveDoc();
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|