Browse Source

Add correct mimetype and expires to blob

Former-commit-id: 807460a9faeb0b2bc5a7e2b2fed84db143865224
Former-commit-id: c68ca7af61e156ccb3088284ed9128115cd6f254
pull/17/head
James South 11 years ago
parent
commit
242021d76f
  1. 13
      src/ImageProcessor.Web.AzureBlobCache/AzureBlobCache.cs
  2. 4
      src/ImageProcessor.Web/Caching/DiskCache.cs
  3. 2
      src/ImageProcessor.Web/Caching/IImageCache.cs
  4. 2
      src/ImageProcessor.Web/Caching/ImageCacheBase.cs
  5. 23
      src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
  6. 2
      src/TestWebsites/MVC/Views/Home/Index.cshtml
  7. 2
      src/TestWebsites/MVC/config/imageprocessor/cache.config

13
src/ImageProcessor.Web.AzureBlobCache/AzureBlobCache.cs

@ -46,7 +46,7 @@
public AzureBlobCache(string requestPath, string fullPath, string querystring)
: base(requestPath, fullPath, querystring)
{
this.maxDays = Convert.ToInt32(this.Settings["MaxAge"]);
this.maxDays = Convert.ToInt32(this.Settings["MaxDays"]);
// Retrieve storage accounts from connection string.
this.cloudCachedStorageAccount = CloudStorageAccount.Parse(this.Settings["CachedStorageAccount"]);
@ -147,19 +147,24 @@
return isUpdated;
}
public override async Task AddImageToCacheAsync(Stream stream)
public override async Task AddImageToCacheAsync(Stream stream, string 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();
}
public override async Task TrimCacheAsync()
{
Uri uri = new Uri(this.CachedPath);
string path = uri.GetLeftPart(UriPartial.Path);
string path = uri.GetLeftPart(UriPartial.Path).Substring(this.cloudCachedBlobContainer.Uri.ToString().Length + 1);
string directory = path.Substring(0, path.LastIndexOf('/'));
string parent = directory.Substring(this.cloudCachedBlobContainer.Uri.ToString().Length + 1, path.LastIndexOf('/'));
string parent = directory.Substring(path.LastIndexOf('/'));
BlobContinuationToken continuationToken = null;
CloudBlobDirectory directoryBlob = this.cloudCachedBlobContainer.GetDirectoryReference(parent);

4
src/ImageProcessor.Web/Caching/DiskCache.cs

@ -61,7 +61,7 @@
public DiskCache(string requestPath, string fullPath, string querystring)
: base(requestPath, fullPath, querystring)
{
this.maxDays = Convert.ToInt32(this.Settings["MaxAge"]);
this.maxDays = Convert.ToInt32(this.Settings["MaxDays"]);
string virtualPath = this.Settings["VirtualCachePath"];
if (!virtualPath.IsValidVirtualPathName())
@ -138,7 +138,7 @@
return isUpdated;
}
public override async Task AddImageToCacheAsync(Stream stream)
public override async Task AddImageToCacheAsync(Stream stream, string contentType)
{
// ReSharper disable once AssignNullToNotNullAttribute
DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(this.CachedPath));

2
src/ImageProcessor.Web/Caching/IImageCache.cs

@ -19,7 +19,7 @@ namespace ImageProcessor.Web.Caching
Task<bool> IsNewOrUpdatedAsync();
Task AddImageToCacheAsync(Stream stream);
Task AddImageToCacheAsync(Stream stream, string contentType);
Task TrimCacheAsync();

2
src/ImageProcessor.Web/Caching/ImageCacheBase.cs

@ -60,7 +60,7 @@
public abstract Task<bool> IsNewOrUpdatedAsync();
public abstract Task AddImageToCacheAsync(Stream stream);
public abstract Task AddImageToCacheAsync(Stream stream, string contentType);
public abstract Task TrimCacheAsync();

23
src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs

@ -404,19 +404,30 @@ namespace ImageProcessor.Web.HttpModules
memoryStream.Position = 0;
// Add to the cache.
await this.imageCache.AddImageToCacheAsync(memoryStream);
await this.imageCache.AddImageToCacheAsync(memoryStream, imageFactory.CurrentImageFormat.MimeType);
// Store the cached path, response type, and cache dependency in the context for later retrieval.
context.Items[CachedPathKey] = cachedPath;
context.Items[CachedResponseTypeKey] = imageFactory.CurrentImageFormat.MimeType;
bool isFileCached = new Uri(cachedPath).IsFile;
if (isFileLocal)
{
// Some services might only provide filename so we can't monitor for the browser.
context.Items[CachedResponseFileDependency] = Path.GetFileName(requestPath) == requestPath
? new List<string> { cachedPath }
: new List<string> { requestPath, cachedPath };
if (isFileCached)
{
// Some services might only provide filename so we can't monitor for the browser.
context.Items[CachedResponseFileDependency] = Path.GetFileName(requestPath) == requestPath
? new List<string> { cachedPath }
: new List<string> { requestPath, cachedPath };
}
else
{
context.Items[CachedResponseFileDependency] = Path.GetFileName(requestPath) == requestPath
? null
: new List<string> { requestPath };
}
}
else
else if (isFileCached)
{
context.Items[CachedResponseFileDependency] = new List<string> { cachedPath };
}

2
src/TestWebsites/MVC/Views/Home/Index.cshtml

@ -7,7 +7,7 @@
<div class="row">
<div class="col-s-6">
<h2>Resized</h2>
<img src="/images/format-Penguins.jpg?width=305" />
<img src="/images/format-Penguins.jpg?width=310" />
@*<h3>Foreign language test.</h3>
<img src="/images/udendørs.jpg?width=300" />
<img src="/images/udendørs.jpg?preset=demo&filter=comic" />

2
src/TestWebsites/MVC/config/imageprocessor/cache.config

@ -2,7 +2,7 @@
<caches>
<cache name="DiskCache" type="ImageProcessor.Web.Caching.DiskCache, ImageProcessor.Web">
<settings>
<setting key="MaxAge" value="56"/>
<setting key="MaxDays" value="56"/>
<setting key="VirtualCachePath" value="~/app_data/cache"/>
</settings>
</cache>

Loading…
Cancel
Save