This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
/
AzureBlobCache.cs
451 lines (393 loc) · 19.3 KB
/
AzureBlobCache.cs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AzureBlobCache.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Provides an <see cref="IImageCache" /> implementation that uses Azure blob storage.
// The cache is self healing and cleaning.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Plugins.AzureBlobCache
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using ImageProcessor.Web.Caching;
using ImageProcessor.Web.Extensions;
using ImageProcessor.Web.Helpers;
using ImageProcessor.Web.HttpModules;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
/// <summary>
/// Provides an <see cref="IImageCache"/> implementation that uses Azure blob storage.
/// The cache is self healing and cleaning.
/// </summary>
public class AzureBlobCache : ImageCacheBase
{
/// <summary>
/// The regular expression for parsing a remote uri.
/// </summary>
private static readonly Regex RemoteRegex = new Regex("^http(s)?://", RegexOptions.Compiled);
/// <summary>
/// The assembly version.
/// </summary>
private static readonly string AssemblyVersion = typeof(ImageProcessingModule).Assembly.GetName().Version.ToString();
/// <summary>
/// The cloud cached blob container.
/// </summary>
private readonly CloudBlobContainer cloudCachedBlobContainer;
/// <summary>
/// The cloud source blob container.
/// </summary>
private readonly CloudBlobContainer cloudSourceBlobContainer;
/// <summary>
/// The cached root url for a content delivery network.
/// </summary>
private readonly string cachedCdnRoot;
/// <summary>
/// Determines if the CDN request is redirected or rewritten
/// </summary>
private readonly bool streamCachedImage;
/// <summary>
/// The cached rewrite path.
/// </summary>
private string cachedRewritePath;
/// <summary>
/// The content MIME type.
/// </summary>
private string mimeType;
/// <summary>
/// Initializes a new instance of the <see cref="AzureBlobCache"/> class.
/// </summary>
/// <param name="requestPath">
/// The request path for the image.
/// </param>
/// <param name="fullPath">
/// The full path for the image.
/// </param>
/// <param name="querystring">
/// The querystring containing instructions.
/// </param>
public AzureBlobCache(string requestPath, string fullPath, string querystring)
: base(requestPath, fullPath, querystring)
{
// Retrieve storage accounts from connection string.
CloudStorageAccount cloudCachedStorageAccount = CloudStorageAccount.Parse(this.Settings["CachedStorageAccount"]);
// Create the blob clients.
CloudBlobClient cloudCachedBlobClient = cloudCachedStorageAccount.CreateCloudBlobClient();
// Retrieve references to a container.
this.cloudCachedBlobContainer = CreateContainer(cloudCachedBlobClient, this.Settings["CachedBlobContainer"], BlobContainerPublicAccessType.Blob);
string sourceAccount = this.Settings.ContainsKey("SourceStorageAccount") ? this.Settings["SourceStorageAccount"] : string.Empty;
// Repeat for source if it exists
if (!string.IsNullOrWhiteSpace(sourceAccount))
{
CloudStorageAccount cloudSourceStorageAccount = CloudStorageAccount.Parse(this.Settings["SourceStorageAccount"]);
CloudBlobClient cloudSourceBlobClient = cloudSourceStorageAccount.CreateCloudBlobClient();
this.cloudSourceBlobContainer = cloudSourceBlobClient.GetContainerReference(this.Settings["SourceBlobContainer"]);
}
this.cachedCdnRoot = this.Settings.ContainsKey("CachedCDNRoot")
? this.Settings["CachedCDNRoot"]
: this.cloudCachedBlobContainer.Uri.ToString().TrimEnd(this.cloudCachedBlobContainer.Name.ToCharArray());
// This setting was added to facilitate streaming of the blob resource directly instead of a redirect. This is beneficial for CDN purposes
// but caution should be taken if not used with a CDN as it will add quite a bit of overhead to the site.
// See: https://github.com/JimBobSquarePants/ImageProcessor/issues/161
this.streamCachedImage = this.Settings.ContainsKey("StreamCachedImage") && this.Settings["StreamCachedImage"].ToLower() == "true";
}
/// <summary>
/// Gets a value indicating whether the image is new or updated in an asynchronous manner.
/// </summary>
/// <returns>
/// The asynchronous <see cref="Task"/> returning the value.
/// </returns>
public override async Task<bool> IsNewOrUpdatedAsync()
{
string cachedFileName = await this.CreateCachedFileNameAsync();
// Collision rate of about 1 in 10000 for the folder structure.
// That gives us massive scope to store millions of files.
string pathFromKey = string.Join("\\", cachedFileName.ToCharArray().Take(6));
this.CachedPath = Path.Combine(this.cloudCachedBlobContainer.Uri.ToString(), pathFromKey, cachedFileName).Replace(@"\", "/");
this.cachedRewritePath = Path.Combine(this.cachedCdnRoot, this.cloudCachedBlobContainer.Name, pathFromKey, cachedFileName).Replace(@"\", "/");
bool isUpdated = false;
CachedImage cachedImage = CacheIndexer.Get(this.CachedPath);
if (new Uri(this.CachedPath).IsFile)
{
FileInfo fileInfo = new FileInfo(this.CachedPath);
if (fileInfo.Exists)
{
// Pull the latest info.
fileInfo.Refresh();
cachedImage = new CachedImage
{
Key = Path.GetFileNameWithoutExtension(this.CachedPath),
Path = this.CachedPath,
CreationTimeUtc = fileInfo.CreationTimeUtc
};
CacheIndexer.Add(cachedImage);
}
}
if (cachedImage == null)
{
string blobPath = this.CachedPath.Substring(this.cloudCachedBlobContainer.Uri.ToString().Length + 1);
CloudBlockBlob blockBlob = this.cloudCachedBlobContainer.GetBlockBlobReference(blobPath);
if (await blockBlob.ExistsAsync())
{
// Pull the latest info.
await blockBlob.FetchAttributesAsync();
if (blockBlob.Properties.LastModified.HasValue)
{
cachedImage = new CachedImage
{
Key = Path.GetFileNameWithoutExtension(this.CachedPath),
Path = this.CachedPath,
CreationTimeUtc = blockBlob.Properties.LastModified.Value.UtcDateTime
};
CacheIndexer.Add(cachedImage);
}
}
}
if (cachedImage == null)
{
// Nothing in the cache so we should return true.
isUpdated = true;
}
else
{
// Check to see if the cached image is set to expire.
if (this.IsExpired(cachedImage.CreationTimeUtc))
{
CacheIndexer.Remove(this.CachedPath);
isUpdated = true;
}
}
return isUpdated;
}
/// <summary>
/// Adds the image to the cache in an asynchronous manner.
/// </summary>
/// <param name="stream">
/// The stream containing the image data.
/// </param>
/// <param name="contentType">
/// The content type of the image.
/// </param>
/// <returns>
/// The <see cref="Task"/> representing an asynchronous operation.
/// </returns>
public override async Task AddImageToCacheAsync(Stream stream, string contentType)
{
this.mimeType = contentType;
string blobPath = this.CachedPath.Substring(this.cloudCachedBlobContainer.Uri.ToString().Length + 1);
CloudBlockBlob blockBlob = this.cloudCachedBlobContainer.GetBlockBlobReference(blobPath);
await blockBlob.UploadFromStreamAsync(stream);
blockBlob.Properties.ContentType = contentType;
blockBlob.Properties.CacheControl = string.Format("public, max-age={0}", this.MaxDays * 86400);
await blockBlob.SetPropertiesAsync();
blockBlob.Metadata.Add("ImageProcessedBy", "ImageProcessor.Web/" + AssemblyVersion);
await blockBlob.SetMetadataAsync();
}
/// <summary>
/// Trims the cache of any expired items in an asynchronous manner.
/// </summary>
/// <returns>
/// The asynchronous <see cref="Task"/> representing an asynchronous operation.
/// </returns>
public override async Task TrimCacheAsync()
{
Uri uri = new Uri(this.CachedPath);
string path = uri.GetLeftPart(UriPartial.Path).Substring(this.cloudCachedBlobContainer.Uri.ToString().Length + 1);
string directory = path.Substring(0, path.LastIndexOf('/'));
string parent = directory.Substring(0, directory.LastIndexOf('/'));
BlobContinuationToken continuationToken = null;
List<IListBlobItem> results = new List<IListBlobItem>();
// Loop through the all the files in a non blocking fashion.
do
{
BlobResultSegment response = await this.cloudCachedBlobContainer
.ListBlobsSegmentedAsync(parent, true, BlobListingDetails.Metadata, 5000, continuationToken, null, null);
continuationToken = response.ContinuationToken;
results.AddRange(response.Results);
}
while (continuationToken != null);
// Now leap through and delete.
foreach (CloudBlockBlob blob in results
.Where((blobItem, type) => blobItem is CloudBlockBlob)
.Cast<CloudBlockBlob>()
.OrderBy(b => b.Properties.LastModified != null ? b.Properties.LastModified.Value.UtcDateTime : new DateTime()))
{
if (blob.Properties.LastModified.HasValue
&& !this.IsExpired(blob.Properties.LastModified.Value.UtcDateTime))
{
break;
}
// Remove from the cache and delete each CachedImage.
CacheIndexer.Remove(blob.Name);
await blob.DeleteAsync();
}
}
/// <summary>
/// Gets a string identifying the cached file name.
/// </summary>
/// <returns>
/// The asynchronous <see cref="Task"/> returning the value.
/// </returns>
public override async Task<string> CreateCachedFileNameAsync()
{
string streamHash = string.Empty;
try
{
if (new Uri(this.RequestPath).IsFile)
{
// Get the hash for the filestream. That way we can ensure that if the image is
// updated but has the same name we will know.
FileInfo imageFileInfo = new FileInfo(this.RequestPath);
if (imageFileInfo.Exists)
{
// Pull the latest info.
imageFileInfo.Refresh();
// Checking the stream itself is far too processor intensive so we make a best guess.
string creation = imageFileInfo.CreationTimeUtc.ToString(CultureInfo.InvariantCulture);
string length = imageFileInfo.Length.ToString(CultureInfo.InvariantCulture);
streamHash = string.Format("{0}{1}", creation, length);
}
}
else if (this.cloudSourceBlobContainer != null)
{
string container = RemoteRegex.Replace(this.cloudSourceBlobContainer.Uri.ToString(), string.Empty);
string blobPath = RemoteRegex.Replace(this.RequestPath, string.Empty);
blobPath = blobPath.Replace(container, string.Empty).TrimStart('/');
CloudBlockBlob blockBlob = this.cloudSourceBlobContainer.GetBlockBlobReference(blobPath);
if (await blockBlob.ExistsAsync())
{
// Pull the latest info.
await blockBlob.FetchAttributesAsync();
if (blockBlob.Properties.LastModified.HasValue)
{
string creation = blockBlob.Properties
.LastModified.Value.UtcDateTime
.ToString(CultureInfo.InvariantCulture);
string length = blockBlob.Properties.Length.ToString(CultureInfo.InvariantCulture);
streamHash = string.Format("{0}{1}", creation, length);
}
}
}
else
{
// Try and get the headers for the file, this should allow cache busting for remote files.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.RequestPath);
request.Method = "HEAD";
using (HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync()))
{
string lastModified = response.LastModified.ToUniversalTime().ToString(CultureInfo.InvariantCulture);
string length = response.ContentLength.ToString(CultureInfo.InvariantCulture);
streamHash = string.Format("{0}{1}", lastModified, length);
}
}
}
catch
{
streamHash = string.Empty;
}
// Use an sha1 hash of the full path including the querystring to create the image name.
// That name can also be used as a key for the cached image and we should be able to use
// The characters of that hash as sub-folders.
string parsedExtension = ImageHelpers.GetExtension(this.FullPath, this.Querystring);
string encryptedName = (streamHash + this.FullPath).ToSHA1Fingerprint();
string cachedFileName = string.Format(
"{0}.{1}",
encryptedName,
!string.IsNullOrWhiteSpace(parsedExtension) ? parsedExtension.Replace(".", string.Empty) : "jpg");
return cachedFileName;
}
/// <summary>
/// Rewrites the path to point to the cached image.
/// </summary>
/// <param name="context">
/// The <see cref="HttpContext"/> encapsulating all information about the request.
/// </param>
public override void RewritePath(HttpContext context)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.cachedRewritePath);
if (this.streamCachedImage)
{
// Write the blob storage directly to the stream
request.Method = "GET";
TryFiveTimes(() =>
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream cachedStream = response.GetResponseStream();
if (cachedStream != null)
{
HttpResponse contextResponse = context.Response;
cachedStream.CopyTo(contextResponse.OutputStream);
// Mimetype can be null when returning from the cache.
ImageProcessingModule.SetHeaders(
context,
string.IsNullOrWhiteSpace(this.mimeType) ? contextResponse.ContentType : this.mimeType,
null,
this.MaxDays,
response.StatusCode);
}
}
});
}
else
{
// Redirect the request to the blob URL
request.Method = "HEAD";
TryFiveTimes(() =>
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
HttpStatusCode responseCode = response.StatusCode;
ImageProcessingModule.AddCorsRequestHeaders(context);
context.Response.Redirect(responseCode == HttpStatusCode.NotFound ? this.CachedPath : this.cachedRewritePath, false);
}
});
}
}
/// <summary>
/// Returns the cache container, creating a new one if none exists.
/// </summary>
/// <param name="cloudBlobClient"><see cref="CloudBlobClient"/> where the container is stored.</param>
/// <param name="containerName">The name of the container.</param>
/// <param name="accessType"><see cref="BlobContainerPublicAccessType"/> indicating the access permissions.</param>
/// <returns>The <see cref="CloudBlobContainer"/></returns>
private static CloudBlobContainer CreateContainer(CloudBlobClient cloudBlobClient, string containerName, BlobContainerPublicAccessType accessType)
{
CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName);
container.CreateIfNotExists();
container.SetPermissions(new BlobContainerPermissions { PublicAccess = accessType });
return container;
}
/// <summary>
/// Tries to execute a delegate action five times.
/// </summary>
/// <param name="delegateAction">The delegate to be executed</param>
private static void TryFiveTimes(Action delegateAction)
{
for (int retry = 0;; retry++)
{
try
{
delegateAction();
return;
}
catch (Exception)
{
if (retry >= 5)
{
throw;
}
}
}
}
}
}