diff --git a/src/ImageProcessor.Web.AzureBlobCache/AzureBlobCache.cs b/src/ImageProcessor.Web.AzureBlobCache/AzureBlobCache.cs index 72c23c5eab..dc6e1a74ca 100644 --- a/src/ImageProcessor.Web.AzureBlobCache/AzureBlobCache.cs +++ b/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); diff --git a/src/ImageProcessor.Web/Caching/DiskCache.cs b/src/ImageProcessor.Web/Caching/DiskCache.cs index e0e2ec5553..451183526e 100644 --- a/src/ImageProcessor.Web/Caching/DiskCache.cs +++ b/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)); diff --git a/src/ImageProcessor.Web/Caching/IImageCache.cs b/src/ImageProcessor.Web/Caching/IImageCache.cs index 3b2d1d3370..976d47012c 100644 --- a/src/ImageProcessor.Web/Caching/IImageCache.cs +++ b/src/ImageProcessor.Web/Caching/IImageCache.cs @@ -19,7 +19,7 @@ namespace ImageProcessor.Web.Caching Task IsNewOrUpdatedAsync(); - Task AddImageToCacheAsync(Stream stream); + Task AddImageToCacheAsync(Stream stream, string contentType); Task TrimCacheAsync(); diff --git a/src/ImageProcessor.Web/Caching/ImageCacheBase.cs b/src/ImageProcessor.Web/Caching/ImageCacheBase.cs index 84517be32c..b496025013 100644 --- a/src/ImageProcessor.Web/Caching/ImageCacheBase.cs +++ b/src/ImageProcessor.Web/Caching/ImageCacheBase.cs @@ -60,7 +60,7 @@ public abstract Task IsNewOrUpdatedAsync(); - public abstract Task AddImageToCacheAsync(Stream stream); + public abstract Task AddImageToCacheAsync(Stream stream, string contentType); public abstract Task TrimCacheAsync(); diff --git a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs index 160d3d48e2..d78bbc4850 100644 --- a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs +++ b/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 { cachedPath } - : new List { 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 { cachedPath } + : new List { requestPath, cachedPath }; + } + else + { + context.Items[CachedResponseFileDependency] = Path.GetFileName(requestPath) == requestPath + ? null + : new List { requestPath }; + } } - else + else if (isFileCached) { context.Items[CachedResponseFileDependency] = new List { cachedPath }; } diff --git a/src/TestWebsites/MVC/Views/Home/Index.cshtml b/src/TestWebsites/MVC/Views/Home/Index.cshtml index 39dc3930e6..f010a23d96 100644 --- a/src/TestWebsites/MVC/Views/Home/Index.cshtml +++ b/src/TestWebsites/MVC/Views/Home/Index.cshtml @@ -7,7 +7,7 @@

Resized

- + @*

Foreign language test.

diff --git a/src/TestWebsites/MVC/config/imageprocessor/cache.config b/src/TestWebsites/MVC/config/imageprocessor/cache.config index 38bce1f29c..5dee07828b 100644 --- a/src/TestWebsites/MVC/config/imageprocessor/cache.config +++ b/src/TestWebsites/MVC/config/imageprocessor/cache.config @@ -2,7 +2,7 @@ - +