This repository was archived by the owner on May 2, 2018. It is now read-only.
forked from sitdap/dynamic-image
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathXmlCacheProvider.cs
More file actions
181 lines (153 loc) · 7.22 KB
/
XmlCacheProvider.cs
File metadata and controls
181 lines (153 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml.Linq;
namespace SoundInTheory.DynamicImage.Caching
{
public class XmlCacheProvider : DiskCacheProviderBase, IDisposable
{
private string _docPath;
private FileSystemWatcher _watcher;
private XDocument _doc;
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
_docPath = HttpContext.Current.Server.MapPath(string.Format("{0}/DynamicImageCache.xml", CachePath));
EnsureDocument();
_watcher = new FileSystemWatcher(Path.GetDirectoryName(_docPath), "DynamicImageCache.xml");
_watcher.Deleted += (sender, e) => _doc = null;
}
void IDisposable.Dispose()
{
if (_watcher != null)
_watcher.Dispose();
}
private void EnsureDocument()
{
lock (this)
{
string imageCacheFolder = HttpContext.Current.Server.MapPath(CachePath);
if (!Directory.Exists(imageCacheFolder))
Directory.CreateDirectory(imageCacheFolder);
if (!File.Exists(_docPath))
{
string xml = new XElement("cache").ToString();
using (var stream = File.CreateText(_docPath))
stream.Write(xml);
}
if (_doc == null)
_doc = XDocument.Load(_docPath);
}
}
public override bool ExistsInCache(string cacheKey)
{
EnsureDocument();
return _doc.Root.Elements().Any(e => e.Attribute("id").Value == cacheKey);
}
public override void AddToCache(string cacheKey, GeneratedImage generatedImage, Dependency[] dependencies)
{
EnsureDocument();
// Save image to disk.
SaveImageToDiskCache(cacheKey, generatedImage);
lock (_doc)
{
// Double-check that item hasn't been added to cache since we checked.
if (ExistsInCache(cacheKey))
return;
var itemElement = new XElement("item",
new XAttribute("id", cacheKey),
new XAttribute("isImagePresent", generatedImage.Properties.IsImagePresent),
new XAttribute("format", generatedImage.Properties.Format),
new XAttribute("colorDepth", generatedImage.Properties.ColorDepth));
if (generatedImage.Properties.Width != null)
itemElement.Add(new XAttribute("width", generatedImage.Properties.Width.Value));
if (generatedImage.Properties.Height != null)
itemElement.Add(new XAttribute("height", generatedImage.Properties.Height.Value));
if (generatedImage.Properties.JpegCompressionLevel != null)
itemElement.Add(new XAttribute("jpegCompressionLevel", generatedImage.Properties.JpegCompressionLevel.Value));
XElement dependenciesElement = new XElement("dependencies");
itemElement.Add(dependenciesElement);
foreach (var dependency in dependencies)
dependenciesElement.Add(new XElement("dependency",
new XAttribute("text1", dependency.Text1 ?? string.Empty),
new XAttribute("text2", dependency.Text2 ?? string.Empty),
new XAttribute("text3", dependency.Text3 ?? string.Empty),
new XAttribute("text4", dependency.Text4 ?? string.Empty)));
_doc.Root.Add(itemElement);
_doc.Save(_docPath);
}
}
public override ImageProperties GetPropertiesFromCache(string cacheKey)
{
EnsureDocument();
lock (_doc)
{
ImageProperties result = null;
XElement itemElement = _doc.Root.Elements().SingleOrDefault(e => e.Attribute("id").Value == cacheKey);
if (itemElement != null)
result = GetImageProperties(itemElement);
return result;
}
}
private static ImageProperties GetImageProperties(XElement itemElement)
{
string cacheKey;
ImageProperties result;
GetImageProperties(itemElement, out cacheKey, out result);
return result;
}
private static void GetImageProperties(XElement itemElement, out string cacheKey, out ImageProperties imageProperties)
{
imageProperties = new ImageProperties();
imageProperties.IsImagePresent = Convert.ToBoolean(itemElement.Attribute("isImagePresent").Value);
imageProperties.Format = (DynamicImageFormat)Enum.Parse(typeof(DynamicImageFormat), itemElement.Attribute("format").Value);
if (itemElement.Attribute("width") != null)
imageProperties.Width = Convert.ToInt32(itemElement.Attribute("width").Value);
if (itemElement.Attribute("height") != null)
imageProperties.Height = Convert.ToInt32(itemElement.Attribute("height").Value);
imageProperties.ColorDepth = Convert.ToInt32(itemElement.Attribute("colorDepth").Value);
if (itemElement.Attribute("jpegCompressionLevel") != null)
imageProperties.JpegCompressionLevel = Convert.ToInt32(itemElement.Attribute("jpegCompressionLevel").Value);
cacheKey = itemElement.Attribute("id").Value;
}
public override void RemoveAllFromCache()
{
EnsureDocument();
lock (_doc)
{
foreach (XElement itemElement in _doc.Root.Elements())
{
string cacheKey;
ImageProperties imageProperties;
GetImageProperties(itemElement, out cacheKey, out imageProperties);
DeleteImageFromDiskCache(cacheKey, imageProperties, HttpContext.Current);
itemElement.Remove();
}
_doc.Save(_docPath);
}
}
public override void RemoveFromCache(Dependency dependency)
{
EnsureDocument();
lock (_doc)
{
var items = _doc.Root.Elements().Where(e => e.Element("dependencies").Elements().Any(m =>
m.Attribute("text1").Value == (dependency.Text1) &&
m.Attribute("text2").Value == (dependency.Text2 ?? "") &&
m.Attribute("text3").Value == (dependency.Text3 ?? "") &&
m.Attribute("text4").Value == (dependency.Text4 ?? "")
)).ToList();
foreach (XElement itemElement in items)
{
string cacheKey;
ImageProperties imageProperties;
GetImageProperties(itemElement, out cacheKey, out imageProperties);
DeleteImageFromDiskCache(cacheKey, imageProperties, HttpContext.Current);
itemElement.Remove();
}
_doc.Save(_docPath);
}
}
}
}